跳到正文

算法·理论

火车头

wangzihaomaster约 19 分钟
原文
文章信息与版本

文章信息

发布
2026年11月30日
收录
2026年11月30日
更新
2026年11月30日
篇幅
约 19 分钟

版本

  1. v1当前

火车头

Best 1

Best 1.0

  • 使用了缓冲区,禁止使用cin、cout、scanf、printf!
  • 只可用于:short,int,long long
  • 若需使用其他数据结构的到V2.1剪切对应数据结构!
C++
char ibuf[1<<20],obuf[1<<20],*ip1=ibuf,*ip2=ibuf,*op=obuf;
inline int gc(){
	if(ip1==ip2){
		ip2=(ip1=ibuf)+fread(ibuf,1,1<<20,stdin);
		if(ip1==ip2)return -1;
	}
	return *ip1++;
}
inline void pc(char c){
	if(op-obuf==1<<20)fwrite(obuf,1,1<<20,stdout),op=obuf;
	*op++=c;
}
struct Flusher{~Flusher(){fwrite(obuf,1,op-obuf,stdout);}}flusher;
template<typename T>
inline typename enable_if<is_integral<T>::value&&!is_unsigned<T>::value>::type read(T&x){
	x=0;
	int f=1,c=gc();
	while(c!='-'&&(c<'0'||c>'9')&&c!=-1)c=gc();
	if(c==-1)return;
	if(c=='-')f=-1,c=gc();
	while(c>='0'&&c<='9')x=x*10+(c-'0'),c=gc();
	if(f<0)x=-x;
}
template<typename T>
inline typename enable_if<is_integral<T>::value&&!is_unsigned<T>::value>::type write(T x){
	if(x<0)pc('-'),x=-x;
	if(x>9)write(x/10);
	pc(x%10+'0');
}

v1

v1.0

  • int/long long/short/double/float/long double/string/char can read()/write()
C++
#pragma GCC optimize("O3,Ofast,unroll-loops,inline")
#pragma GCC target("avx2")
char ibuf[1<<20],obuf[1<<20],*ip1=ibuf,*ip2=ibuf,*op=obuf;
inline int gc(){
	if(ip1==ip2){
		ip2=(ip1=ibuf)+fread(ibuf,1,1<<20,stdin);
		if(ip1==ip2)return -1;
	}
	return *ip1++;
}
inline void pc(char c){
	if(op-obuf==1<<20)fwrite(obuf,1,1<<20,stdout),op=obuf;
	*op++=c;
}
struct Flusher{~Flusher(){fwrite(obuf,1,op-obuf,stdout);}}flusher;
inline void read(){}
template<typename T>
inline typename enable_if<is_integral<T>::value&&!is_unsigned<T>::value>::type read(T&x){
	x=0;
	int f=1,c=gc();
	while(c!='-'&&(c<'0'||c>'9')&&c!=-1)c=gc();
	if(c==-1)return;
	if(c=='-')f=-1,c=gc();
	while(c>='0'&&c<='9')x=x*10+(c-'0'),c=gc();
	if(f<0)x=-x;
}
template<typename T>
inline typename enable_if<is_unsigned<T>::value>::type read(T&x){
	x=0;
	int c=gc();
	while((c<'0'||c>'9')&&c!=-1)c=gc();
	if(c==-1)return;
	while(c>='0'&&c<='9')x=x*10+(c-'0'),c=gc();
}
template<typename T>
inline typename enable_if<is_floating_point<T>::value>::type read(T&x){
	x=0;
	int f=1,c=gc();
	while(c!='-'&&(c<'0'||c>'9')&&c!=-1)c=gc();
	if(c==-1)return;
	if(c=='-')f=-1,c=gc();
	while(c>='0'&&c<='9')x=x*10+c-'0',c=gc();
	if(c=='.'){
		T w=1;
		c=gc();
		while(c>='0'&&c<='9')w/=10,x=x+(c-'0')*w,c=gc();
	}
	if(f<0)x=-x;
}
inline void read(char&c){
	int t=gc();
	while(t!=-1&&(t==' '||t=='\n'||t=='\r'||t=='\t'))t=gc();
	c=t;
}
inline void read(bool&x){
	char c;
	read(c);
	x=c=='1';
}
inline void read(string&s){
	s="";
	char c;
	read(c);
	if(c==-1)return;
	s+=c;
	int t=gc();
	while(t!=-1&&t!=' '&&t!='\n'&&t!='\r'&&t!='\t')s+=(char)t,t=gc();
}
template<typename T,typename...A>
inline void read(T&x,A&...a){
	read(x);
	read(a...);
}
inline void write(){}
template<typename T>
inline typename enable_if<is_integral<T>::value&&!is_unsigned<T>::value>::type write(T x){
	if(x<0)pc('-'),x=-x;
	if(x>9)write(x/10);
	pc(x%10+'0');
}
template<typename T>
inline typename enable_if<is_unsigned<T>::value>::type write(T x){
	if(x>9)write(x/10);
	pc(x%10+'0');
}
template<typename T>
inline typename enable_if<is_floating_point<T>::value>::type write(T x,int p=6){
	if(x<0)pc('-'),x=-x;
	write((long long)x);
	pc('.');
	while(p--)x=(x-(long long)x)*10,pc((int)x+'0');
}
inline void write(char c){pc(c);}
inline void write(bool x){pc(x?'1':'0');}
inline void write(const char*s){while(*s)pc(*s++);}
inline void write(const string&s){for(char c:s)pc(c);}
template<typename T,typename...A>
inline void write(T x,A...a){
	write(x);
	write(a...);
}

v2

v2.0

  • add __int128 read()/write()
C++
#include <bits/stdc++.h>
using namespace std;
char ibuf[1<<20],obuf[1<<20],*ip1=ibuf,*ip2=ibuf,*op=obuf;
inline int gc(){
	if(ip1==ip2){
		ip2=(ip1=ibuf)+fread(ibuf,1,1<<20,stdin);
		if(ip1==ip2)return -1;
	}
	return *ip1++;
}
inline void pc(char c){
	if(op-obuf==1<<20)fwrite(obuf,1,1<<20,stdout),op=obuf;
	*op++=c;
}
struct Flusher{~Flusher(){fwrite(obuf,1,op-obuf,stdout);}}flusher;
inline void read(){}
template<typename T>
inline typename enable_if<is_same<T, __int128>::value>::type read(T	&x){
	x=0;
	int f=1,c=gc();
	while(c!='-'&&(c<'0'||c>'9')&&c!=-1)c=gc();
	if(c==-1)return;
	if(c=='-')f=-1,c=gc();
	while(c>='0'&&c<='9')x=x*10+(c-'0'),c=gc();
	if(f<0)x=-x;
}
template<typename T>
inline typename enable_if<is_integral<T>::value&&!is_unsigned<T>::value>::type read(T&x){
	x=0;
	int f=1,c=gc();
	while(c!='-'&&(c<'0'||c>'9')&&c!=-1)c=gc();
	if(c==-1)return;
	if(c=='-')f=-1,c=gc();
	while(c>='0'&&c<='9')x=x*10+(c-'0'),c=gc();
	if(f<0)x=-x;
}
template<typename T>
inline typename enable_if<is_unsigned<T>::value>::type read(T&x){
	x=0;
	int c=gc();
	while((c<'0'||c>'9')&&c!=-1)c=gc();
	if(c==-1)return;
	while(c>='0'&&c<='9')x=x*10+(c-'0'),c=gc();
}
template<typename T>
inline typename enable_if<is_floating_point<T>::value>::type read(T&x){
	x=0;
	int f=1,c=gc();
	while(c!='-'&&(c<'0'||c>'9')&&c!=-1)c=gc();
	if(c==-1)return;
	if(c=='-')f=-1,c=gc();
	while(c>='0'&&c<='9')x=x*10+c-'0',c=gc();
	if(c=='.'){
		T w=1;
		c=gc();
		while(c>='0'&&c<='9')w/=10,x=x+(c-'0')*w,c=gc();
	}
	if(f<0)x=-x;
}
inline void read(char&c){
	int t=gc();
	while(t!=-1&&(t==' '||t=='\n'||t=='\r'||t=='\t'))t=gc();
	c=t;
}
inline void read(bool&x){
	char c;
	read(c);
	x=c=='1';
}
inline void read(string&s){
	s="";
	char c;
	read(c);
	if(c==-1)return;
	s+=c;
	int t=gc();
	while(t!=-1&&t!=' '&&t!='\n'&&t!='\r'&&t!='\t')s+=(char)t,t=gc();
}
template<typename T,typename...A>
inline void read(T&x,A&...a){
	read(x);
	read(a...);
}
inline void write(){}
template<typename T>
inline typename enable_if<is_same<T, __int128>::value>::type write(T x){
	if(x<0)pc('-'),x=-x;
	if(x>9)write(x/10);
	pc(x%10+'0');
}
template<typename T>
inline typename enable_if<is_integral<T>::value&&!is_unsigned<T>::value>::type write(T x){
	if(x<0)pc('-'),x=-x;
	if(x>9)write(x/10);
	pc(x%10+'0');
}
template<typename T>
inline typename enable_if<is_unsigned<T>::value>::type write(T x){
	if(x>9)write(x/10);
	pc(x%10+'0');
}
template<typename T>
inline typename enable_if<is_floating_point<T>::value>::type write(T x,int p=6){
	if(x<0)pc('-'),x=-x;
	write((long long)x);
	pc('.');
	while(p--)x=(x-(long long)x)*10,pc((int)x+'0');
}
inline void write(char c){pc(c);}
inline void write(bool x){pc(x?'1':'0');}
inline void write(const char*s){while(*s)pc(*s++);}
inline void write(const string&s){for(char c:s)pc(c);}
template<typename T,typename...A>
inline void write(T x,A...a){
	write(x);
	write(a...);
}

v2.1

  • 修复了已知的bug
C++
#include <bits/stdc++.h>
using namespace std;

char ibuf[1<<20],obuf[1<<20],*ip1=ibuf,*ip2=ibuf,*op=obuf;
inline int gc(){
	if(ip1==ip2){
		ip2=(ip1=ibuf)+fread(ibuf,1,1<<20,stdin);
		if(ip1==ip2)return EOF;
	}
	return *ip1++;
}
inline void pc(char c){
	if(op-obuf==1<<20)fwrite(obuf,1,1<<20,stdout),op=obuf;
	*op++=c;
}
struct Flusher{~Flusher(){fwrite(obuf,1,op-obuf,stdout);}}flusher;

template<typename T>
inline typename enable_if<is_same<T, __int128>::value>::type read(T &x){
	x=0;
	int f=1,c=gc();
	while(c!='-'&&(c<'0'||c>'9')&&c!=EOF)c=gc();
	if(c==EOF)return;
	if(c=='-')f=-1,c=gc();
	while(c>='0'&&c<='9')x=x*10+(c-'0'),c=gc();
	if(f<0)x=-x;
}
template<typename T>
inline typename enable_if<is_integral<T>::value&&!is_unsigned<T>::value&&!is_same<T,__int128>::value>::type read(T&x){
	x=0;
	int f=1,c=gc();
	while(c!='-'&&(c<'0'||c>'9')&&c!=EOF)c=gc();
	if(c==EOF)return;
	if(c=='-')f=-1,c=gc();
	while(c>='0'&&c<='9')x=x*10+(c-'0'),c=gc();
	if(f<0)x=-x;
}
template<typename T>
inline typename enable_if<is_unsigned<T>::value>::type read(T&x){
	x=0;
	int c=gc();
	while((c<'0'||c>'9')&&c!=EOF)c=gc();
	if(c==EOF)return;
	while(c>='0'&&c<='9')x=x*10+(c-'0'),c=gc();
}
template<typename T>
inline typename enable_if<is_floating_point<T>::value>::type read(T&x){
	x=0;
	int f=1,c=gc();
	while(c!='-'&&(c<'0'||c>'9')&&c!=EOF)c=gc();
	if(c==EOF)return;
	if(c=='-')f=-1,c=gc();
	while(c>='0'&&c<='9')x=x*10+c-'0',c=gc();
	if(c=='.'){
		T w=1;
		c=gc();
		while(c>='0'&&c<='9')w/=10,x=x+(c-'0')*w,c=gc();
	}
	if(f<0)x=-x;
}
inline void read(char&c){
	int t=gc();
	while(t!=EOF&&(t==' '||t=='\n'||t=='\r'||t=='\t'))t=gc();
	c=t;
}
inline void read(bool&x){
	char c;
	read(c);
	x=c=='1';
}
inline void read(string&s){
	s="";
	int t=gc();
	while(t!=EOF&&(t==' '||t=='\n'||t=='\r'||t=='\t'))t=gc();
	if(t==EOF)return;
	while(t!=EOF&&t!=' '&&t!='\n'&&t!='\r'&&t!='\t')s+=(char)t,t=gc();
}
template<typename T,typename...A>
inline void read(T&x,A&...a){
	read(x);
	read(a...);
}

inline void write(){}
template<typename T>
inline typename enable_if<is_same<T, __int128>::value>::type write(T x){
	if(x<0)pc('-'),x=-x;
	if(x>9)write(x/10);
	pc((char)(x%10+'0'));
}
template<typename T>
inline typename enable_if<is_integral<T>::value&&!is_unsigned<T>::value&&!is_same<T,__int128>::value>::type write(T x){
	if(x<0)pc('-'),x=-x;
	if(x>9)write(x/10);
	pc(x%10+'0');
}
template<typename T>
inline typename enable_if<is_unsigned<T>::value>::type write(T x){
	if(x>9)write(x/10);
	pc(x%10+'0');
}
template<typename T>
inline typename enable_if<is_floating_point<T>::value>::type write(T x,int p=6){
	if(x<0)pc('-'),x=-x;
	write((long long)x);
	pc('.');
	while(p--)x=(x-(long long)x)*10,pc((int)x+'0');
}
inline void write(char c){pc(c);}
inline void write(bool x){pc(x?'1':'0');}
inline void write(const char*s){while(*s)pc(*s++);}
inline void write(const string&s){for(char c:s)pc(c);}
template<typename T,typename...A>
inline void write(T x,A...a){
	write(x);
	write(a...);
}
原文