-
Notifications
You must be signed in to change notification settings - Fork 0
/
base_form.cpp
318 lines (287 loc) · 7.86 KB
/
base_form.cpp
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
#define FNAME ""
#include <bits/stdc++.h>
using namespace std;
#define tcT template <class T
#define tcTU tcT, class U
tcT > using V = vector<T>;
tcT, size_t SZ > using AR = array<T, SZ>;
tcT > using PR = pair<T, T>;
template<typename... T>
void see(T&... args) { ((cin >> args), ...);}
template<typename... T>
void put(T&&... args) { ((cout << args << " "), ...);}
template<typename... T>
void putl(T&&... args) { ((cout << args << " "), ...); cout<<'\n';}
#define el '\n'
#define sp ' '
#define int long long
//pair
#define st first
#define nd second
#define mp(a,b) make_pair(a,b)
// vector , data structure
#define bg(x) begin(x)
#define eb(x) emplace_back(x);
#define pb(x) push_back(x);
#define pf(x) push_front(x);
#define ph(x) push(x);
#define PB pop_back();
#define PF pop_front();
#define all(a) bg(a),end(a)
#define rall(a) a.rbegin(),a.rend()
#define sz(a) (int) a.size()
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define mty empty()
#define ms(a,val) memset(a,val,sizeof(a))
#define lb lower_bound
#define ub upper_bound
tcT > int lwb(V<T> &a, const T &b) { return (int)(lb(all(a), b) - bg(a)); }
tcT > int upb(V<T> &a, const T &b) { return (int)(ub(all(a), b) - bg(a)); }
// loops
#define fu(i,a,b) for(int i=a;i<=b;++i)
#define fd(i,a,b) for(int i=a;i>=b;--i)
#define f0u(i,b) for(int i=0;i<b;++i)
#define f0d(i,b) for(int i=(b)-1;i>=0;--i)
#define FU(i,a,b,x) for (int i=a;i<=b;i+=x)
#define FD(i,a,b,x) for (int i=a;i>=b;i-=x)
#define trav(a, x) for (auto &a : x)
#define fos(it,s) for(set<int> :: iterator it=s.begin(); it!=s.end();++it)
#define seea(a,x,y) fu(i,x,y) {cin>>a[i];}
#define seev(v,n) for(int i=0;i<n;i++){int x; cin>>x; v.push_back(x);}
#define sees(s,n) for(int i=0;i<n;i++){int x; cin>>x; s.insert(x);}
//# pragma GCC target("avx")
//# pragma GCC target("avx2")
//# pragma GCC target("fma")
//# pragma GCC optimize("Os")
//# pragma GCC optimize("Ofast")
//# pragma GCC optimization ("03")
//# pragma GCC optimization ("unroll-loops")
//datatypes
//using ll = long long;
using str = string;
using db = double;
using ldb = long double;
using ull = unsigned long long;
// pair , vector
using pii = pair<int,int>;
using pd = pair<db,db>;
using mii = map<int,int>;
using vpi = vector<pii>;
using vpd = vector<pd>;
using vi = vector<int>;
using vd = vector<db>;
using vs = vector<str>;
using vvi = vector<vi>;
using vvd = vector<vd>;
const int MOD=1e9+7;
const int N=2e5+5;
const ldb PI = acos((ldb)-1);
const int dx[4] = {1, 0, -1, 0},
dy[4] = {0, 1, 0, -1};
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
template <class T> using pqg = priority_queue<T, vector<T>, greater<T>>;
template <class T> using pql = priority_queue<T, vector<T>, less<T>>;
tcT> long long add(T &a, const T b) { a+=b; return a>=MOD?a-MOD:a; }
//math
tcT>inline T gcd(T a,T b) { while (b != 0) swap(b, a %= b); return a; }
tcT>inline T lcm(T a,T b) { return a / gcd(a, b) * b; }
tcT>inline tuple<T,T,T> extgcd(T a,T b) {
if(a==0)return mt(b,0,1);
T g,x,y;
tie(g,x,y)=extgcd(b%a,a);
return mt(g,y-(b/a)*x,x);
}
//bitwise
constexpr int pct(int x) { return __builtin_popcount(x); }
constexpr int ctz(int x) { return __builtin_ctzll(x); }
constexpr int clz(int x) { return __builtin_clzll(x); }
constexpr int ffs(int x) { return __builtin_ffsll(x); }
constexpr int bits(int x) { // assert(x >= 0);
return x == 0 ? 0 : 31 - __builtin_clz(x);
} // floor(log2(x))
constexpr int pow2(int x) { return 1ll << (x); }
constexpr int msk2(int x) { return pow2(x) - 1; }
bool is_power2(int n){
//return pct(n)==1;
return n > 0 && (!(n & (n - 1)));
}
int cdiv(int a, int b) {
return a / b + ((a ^ b) > 0 && a % b);
} // divide a by b rounded up
int fdiv(int a, int b) {
return a / b - ((a ^ b) < 0 && a % b);
} // divide a by b rounded down
tcT > bool ckmin(T &a, const T &b) {
return b < a ? a = b, 1 : 0;
} // set a = min(a,b)
tcT > bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
tcTU > T fstTrue(T lo, T hi, U f) {
hi++;
assert(lo <= hi); // assuming f is increasing
while (lo < hi) { // find first index such that f is true
T mid = lo + ((hi - lo) >> 1);
f(mid) ? hi = mid : lo = mid + 1;
}
return lo;
}
tcTU > T lstTrue(T lo, T hi, U f) {
lo--;
assert(lo <= hi); // assuming f is decreasing
while (lo < hi) { // find first index such that f is true
T mid = lo + ((hi - lo + 1) >> 1);
f(mid) ? lo = mid : hi = mid - 1;
}
return lo;
}
tcT > void remDup(vector<T> &v) { // sort and remove duplicates
sort(all(v));
v.erase(unique(all(v)), end(v));
}
tcTU > void erase(T &t, const U &u) { // don't erase
auto it = t.find(u);
assert(it != end(t));
t.erase(it);
} // element that doesn't exist from (multi)set
// INPUT
#define tcTUU tcT, class... U
tcT > void re(complex<T> &c);
tcTU > void re(pair<T, U> &p);
tcT > void re(V<T> &v);
tcT, size_t SZ > void re(AR<T, SZ> &a);
tcT > void re(T &x) { cin >> x; }
void re(db &d) {
str t;
re(t);
d = stod(t);
}
void re(ldb &d) {
str t;
re(t);
d = stold(t);
}
tcTUU > void re(T &t, U &...u) {
re(t);
re(u...);
}
tcT > void re(complex<T> &c) {
T a, b;
re(a, b);
c = {a, b};
}
tcTU > void re(pair<T, U> &p) { re(p.f, p.s); }
tcT > void re(V<T> &x) { trav(a, x) re(a); }
tcT, size_t SZ > void re(AR<T, SZ> &x) { trav(a, x) re(a); }
tcT > void rv(int n, V<T> &x) {
x.rsz(n);
re(x);
}
// TO_STRING
#define ts to_string
str ts(char c) { return str(1, c); }
str ts(const char *s) { return (str)s; }
str ts(str s) { return s; }
str ts(bool b) {
#ifdef LOCAL
return b ? "true" : "false";
#else
return ts((int)b);
#endif
}
tcT > str ts(complex<T> c) {
stringstream ss;
ss << c;
return ss.str();
}
str ts(V<bool> v) {
str res = "{";
fu(i,0, sz(v)) res += char('0' + v[i]);
res += "}";
return res;
}
template <size_t SZ> str ts(bitset<SZ> b) {
str res = "";
fu(i,0, SZ) res += char('0' + b[i]);
return res;
}
tcTU > str ts(pair<T, U> p);
tcT > str ts(T v) { // containers with begin(), end()
#ifdef LOCAL
bool fst = 1;
str res = "{";
for (const auto &x : v) {
if (!fst) res += ", ";
fst = 0;
res += ts(x);
}
res += "}";
return res;
#else
bool fst = 1;
str res = "";
for (const auto &x : v) {
if (!fst) res += " ";
fst = 0;
res += ts(x);
}
return res;
#endif
}
tcTU > str ts(pair<T, U> p) {
#ifdef LOCAL
return "(" + ts(p.f) + ", " + ts(p.s) + ")";
#else
return ts(p.f) + " " + ts(p.s);
#endif
}
// OUTPUT
tcT > void pr(T x) { cout << ts(x); }
tcTUU > void pr(const T &t, const U &...u) {
pr(t);
pr(u...);
}
void ps() { pr("\n"); } // print w/ spaces
tcTUU > void ps(const T &t, const U &...u) {
pr(t);
if (sizeof...(u)) pr(" ");
ps(u...);
}
// DEBUG
void DBG() { cerr << "]" << endl; }
tcTUU > void DBG(const T &t, const U &...u) {
cerr << ts(t);
if (sizeof...(u)) cerr << ", ";
DBG(u...);
}
#ifdef LOCAL // compile with -DLOCAL, chk -> fake assert
#define dbg(...) \
cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", \
DBG(__VA_ARGS__)
#define chk(...) \
if (!(__VA_ARGS__)) \
cerr << "Line(" << __LINE__ << ") -> function(" << __FUNCTION__ \
<< ") -> CHK FAILED: (" << #__VA_ARGS__ << ")" \
<< "\n", \
exit(0);
#else
#define dbg(...) 0
#define chk(...) 0
#endif
void setPrec(signed x) { cout << fixed << setprecision(x); }
void unsyncIO() { cin.tie(0)->sync_with_stdio(0); }
// FILE I/O
void setIn(str s) { freopen(s.c_str(), "r", stdin); }
void setOut(str s) { freopen(s.c_str(), "w", stdout); }
void setIO(str s) {
unsyncIO(); //FAST
//setPrec(2);
// cin.exceptions(cin.failbit);
// ex. try to read letter into int
if (sz(s)>0) setIn(s + ".INP"), setOut(s + ".OUT");
}
signed main(signed argc, char *argv[]) {
setIO(FNAME);
return 0;
}