常用模板

常用模板

造数据

#include<bits/stdc++.h>
using namespace std;
int main(){
    int x = 1;
    int y = 1000;
    random_device rd;
    mt19937 r_eng(rd());//r_eng是变量名
    uniform_int_distribution <int> dis(x, y);//生成x-y之间的高质量随机数。(dis,x,y是变量名)
    cout << dis(r_eng);//调用随机数
    return 0;
}

upd:

in

#include<bits/stdc++.h>
using namespace std;
string A="D:/data/ data/",B=".in",C=".out",op="Subtask";
random_device rd;
mt19937 rg(rd());
int rand(int x,int y) {
	uniform_int_distribution<int> dis(x,y);
	return dis(rg);
}
signed main() {
	for (int data= ;data<= ;data++) {
		string s=A+op+to_string(data)+B;
		freopen(s.c_str(),"w",stdout);

		fclose(stdout);
	}
	return 0;
}

out

#include<bits/stdc++.h>
using namespace std;
void solve() {

}
string A="D:/data/ data/",B=".in",C=".out",op="Subtask";
signed main() {
    for (int data= ;data<= ;data++) {
        string s=A+op+to_string(data)+B;
        freopen(s.c_str(),"r",stdin);
        s=A+op+to_string(data)+C;
        freopen(s.c_str(),"w",stdout);

        solve();

        fclose(stdin);
        fclose(stdout);
    }
    return 0;
}

高精度

高精度1(函数包装)

高精度2(符号替换)

高精度3(结构体+函数)(云课堂)

卡常

O3 优化

#pragma GCC optimize(3)

加速器

std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);

各种优化

link

快读快写

超级大快读

link

void

inline void read(int &x){
	x=0; 
	char c=0; 
	int w=0;  
	while(!isdigit(c))
      w=-1,c=getchar();  
	while(isdigit(c))
      x=x*10+(c^48),c=getchar();  
	if(w) x=-x;  
}

int

inline int read(){
	int s=0,w=1;
	char ch=getchar();
	while(ch<'0' || ch>'9') {
		if(ch=='-') w=-1;
		ch=getchar();
	}
	while(ch>='0' && ch<='9') {
		s=s*10+ch-'0';
		ch=getchar();
	}
	return s*w;
}

不分整数类型的快读

template<typename T>
inline void read(T &x){
	x=0; 
	char c=0; 
	T w=0;
	while(!isdigit(c)) w=-1,c=getchar();  
	while(isdigit(c)) x=x*10+(c^48),c=getchar();  
	if(w) x=-x;  
}

位运算优化

inline int read() {
    register int x=0,f=1;
    char c=getchar();
    while(c<'0' || c>'9') {
        if(c=='-') f=-1;
        c=getchar();
    }
    while(c>='0' && c<='9') {
        x=(x<<3)+(x<<1)+(c^48); //等价于x*10+c-48,使用位运算加速
        c=getchar();
    }
    return x*f;
}

unlocked

其实就是把 getchar 换成 getchar_unlocked,常数接近 fread

#include<bits/stdc++.h>
using namespace std;
inline int read(){
    register int x=0,f=1;
    char c=getchar_unlocked();
    while(c<'0' || c>'9') {
        if(c=='-') f=-1;
        c=getchar_unlocked();
    }
    while(c>='0' && c<='9') {
        x=(x<<3)+(x<<1)+(c^48);
        c=getchar_unlocked();
    }
    return x*f;
}
inline void write(int x){
    if(x<0) putchar('-'),x=-x;
    if(x>9) write(x/10);
    putchar(x%10+'0');
}

fread

//快读优化 
char buf[1<<21],*p1,*p2;
inline char gc(){
	if(p1==p2) p2=(p1=buf)+fread(buf,1,1<<21,stdin);
	return *(p1++);
}

inline int read(){//快读 
	int s=0,fu=1;char ch=gc();
	while(ch<'0'||ch>'9'){
		if(ch=='-')fu*=-1;
		ch=gc();
	}
	while(ch>='0'&&ch<='9'){
		s=(s<<1)+(s<<3)+(ch^48);
		ch=gc();
	}
	return s;
}

快写

inline void write(int x){
	if(x<0) putchar('-'),x=-x;
	if(x>9) write(x/10);
	putchar(x%10+'0');
	return;
}

手写栈优化快写

inline void write(int x) {
	int p=0,st[35];
	do {
		st[p++]=x%10,x/=10;
	}while(x);
	while(p) putchar(st[--p]+'0');
}

不分整数类型的快写

template <typename T>
inline void write(T x) {
    if(x<0) putchar('-'),x=-x;
    if(x<10) putchar(x+'0');
    else write(x/10),putchar(x%10+'0');
}

fwrite

#define bsiz 1000000

int sta[30];
char buf[bsiz], pbuf[bsiz], *p = pbuf, *s = buf, *t = buf;

#define mputc(ch) (p - pbuf == bsiz ? fwrite(pbuf, 1, bsiz, stdout), p = pbuf, *p++ = ch : *p++ = ch)

inline void putll(LL x) {
    register int top = 0;
    if (x<0) mputc('-'), x = -x;
        do sta[top++] = x % 10, x /= 10;
    while (x);
    while (top) mputc(sta[--top] + '0');
}

int main() {
    LL ans=123;
	putll(ans);
    fwrite(pbuf, 1, p - pbuf, stdout);
	return 0;
}

封装成类

#include<bits/stdc++.h>
using namespace std;
class GET{
private:
    int num;
    char c;
    string str;
    long long lng;
    stringstream strr;
    double d;
public:
    GET(){
        num=0;
        c=' ';
        str="";
        lng=0;
        strr.clear();
    }
    int get_int(char end='\n'){
        lng=0;
        c=getchar();
        while(c!=end){
            num=num*10+(c-48);
            c=getchar();
        }GET();
        return num;
    }
    long long get_long(char end='\n'){
        lng=0;
        c=getchar();
        while(c!=end){
            lng=lng*10+(c-48);
            c=getchar();
        }
        return lng;
    }
    string get_str(char end='\n'){
        strr.clear();
        str="";
        c=getchar();
        while(c!=end){
            strr<<c;
            c=getchar();
        }
        strr>>str;
        return str;
    }
    char get_char(void){
        return getchar();
    }
    double get_double(char end='\n'){
        d=0;
        int t=0;
        c=getchar();
        while(c!='.'){
            d=d*10+(c-48);
            c=getchar();
        }
        c=getchar();
        while(c!=end){
            d=(d*10+(c-48));
            t++;
            c=getchar();
        }
        return d/pow(10,t);
    }
};

namespace

namespace FAST_IO {
	template<typename T> void read(T &a) {
		a=0;
		int f=1;
		char c=getchar();
		while(!isdigit(c)) {
			if(c=='-') f=-1;
			c=getchar();
		}
		while(isdigit(c))
			a=a*10+c-'0',c=getchar();
		a=a*f;
	}
	template <typename T> void write(T a) {
		if(a<0) {
			a=-a;
			putchar('-');
		}
		if(a>9) write(a/10);
		putchar(a%10+'0');
	}
	template <typename T> void writeln(T a) {
		write(a);
		puts("");
	}
}
posted @ 2025-10-19 21:08  酱云兔  阅读(5)  评论(0)    收藏  举报