【模板】快速读入

· · 科技·工程

struct fastio {
    static const int S = 1 << 21;
    char buf[S], *p1, *p2;
    char pbuf[S], *pp;

    fastio() : p1(buf), p2(buf), pp(pbuf) {}
    ~fastio() { fwrite(pbuf, 1, pp - pbuf, stdout); }

    inline char gc() {
        if (p1 == p2) p2 = (p1 = buf) + fread(buf, 1, S, stdin);
        return p1 == p2 ? EOF : *p1++;
    }

    template <typename T>
    inline void read(T &x) {
        x = 0; char c = gc(); bool f = false;
        for (; !isdigit(c); c = gc()) if (c == '-') f = true;
        for (; isdigit(c); c = gc()) x = (x << 1) + (x << 3) + (c ^ 48);
        if (f) x = -x;
    }

    inline void read(char &c) {
        do { c = gc(); } while (isspace(c));
    }

    inline void read(std::string &s) {
        s.clear(); char c = gc();
        while (isspace(c)) c = gc();
        while (!isspace(c) && c != EOF) s += c, c = gc();
    }

    template <typename T, typename... Args>
    inline void read(T &x, Args &...args) {
        read(x); read(args...);
    }

    inline void push(const char &c) {
        if (pp - pbuf == S) fwrite(pbuf, 1, S, stdout), pp = pbuf;
        *pp++ = c;
    }

    template <typename T>
    inline void write(T x) {
        if (x < 0) push('-'), x = -x;
        static int stk[35]; int top = 0;
        do { stk[top++] = x % 10; x /= 10; } while (x);
        while (top) push(stk[--top] + '0');
    }

    inline void write(char c) { push(c); }
    inline void write(const char *s) { while (*s) push(*s++); }
    inline void write(const std::string &s) { for (auto &c : s) push(c); }

    template <typename T, typename... Args>
    inline void write(T x, Args... args) {
        write(x); write(args...);
    }
} io;