利用 fread/fwrite 进行输入输出优化

Laffey 2022-10-20 21:14:40 2022-10-20 21:19:37

就是放个板子方便复制

#include <cstdio>
using namespace std;

namespace FastIO {
    namespace FastInput {
        const int SIZE = 1 << 21;
        char buffer[SIZE];
        const char *p1, *p2;

        inline const char get()
        {
            if (p1 == p2) {
                p1 = buffer, p2 = buffer + fread(buffer, 1, SIZE, stdin);
            }
            return p1 == p2 ? EOF : *p1++;
        }

        inline int read()
        {
            int ans = 0, nev = 1;
            char c = get();
            while (c < '0' || c > '9') {
                if (c == '-') {
                    nev = -1;
                }
                c = get();
            }
            while (c >= '0' && c <= '9') {
                ans = (ans << 1) + (ans << 3) + (c ^ 48);
                c = get();
            }
            return ans * nev;
        }
    }
    namespace FastOutput {
        const int SIZE = 1 << 21;
        char buffer[SIZE];
        char *p = buffer;
        const char* const tail = buffer + SIZE;

        inline void flush()
        {
            fwrite(buffer, 1, p - buffer, stdout);
        }

        inline void put(const char &c)
        {
            if (p == tail) {
                flush();
                p = buffer;
            }
            *p++ = c;
        }

        inline void write(int x)
        {
            static char t[100];
            int len = 0;
            if (x < 0) {
                put('-');
                x = -x;
            }
            do {
                t[len++] = x % 10UL + '0';
                x /= 10;
            } while (x);
            for (int i = len - 1; i >= 0; i--) {
                put(t[i]);
            }
        }

        inline void newline()
        {
            put('\n');
        }

        inline void space()
        {
            put(' ');
        }
    }

    using FastInput::read;
    using FastOutput::write;
    using FastOutput::newline;
    using FastOutput::space;
    using FastOutput::flush;
}

int main()
{
    freopen("in", "r", stdin);
    freopen("out", "w", stdout);
    using FastIO::read, FastIO::write, FastIO::newline, FastIO::flush;
    int n = 1e7;
    while (n--) {
        int a = read();
        write(a);
        newline();
    }
    flush();
}