思路来源
https://blog.csdn.net/balalalalalalala/article/details/81869016
想测快读快不快,nth_element+快读CSU2078来战ORZ,
1e7的数据量配上均值意义的O(n),删了好几个假的快读板子
快读板子
整数读入输出挂
数组空间一般开10W,如果不能fread,把gc改成getchar
缓存空间大减少缓存次数,空间大开空间也需要时间,二者存在tradeoff
绝大多数情况用这种文件读入即可
namespace fastIO
{
static char buf[100000],*h=buf,*d=buf;//缓存开大可减少读入时间,看题目给的空间
#define gc h==d&&(d=(h=buf)+fread(buf,1,100000,stdin),h==d)?EOF:*h++//不能用fread则换成getchar
template<typename T>
inline void read(T&x)
{
int f = 1;x = 0;
register char c(gc);
while(c>'9'||c<'0'){
if(c == '-') f = -1;
c=gc;
}
while(c<='9'&&c>='0')x=(x<<1)+(x<<3)+(c^48),c=gc;
x *= f;
}
template<typename T>
void output(T x)
{
if(x<0){putchar('-');x=~(x-1);}
static int s[20],top=0;
while(x){s[++top]=x%10;x/=10;}
if(!top)s[++top]=0;
while(top)putchar(s[top--]+'0');
}
}
using namespace fastIO;
浮点数读入挂
inline bool scan_lf(double &num)
{
char in;double Dec=0.1;
bool IsN=false,IsD=false;
in=getchar();
if(in==EOF) return false;
while(in!='-'&&in!='.'&&(in<'0'||in>'9'))
in=getchar();
if(in=='-'){IsN=true;num=0;}
else if(in=='.'){IsD=true;num=0;}
else num=in-'0';
if(!IsD){
while(in=getchar(),in>='0'&&in<='9'){
num*=10;num+=in-'0';}
}
if(in!='.'){
if(IsN) num=-num;
return true;
}else{
while(in=getchar(),in>='0'&&in<='9'){
num+=Dec*(in-'0');Dec*=0.1;
}
}
if(IsN) num=-num;
return true;
}
cin关同步
cin读入的数据总是要放到缓存中,取消同步会快一点
一般都是scanf比cin快,所以平时除了cin>>string的时候好像用不到……
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
FastIO挂
视题目给的空间调整buffer_size的大小,一般开10W
缓存空间大减少缓存次数,空间大开空间也需要时间,二者存在tradeoff
短一点的板子,但略慢
namespace fastIO
{
#define BUF_SIZE 100000
bool IOerror = 0;
inline char nc(){
static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
if(p1==pend){
p1=buf;
pend=buf+fread(buf,1,BUF_SIZE,stdin);
}
return (*p1++);
}
inline int in(){
char c=nc();
while(!isdigit(c))c=nc();
int x=0;
while(isdigit(c)){
x=x*10+c-'0';
c=nc();
}
return x;
}
#undef BUF_SIZE
};
using namespace fastIO;
长一点的板子,相对来说较快
struct FastIO{
FILE *fp;
static const int S=1e5;
typedef long long ll;
int wpos;
char wbuf[S];
FastIO():wpos(0){}
inline int xchar(){
static char buf[S];
static int len=0,pos=0;
if(pos==len)pos=0,len=fread(buf,1,S,stdin);
return buf[pos++];
}
inline int xuint(){
int c=xchar(),x=0;
while(c<=32)c=xchar();
for(;'0'<=c&&c<='9';c=xchar())x=x*10+c-'0';
return x;
}
inline int xint(){
int s=1,c=xchar(),x=0;
while(c<=32)c=xchar();
if(c=='-')s=-1,c=xchar();
for(;'0'<=c&&c<='9';c=xchar())x=x*10+c-'0';
return x*s;
}
inline void xstring(char *s){
int c=xchar();
while(c<=32)c=xchar();
for(;c>32;c=xchar())*s++=c;
*s=0;
}
inline void wchar(int x){
if(wpos==S)fwrite(wbuf,1,S,stdout);
wbuf[wpos++]=x;
}
inline void wint(ll x){
if(x<0)wchar('-'),x=-x;
char s[24];
int n=0;
while(x||!n)s[n++]='0'+x%10,x/=10;
while(n--)wchar(s[n]);
wchar('\n');
}
inline void wstring(const char *s){
while(*s)wchar(*s++);
}
~FastIO(){
if(wpos)fwrite(wbuf,1,wpos,stdout),wpos=0;
}
}io;