Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

Eau de Cologne

FastIO 메모 본문

rkgk

FastIO 메모

cologne 2023. 5. 25. 13:58
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <stdio.h>
#include <unistd.h>
#include <inttypes.h>
#include <sys/stat.h>
#include <sys/mman.h>
// This class only aims for fast input, skipping various checks.
// This class assumes input is well-formed, which means
// every token is separated by single whitespace (ascii <= 32)
class FastIO
{
    constexpr static off_t BUF_SIZE = 1 << 20;
    constexpr static char d2[] = "00010203040506070809101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899";
    char buf[BUF_SIZE];
    char *p;
    int ofd, idx;
 
public:
    // opens file descriptor
    explicit FastIO(int ifd = 0int ofd = 1)
    {
        struct stat st;
        fstat(ifd, &st);
        if (S_ISREG(st.st_mode))
            p = (char *)mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, ifd, 0);
        this->ofd = ofd;
        idx = 0;
    }
 
    ~FastIO()
    {
        flush();
        _exit(0);
    }
 
    void flush()
    {
        write(ofd, buf, idx);
        idx = 0;
    }
 
    void writeI64(int64_t x, char end)
    {
        static const off_t MAX_WIDTH = 21;
        if (idx + MAX_WIDTH > BUF_SIZE)
            flush();
 
        char tmp[MAX_WIDTH];
        int sz = 0;
 
        if (x < 0)
            buf[idx++= '-', x = -x;
        if (x == 0)
            tmp[sz++= '0';
        while (x)
        {
            if (x >= 10)
            {
                int64_t m = x % 100;
                tmp[sz++= d2[2 * m + 1];
                tmp[sz++= d2[2 * m];
                x /= 100;
            }
            else
            {
                tmp[sz++= '0' + (x % 10);
                break;
            }
        }
        while (sz)
            buf[idx++= tmp[--sz];
        buf[idx++= end;
    }
 
    void writeU64(uint64_t x, char end)
    {
        static const off_t MAX_WIDTH = 21;
        if (idx + MAX_WIDTH > BUF_SIZE)
            flush();
        char tmp[MAX_WIDTH];
        int sz = 0;
 
        if (x == 0)
            tmp[sz++= '0';
        while (x)
        {
            if (x >= 10)
            {
                uint64_t m = x % 100;
                tmp[sz++= d2[2 * m + 1];
                tmp[sz++= d2[2 * m];
                x /= 100;
            }
            else
            {
                tmp[sz++= '0' + (x % 10);
                break;
            }
        }
        while (sz)
            buf[idx++= tmp[--sz];
        buf[idx++= end;
    }
 
    uint64_t readU64()
    {
        uint64_t r = 0;
        if (p)
        {
            int t;
            r = *p++ - 48;
            while ((t = *p++ - 48>= 0)
                r = r * 10 + t;
        }
        else
            scanf("%" SCNu64, &r);
        return r;
    }
 
    int64_t readI64()
    {
        int64_t r = 0;
        if (p)
        {
            int t, s = 0;
            r = *p++ - 48;
            if (r < 0)
                s = 1, r = *p++ - 48;
            while ((t = *p++ - 48>= 0)
                r = r * 10 + t;
            if (s)
                r = -r;
        }
        else
            scanf("%" SCNd64, &r);
        return r;
    }
} io;
cs