Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
372 lines (321 loc) · 9.78 KB
/
Copy pathmain.cpp
File metadata and controls
372 lines (321 loc) · 9.78 KB
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
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <unordered_set>
#include <queue>
#include <deque>
#include <string>
#include <sstream>
#include <iomanip>
#include <map>
#include <unordered_map>
#include <stack>
#include <cstdio>
#include <climits>
#include <tuple>
#include <ctime>
#include <cstring>
#include <numeric>
#include <functional>
#include <chrono>
#include <cassert>
#include <bitset>
#include <fstream>
#include <random>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define lld long double
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define vr(v) v.begin(),v.end()
#define rv(v) v.rbegin(),v.rend()
#define Code ios_base::sync_with_stdio(false);
#define By cin.tie(NULL);
#define ME cout.tie(NULL);
using namespace std;
// using namespace __gnu_pbds;
// template<typename T, typename Compare = less<T>>
// using indexed_set = tree<
// T,
// null_type,
// Compare,
// rb_tree_tag,
// tree_order_statistics_node_update>;
//indexed_set<int> int_set;
//*int_set.find_by_order(1)
//int_set.order_of_key(1)
namespace RND {
mt19937 myrand(chrono::steady_clock::now().time_since_epoch().count());
//mt19937 myrand(373);
ll RANDOM(ll l, ll r) {
ll res = myrand() % (r - l + 1) + l;
return res;
}
void shuffle(vector<int> &a) {
for (int i = 0; i < static_cast<int>(a.size()); i++) {
int idx = RANDOM(0, i);
swap(a[i], a[idx]);
}
}
};
class DSU {
public:
DSU(int n) {
m_parent.resize(n);
m_size.resize(n);
for (int i = 0; i < n; i++)m_size[i] = 1, m_parent[i] = i;
}
int get(int v) {
return v == m_parent[v] ? v : (m_parent[v] = get(m_parent[v]));
}
void merge(int a, int b) {
a = get(a);
b = get(b);
if (a != b) {
if (m_size[a] < m_size[b]) swap(a, b);
m_parent[b] = a;
m_size[a] += m_size[b];
}
}
private:
vector<int> m_parent, m_size;
};
class TRIE {
public:
bool search(const string &word) {
int node = 0;
for (int i = 0; i < (int) word.size(); i++) {
char c = word[i];
if (m_trie[node][c - 'a'] == 0) return false;
node = m_trie[node][c - 'a'];
}
return true;
}
void insert(const string &word) {
int node = 0;
for (char c: word) {
if (m_trie[node][c - 'a'] == 0) {
m_trie.push_back(vector<int>(26));
m_trie[node][c - 'a'] = (int) m_trie.size() - 1;
}
node = m_trie[node][c - 'a'];
}
}
private:
vector<vector<int> > m_trie;
};
class LCA {
public:
LCA(int n) {
while ((1 << m_LOG) <= n)m_LOG++;
m_gp = vector<vector<int> >(n);
m_up = vector<vector<int> >(n, vector<int>(m_LOG));
m_depth = vector<int>(n);
for (int i = 0; i < m_LOG; i++) m_up[0][i] = -1;
}
void run() {
dfs(0);
}
int getKthParent(int u, int k) {
for (int i = m_LOG - 1; i >= 0; i--) {
if (k & (1 << i)) {
u = m_up[u][i];
if (u == -1)break;
}
}
return u;
}
int lca(int a, int b) {
if (m_depth[a] < m_depth[b])swap(a, b);
int diff = m_depth[a] - m_depth[b];
for (int i = m_LOG - 1; i >= 0; i--) {
if (diff & (1 << i)) a = m_up[a][i];
}
if (a == b)return a;
for (int i = m_LOG - 1; i >= 0; i--) {
if (m_up[a][i] != m_up[b][i]) {
a = m_up[a][i];
b = m_up[b][i];
}
}
return m_up[a][0];
}
void addEdge(int u, int v) {
//u is parent of v
m_gp[u].push_back(v);
}
private:
void dfs(int u) {
for (auto v: m_gp[u]) {
m_depth[v] = m_depth[u] + 1;
m_up[v][0] = u;
for (int i = 1; i < m_LOG; i++) {
if (m_up[v][i - 1] == -1)m_up[v][i] = -1;
else m_up[v][i] = m_up[m_up[v][i - 1]][i - 1];
}
dfs(v);
}
}
private:
vector<vector<int> > m_gp;
vector<vector<int> > m_up;
vector<int> m_depth;
int m_LOG = 1;
};
class DIJKSTRA {
public:
DIJKSTRA(int n) : m_n(n) {
m_gp = vector<vector<pair<int, long long> > >(n);
}
void addEdge(int u, int v, long long w) {
m_gp[u].push_back({v, w});
}
vector<long long> getDist(int u) {
vector<ll> p(m_n, -1), d(m_n, INF);
set<pair<ll, ll> > q;
q.insert({0, u});
d[u] = 0;
while (!q.empty()) {
ll v = q.begin()->second;
q.erase(q.begin());
for (auto edge: m_gp[v]) {
ll to = edge.first;
ll len = edge.second;
if (d[v] + len < d[to]) {
q.erase({d[to], to});
d[to] = d[v] + len;
p[to] = v;
q.insert({d[to], to});
}
}
}
return d;
}
private:
const long long INF = 1e18;
int m_n;
vector<vector<pair<int, long long> > > m_gp;
};
class SegmentTree {
//m_modify_type : {assign, add}
//m_query_type : {sum,min,max}
// vector<long long> a = {1, 2, 3, 4, 5};
// SegmentTree seg1(a.size(), "add", "sum");
// seg1.build(a);
// std::cout<<seg1.get(1, 3);
// seg1.modify(1, 3, 10);
public:
SegmentTree(int size, const string &modify_type, const string &query_type)
: m_n(size), m_modify_type(modify_type), m_query_type(query_type) {
m_seg.resize(4 * m_n);
if (m_query_type == "sum") m_default_value = 0;
else if (m_query_type == "min") m_default_value = LLONG_MAX;
else if (m_query_type == "max") m_default_value = LLONG_MIN;
}
void build(const vector<long long> &arr) {
build(0, 0, m_n - 1, arr);
}
void modify(int l, int r, long long val) {
modify(0, 0, m_n - 1, l, r, val);
}
long long get(int l, int r) {
return get(0, 0, m_n - 1, l, r);
}
private:
void build(int node, int l, int r, const vector<long long> &arr) {
if (l == r) {
m_seg[node].value = arr[l];
} else {
int m = (l + r) / 2;
build(2 * node + 1, l, m, arr);
build(2 * node + 2, m + 1, r, arr);
m_seg[node].value = combine(m_seg[2 * node + 1].value, m_seg[2 * node + 2].value);
}
}
void push(int node, int l, int r) {
if (m_seg[node].has_assign) {
m_seg[node].value = apply_assign(m_seg[node].lazy_assign, r - l + 1);
if (l != r) {
apply_assign_lazy(2 * node + 1, m_seg[node].lazy_assign);
apply_assign_lazy(2 * node + 2, m_seg[node].lazy_assign);
}
m_seg[node].lazy_assign = 0;
m_seg[node].lazy_add = 0;
m_seg[node].has_assign = false;
} else if (m_seg[node].lazy_add != 0) {
m_seg[node].value = apply_add(m_seg[node].value, m_seg[node].lazy_add, r - l + 1);
if (l != r) {
m_seg[2 * node + 1].lazy_add += m_seg[node].lazy_add;
m_seg[2 * node + 2].lazy_add += m_seg[node].lazy_add;
}
m_seg[node].lazy_add = 0;
}
}
void apply_assign_lazy(int node, long long val) {
m_seg[node].lazy_assign = val;
m_seg[node].has_assign = true;
m_seg[node].lazy_add = 0;
}
long long apply_add(long long current, long long val, int len) {
if (m_query_type == "sum") return current + val * len;
else return current + val;
}
long long apply_assign(long long val, int len) {
if (m_query_type == "sum") return val * len;
else return val;
}
long long combine(long long a, long long b) {
if (m_query_type == "sum") return a + b;
else if (m_query_type == "min") return min(a, b);
else return max(a, b);
}
void modify(int node, int l, int r, int ql, int qr, long long val) {
push(node, l, r);
if (qr < l || r < ql) return;
if (ql <= l && r <= qr) {
if (m_modify_type == "assign") {
apply_assign_lazy(node, val);
} else if (m_modify_type == "add") {
m_seg[node].lazy_add += val;
}
push(node, l, r);
return;
}
int m = (l + r) / 2;
modify(2 * node + 1, l, m, ql, qr, val);
modify(2 * node + 2, m + 1, r, ql, qr, val);
m_seg[node].value = combine(m_seg[2 * node + 1].value, m_seg[2 * node + 2].value);
}
long long get(int node, int l, int r, int ql, int qr) {
push(node, l, r);
if (qr < l || r < ql) return m_default_value;
if (ql <= l && r <= qr) return m_seg[node].value;
int m = (l + r) / 2;
long long left = get(2 * node + 1, l, m, ql, qr);
long long right = get(2 * node + 2, m + 1, r, ql, qr);
return combine(left, right);
}
private:
struct Node {
long long value = 0;
long long lazy_add = 0;
long long lazy_assign = 0;
bool has_assign = false;
};
int m_n;
vector<Node> m_seg;
string m_modify_type, m_query_type;
long long m_default_value;
};
void solve() {
}
int main() {
int t = 1;
// cin >> t;
while (t--)solve();
}