快读快写(卡常版)

· · 个人记录

namespace FastIO {
    const int BUFFER_SIZE = 1 << 20;
    char in_buf[BUFFER_SIZE];
    char *in_ptr = in_buf;
    char *in_end = in_buf;
    char out_buf[BUFFER_SIZE];
    char *out_ptr = out_buf;
    inline void load() {
        in_end = in_buf + fread(in_buf, 1, BUFFER_SIZE, stdin);
        in_ptr = in_buf;
    }
    inline char getchar() {
        if (in_ptr == in_end) {
            load();
            if (in_ptr == in_end) return EOF;
        }
        return *in_ptr++;
    }
    inline void putchar(char c) {
        if (out_ptr == out_buf + BUFFER_SIZE) {
            fwrite(out_buf, 1, BUFFER_SIZE, stdout);
            out_ptr = out_buf;
        }
        *out_ptr++ = c;
    }
    inline void flush() {
        fwrite(out_buf, 1, out_ptr - out_buf, stdout);
        out_ptr = out_buf;
    }
    template <typename T>
    inline bool read(T &x) {
        x = 0;
        char c = getchar();
        bool negative = false;
        while (c != EOF && isspace(c)) c = getchar();
        if (c == EOF) return false;
        if (c == '-') {
            negative = true;
            c = getchar();
        }
        while (c != EOF && isdigit(c)) {
            x = x * 10 + (c - '0');
            c = getchar();
        }
        if (negative) x = -x;
        return true;
    }
    inline bool read(std::string &s) {
        s.clear();
        char c = getchar();
        while (c != EOF && isspace(c)) c = getchar();
        if (c == EOF) return false;
        while (c != EOF && !isspace(c)) {
            s.push_back(c);
            c = getchar();
        }
        return true;
    }
    inline bool read(char *s) {
        char c = getchar();
        while (c != EOF && isspace(c)) c = getchar();
        if (c == EOF) return false;
        while (c != EOF && !isspace(c)) {
            *s++ = c;
            c = getchar();
        }
        *s = '\0';
        return true;
    }
    template <typename T>
    inline void write(T x) {
        if (x < 0) {
            putchar('-');
            x = -x;
        }
        char tmp[40];
        int len = 0;
        do {
            tmp[len++] = '0' + (x % 10);
            x /= 10;
        } while (x);
        while (len--) putchar(tmp[len]);
    }
    inline void write(const char *s) {
        while (*s) putchar(*s++);
    }
    inline void writeln() {
        putchar('\n');
    }
    template <typename T>
    inline void writeln(T x) {
        write(x);
        putchar('\n');
    }
    inline void writeln(const char *s) {
        write(s);
        putchar('\n');
    }
}
using namespace FastIO;