火车头

· · 算法·理论

火车头

Best 1

Best 1.0

v2

v2.0

#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...);
}