CFTemplate

这是我的Codeforces模板

Show Code
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#include <bits/stdc++.h>
namespace fastio {
// tested (integer, string and char only)
template<size_t BF_SIZE>
class FastIO {
private:
char inbuf[BF_SIZE];
char outbuf[BF_SIZE];
char intStr[64];
size_t l = 0;
size_t r = 0;
size_t outidx = 0;
public:
~FastIO() {
flush();
}
static inline bool isNspace(char c) {
return !isspace(c);
}
inline char gc() {
if (l >= r) {
l = 0;
r = fread(inbuf, 1, BF_SIZE, stdin);
}
if (l < r) {
return inbuf[l++];
}
return EOF;
}
template<typename Int>
inline void getInt(Int& x) {
x = 0; bool p = 0; char ch = gc();
while (!isdigit(ch)) p |= (ch == '-'), ch = gc();
while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ '0'), ch = gc();
if (p) x = -x;
}
template<typename UInt>
inline void getUInt(UInt& x) {
x = 0; char ch = gc();
while (!isdigit(ch)) ch = gc();
while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ '0'), ch = gc();
}
inline void getChr(char& c, std::function<bool(char)> mask = isNspace) {
c = gc();
while (c != EOF && !mask(c)) c = gc();
}
inline void getStr(std::string& s, std::function<bool(char)> mask = isNspace) {
s.clear(); char ch = gc();
while (ch != EOF && !mask(ch)) ch = gc();
while (ch != EOF && mask(ch)) s += ch, ch = gc();
}
inline void pc(char c) {
outbuf[outidx++] = c;
if (outidx == BF_SIZE) flush();
}
inline void putStr(const std::string& s) {
const char* sc = s.c_str();
size_t n = s.length();
size_t idx = 0;
while (idx < n) {
if (n - idx <= BF_SIZE - outidx) memcpy(outbuf + outidx, sc + idx, n - idx), outidx += n - idx, idx = n;
else memcpy(outbuf + outidx, sc + idx, BF_SIZE - outidx), idx += BF_SIZE - outidx, outidx = BF_SIZE;
if (outidx == BF_SIZE) flush();
}
}
template<typename Int>
inline void putInt(Int x) {
if (x < 0) {
pc('-');
x = -x;
}
int idx = 0;
do {
intStr[idx++] = '0' + x % 10;
x /= 10;
} while (x);
for (--idx; idx >= 0; --idx) {
pc(intStr[idx]);
}
}
template<typename UInt>
inline void putUInt(UInt x) {
int idx = 0;
do {
intStr[idx++] = '0' + x % 10;
x /= 10;
} while (x);
for (--idx; idx >= 0; --idx) {
pc(intStr[idx]);
}
}
inline void flush() {
fwrite(outbuf, 1, outidx, stdout);
outidx = 0;
}
// ---
FastIO& operator >>(int& other) {
getInt(other);
return *this;
}
FastIO& operator >>(long long& other) {
getInt(other);
return *this;
}
FastIO& operator >>(short& other) {
getInt(other);
return *this;
}
FastIO& operator >>(__int128& other) {
getInt(other);
return *this;
}
FastIO& operator >>(unsigned int& other) {
getUInt(other);
return *this;
}
FastIO& operator >>(unsigned long long& other) {
getUInt(other);
return *this;
}
FastIO& operator >>(unsigned short& other) {
getUInt(other);
return *this;
}
FastIO& operator >>(unsigned __int128& other) {
getUInt(other);
return *this;
}
FastIO& operator >>(char& other) {
getChr(other);
return *this;
}
FastIO& operator >>(std::string& other) {
getStr(other);
return *this;
}
// ---
FastIO& operator <<(int other) {
putInt(other);
return *this;
}
FastIO& operator <<(long long other) {
putInt(other);
return *this;
}
FastIO& operator <<(__int128 other) {
putInt(other);
return *this;
}
FastIO& operator <<(unsigned int other) {
putUInt(other);
return *this;
}
FastIO& operator <<(unsigned long long other) {
putUInt(other);
return *this;
}
FastIO& operator <<(unsigned short other) {
putUInt(other);
return *this;
}
FastIO& operator <<(unsigned __int128 other) {
putUInt(other);
return *this;
}
FastIO& operator <<(short other) {
putInt(other);
return *this;
}
FastIO& operator <<(const std::string& s) {
putStr(s);
return *this;
}
FastIO& operator <<(char c) {
pc(c);
return *this;
}

};
}
namespace bitree {
// BIT template (tested)
template<typename T>
class BIT {
private:
std::vector<T> tr;
int size;
public:
BIT(int n) : tr(n + 2, 0), size(n) {}
inline void add(int x, T v) {
// add v to position x
assert(x >= 0 && x <= size);
++x;
for (; x <= size + 1; x += x & -x) tr[x] += v;
}
inline T sum(int x) {
// calculate the sum from 0 to x
assert(x >= 0 && x <= size);
++x;
T res = 0;
for (; x; x -= x & -x) res += tr[x];
return res;
}
inline T sum(int x, int y) {
assert(x >= 0 && y <= size);
if (x > y) return 0;
return sum(y) - sum(x - 1);
}
inline int kth(T v) {
// if this is a count tree, find the kth value on the tree
assert(v >= 0);
int x = 0;
for (int now = log2(size + 1) + 2; now >= 0; --now) {
if (x + (1 << now) <= size + 1 && tr[x + (1 << now)] < v) {
x += (1 << now);
v -= tr[x];
}
}
return x;
}
};
}
namespace segtree {
// tested
template <typename T>
class SegTree {
// range add / range Query
private:
std::vector<T> data;
std::vector<T> lz;
int data_min, data_max;
inline void add(int k, int l, int r, T v) {
data[k] += v * (r - l + 1);
lz[k] += v;
}
inline void pushup(int k) {
data[k] = data[k << 1] + data[k << 1 | 1];
}
inline void pushdown(int k, int l, int r, int mid) {
if (lz[k]) {
add(k << 1, l, mid, lz[k]);
add(k << 1 | 1, mid + 1, r, lz[k]);
lz[k] = 0;
}
}
void modify(int k, int l, int r, int le, int ri, T v) {
if (l >= le && r <= ri) return add(k, l, r, v);
int mid = (l + r) >> 1;
pushdown(k, l, r, mid);
if (mid >= le) modify(k << 1, l, mid, le, ri, v);
if (mid < ri) modify(k << 1 | 1, mid + 1, r, le, ri, v);
pushup(k);
}
T query(int k, int l, int r, int le, int ri) {
if (l >= le && r <= ri) return data[k];
int mid = (l + r) >> 1; T res = 0;
pushdown(k, l, r, mid);
if (mid >= le) res += query(k << 1, l, mid, le, ri);
if (mid < ri) res += query(k << 1 | 1, mid + 1, r, le, ri);
return res;
}
void build(int k, int l, int r, const std::vector<T>& v, int begin) {
lz[k] = 0;
if (l == r) return data[k] = v[l - data_min + begin], void();
int mid = (l + r) >> 1;
build(k << 1, l, mid, v, begin);
build(k << 1 | 1, mid + 1, r, v, begin);
pushup(k);
}
void print(int k, int l, int r) {
std::cerr << "k = " << k << ", l = " << l << ", r = " << r << ", data = " << data[k] << '\n';
if (l == r) return;
int mid = (l + r) >> 1;
pushdown(k, l, r, mid);
print(k << 1, l, mid);
print(k << 1 | 1, mid + 1, r);
}
public:
SegTree(int _data_min, int _data_max) :
data((_data_max - _data_min + 2) << 2, 0),
lz((_data_max - _data_min + 2) << 2, 0),
data_min(_data_min),
data_max(_data_max) {
// _data_min: lower_bound of data; _data_max: upper_bound of data
assert(_data_max >= _data_min);
}
inline void modify(int le, int ri, T v) {
// add v to all data in [le, ri]
modify(1, data_min, data_max, le, ri, v);
}
inline T query(int le, int ri) {
// query the sum of data in [le, ri]
return query(1, data_min, data_max, le, ri);
}
inline void build(const std::vector<T>& v, int begin) {
// build the tree based on array v from index 'begin'
assert(int(v.size()) - begin >= data_max - data_min + 1);
build(1, data_min, data_max, v, begin);
}
inline void clear() {
// clear the whole tree
build(std::vector<T>(data_max - data_min + 1), 0);
}
inline void print() {
// print the whole tree
std::cerr << "Current Tree is :::\n";
print(1, data_min, data_max);
}
};
}
namespace simplemath {
template <typename T>
inline T gcd(T a, T b) {
if (a < b) a ^= b ^= a ^= b;
while (b) {
a %= b;
a ^= b ^= a ^= b;
}
return a;
}
template <typename T>
inline T lcm(T a, T b) {
return a * b / gcd(a, b);
}
using LL = long long;
inline int fpow(int a, int b, int MOD) {
int x = 1;
while (b) {
if (b & 1) x = LL(x) * a % MOD;
a = LL(a) * a % MOD;
b >>= 1;
}
return x;
}
class Factoral {
// calculates factoral related problems
private:
std::vector<int> jc;
std::vector<int> nc;
inline void init() {
jc[0] = 1;
for (int i = 1; i <= size; ++i) jc[i] = LL(jc[i - 1]) * i % MOD;
nc[size] = fpow(jc[size], MOD - 2, MOD);
for (int i = size; i >= 1; --i) nc[i - 1] = LL(nc[i]) * i % MOD;
}
public:
const int MOD;
const int size;
Factoral(int max_n, int _mod) : jc(max_n + 1, 0), nc(max_n + 1, 0), MOD(_mod), size(max_n) {init();};
inline int C(int x, int y) {
assert(x >= 0 && y >= 0 && x >= y && x <= size);
return LL(jc[x]) * nc[y] % MOD * nc[x - y] % MOD;
}
inline int A(int x, int y) {
assert(x >= 0 && y >= 0 && x >= y && x <= size);
return LL(jc[x]) * nc[x - y] % MOD;
}
inline int fac(int x) {
assert(x >= 0 && x <= size);
return jc[x];
}
inline int fac_inv(int x) {
assert(x >= 0 && x <= size);
return nc[x];
}
inline int inv(int x) {
assert(x >= 1 && x < MOD);
if (x >= size) {
return fpow(x, MOD - 2, MOD);
}
return LL(nc[x]) * jc[x - 1] % MOD;
}
};
}
using namespace simplemath;
using namespace segtree;
using namespace fastio;
using namespace bitree;
FastIO<1> io;
// FastIO<1 << 16> io;
using LL = long long;

signed main() {
int t; io >> t;
while (t--) {

}
return 0;
}