快读快写模板

· · 个人记录

int read(){
    int x=0,f=1;//x表示当前数字,f表示符号正负
    char c=getchar();
    while(c<'0'||c>'9')//如果读进来的是符号
    {
        if(c=='-') f=-1;
        c=getchar();
    }
    while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar();

    return x*f;
}

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

//高级快读快写,不用管数据类型,read传参可以传多个,类型随便搞,不用担心快读是int自己传参传的是long long

如io::read(n, m, a, c); io::write(a + b);

namespace io{
    template <typename T> inline void read(T& x){x = 0; bool f = false; char ch = getchar(); while(ch < '0' || ch > '9'){if(ch == '-') f = !f; ch = getchar();}while(ch >= '0' && ch <= '9'){x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar();} x = (f ? -x : x); return;}
    template <typename T, typename... Args> inline void read(T& x, Args&...x_){read(x), read(x_...);}
    template <typename T> inline void put(T x){if(x < 0) putchar('-'), x = -x; if(x > 9) put(x / 10); putchar(x % 10 + '0'); return ;}
    template <typename T> inline void write(T x){put(x);}
    template <typename T, typename... Args> inline void write(T x, Args...x_){write(x), write(x_...);}
}

//使用fread的快读快写,速度比上面更快
namespace io{
    char buf[1<<20],*p1,*p2;
    #define gc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf,1,1<<20,stdin), p1 == p2) ? 0 : *p1++)
    template <typename T> inline void read(T &x){T w=1; x=0;char c=gc();while(!isdigit(c)){ if(c=='-') w=-1; c=gc();}while(isdigit(c)){ x=(x<<1)+(x<<3)+(c^48); c=gc();}x=x*w;}
    template <typename T, typename... Args> inline void read(T& x, Args&...x_){read(x), read(x_...);}
    template <typename T> inline void put(T x){if(x < 0) putchar('-'), x = -x; if(x > 9) put(x / 10); putchar(x % 10 + '0'); return ;}
    template <typename T> inline void write(T x){put(x);}
    template <typename T, typename... Args> inline void write(T x, Args...x_){write(x), write(x_...);}
}
using namespace io;