#include "werewolf.h"
using namespace std;
#include <bits/stdc++.h>
class tree_t {
public:
int N, timer;
vector<int> tin, tout, a;
vector<vector<int>> g, par;
tree_t(int N = 0) : N(N), tin(N), tout(N), g(N), timer(0), a(N), par(__lg(N) + 1, vector<int>(N)) {}
void dfs(int u, int p) {
tin[u] = timer++;
par[0][u] = p;
for (int i = 1; i <= __lg(N); i++) par[i][u] = par[i - 1][par[i - 1][u]];
for (int v : g[u]) dfs(v, u);
tout[u] = timer;
}
};
class segtree_t {
public:
segtree_t *left, *right;
vector<int> val;
int l, r, m;
segtree_t(int l, int r) : l(l), r(r), m(l + r >> 1), val() {
if (l == r) return;
left = new segtree_t(l, m);
right = new segtree_t(m + 1, r);
}
void Update(int x, int y) {
val.emplace_back(y);
if (l == r) return;
if (x <= m) {
left->Update(x, y);
} else {
right->Update(x, y);
}
}
void Sort() {
sort(val.begin(), val.end());
if (l == r) return;
left->Sort(), right->Sort();
}
bool Get(int lx, int rx, int ly, int ry) {
if (l > rx || r < lx) return 0;
if (lx <= l && r <= rx) return lower_bound(val.begin(), val.end(), ry) - lower_bound(val.begin(), val.end(), ly);
return left->Get(lx, rx, ly, ry) || right->Get(lx, rx, ly, ry);
}
};
std::vector<int> check_validity(int N, std::vector<int> X, std::vector<int> Y,
std::vector<int> S, std::vector<int> E,
std::vector<int> L, std::vector<int> R) {
int M = X.size(), Q = L.size();
vector<vector<int>> adj(N);
for (int i = 0; i < M; i++) adj[X[i]].emplace_back(Y[i]);
for (int i = 0; i < M; i++) adj[Y[i]].emplace_back(X[i]);
vector<int> par(N, -1);
function<int(int)> root = [&](int u) { return par[u] < 0 ? u : (par[u] = root(par[u])); };
int cur_N = N;
tree_t TL(N * 2 - 1), TR(N * 2 - 1);
for (int i = 0; i < N; i++) {
TL.a[i] = i;
for (int j : adj[i]) {
if (j >= i) continue;
int u = root(i), v = root(j);
if (u == v) continue;
par.emplace_back(par[u] + par[v]);
TL.g[cur_N].emplace_back(u);
TL.g[cur_N].emplace_back(v);
TL.a[cur_N] = i;
par[u] = par[v] = cur_N++;
}
}
par = vector<int>(N, -1);
cur_N = N;
for (int i = N - 1; i >= 0; i--) {
TR.a[i] = i;
for (int j : adj[i]) {
if (j <= i) continue;
int u = root(i), v = root(j);
if (u == v) continue;
par.emplace_back(par[u] + par[v]);
TR.g[cur_N].emplace_back(u);
TR.g[cur_N].emplace_back(v);
TR.a[cur_N] = i;
par[u] = par[v] = cur_N++;
}
}
const int Root = N * 2 - 1;
TL.dfs(Root - 1, Root - 1);
TR.dfs(Root - 1, Root - 1);
segtree_t *tree = new segtree_t(0, Root);
for (int i = 0; i < N; i++) {
int x = TL.tin[i], y = TR.tin[i];
tree->Update(x, y);
}
tree->Sort();
vector<int> ans;
for (int i = 0; i < Q; i++) {
int s = S[i], e = E[i], l = L[i], r = R[i];
for (int j = __lg(Root); j >= 0; j--) {
if (TR.a[TR.par[j][s]] >= l) s = TR.par[j][s];
if (TL.a[TL.par[j][e]] <= r) e = TL.par[j][e];
}
ans.emplace_back(tree->Get(TL.tin[e], TL.tout[e] - 1, TR.tin[s], TR.tout[s]));
}
return ans;
}
Compilation message
werewolf.cpp: In constructor 'tree_t::tree_t(int)':
werewolf.cpp:8:29: warning: 'tree_t::g' will be initialized after [-Wreorder]
8 | vector<vector<int>> g, par;
| ^
werewolf.cpp:6:16: warning: 'int tree_t::timer' [-Wreorder]
6 | int N, timer;
| ^~~~~
werewolf.cpp:10:9: warning: when initialized here [-Wreorder]
10 | tree_t(int N = 0) : N(N), tin(N), tout(N), g(N), timer(0), a(N), par(__lg(N) + 1, vector<int>(N)) {}
| ^~~~~~
werewolf.cpp: In constructor 'segtree_t::segtree_t(int, int)':
werewolf.cpp:27:51: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
27 | segtree_t(int l, int r) : l(l), r(r), m(l + r >> 1), val() {
| ~~^~~
werewolf.cpp:25:19: warning: 'segtree_t::m' will be initialized after [-Wreorder]
25 | int l, r, m;
| ^
werewolf.cpp:24:21: warning: 'std::vector<int> segtree_t::val' [-Wreorder]
24 | vector<int> val;
| ^~~
werewolf.cpp:27:9: warning: when initialized here [-Wreorder]
27 | segtree_t(int l, int r) : l(l), r(r), m(l + r >> 1), val() {
| ^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
8 ms |
3164 KB |
Output is correct |
11 |
Correct |
8 ms |
3416 KB |
Output is correct |
12 |
Correct |
7 ms |
3168 KB |
Output is correct |
13 |
Correct |
8 ms |
3160 KB |
Output is correct |
14 |
Correct |
8 ms |
3164 KB |
Output is correct |
15 |
Correct |
8 ms |
3252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1691 ms |
206944 KB |
Output is correct |
2 |
Correct |
1188 ms |
209960 KB |
Output is correct |
3 |
Correct |
1122 ms |
207796 KB |
Output is correct |
4 |
Correct |
1367 ms |
206996 KB |
Output is correct |
5 |
Correct |
1501 ms |
206872 KB |
Output is correct |
6 |
Correct |
1712 ms |
206660 KB |
Output is correct |
7 |
Correct |
1570 ms |
206976 KB |
Output is correct |
8 |
Correct |
1135 ms |
210152 KB |
Output is correct |
9 |
Correct |
927 ms |
207864 KB |
Output is correct |
10 |
Correct |
689 ms |
206828 KB |
Output is correct |
11 |
Correct |
756 ms |
215264 KB |
Output is correct |
12 |
Correct |
1004 ms |
215336 KB |
Output is correct |
13 |
Correct |
1408 ms |
220888 KB |
Output is correct |
14 |
Correct |
1336 ms |
221220 KB |
Output is correct |
15 |
Correct |
1296 ms |
221032 KB |
Output is correct |
16 |
Correct |
1389 ms |
221404 KB |
Output is correct |
17 |
Correct |
1494 ms |
215716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
8 ms |
3164 KB |
Output is correct |
11 |
Correct |
8 ms |
3416 KB |
Output is correct |
12 |
Correct |
7 ms |
3168 KB |
Output is correct |
13 |
Correct |
8 ms |
3160 KB |
Output is correct |
14 |
Correct |
8 ms |
3164 KB |
Output is correct |
15 |
Correct |
8 ms |
3252 KB |
Output is correct |
16 |
Correct |
1691 ms |
206944 KB |
Output is correct |
17 |
Correct |
1188 ms |
209960 KB |
Output is correct |
18 |
Correct |
1122 ms |
207796 KB |
Output is correct |
19 |
Correct |
1367 ms |
206996 KB |
Output is correct |
20 |
Correct |
1501 ms |
206872 KB |
Output is correct |
21 |
Correct |
1712 ms |
206660 KB |
Output is correct |
22 |
Correct |
1570 ms |
206976 KB |
Output is correct |
23 |
Correct |
1135 ms |
210152 KB |
Output is correct |
24 |
Correct |
927 ms |
207864 KB |
Output is correct |
25 |
Correct |
689 ms |
206828 KB |
Output is correct |
26 |
Correct |
756 ms |
215264 KB |
Output is correct |
27 |
Correct |
1004 ms |
215336 KB |
Output is correct |
28 |
Correct |
1408 ms |
220888 KB |
Output is correct |
29 |
Correct |
1336 ms |
221220 KB |
Output is correct |
30 |
Correct |
1296 ms |
221032 KB |
Output is correct |
31 |
Correct |
1389 ms |
221404 KB |
Output is correct |
32 |
Correct |
1494 ms |
215716 KB |
Output is correct |
33 |
Correct |
1873 ms |
215808 KB |
Output is correct |
34 |
Correct |
203 ms |
32464 KB |
Output is correct |
35 |
Correct |
1786 ms |
219232 KB |
Output is correct |
36 |
Correct |
1828 ms |
215688 KB |
Output is correct |
37 |
Correct |
1761 ms |
217784 KB |
Output is correct |
38 |
Correct |
1763 ms |
216364 KB |
Output is correct |
39 |
Correct |
1717 ms |
222824 KB |
Output is correct |
40 |
Correct |
1262 ms |
224944 KB |
Output is correct |
41 |
Correct |
1091 ms |
217204 KB |
Output is correct |
42 |
Correct |
851 ms |
215800 KB |
Output is correct |
43 |
Correct |
1181 ms |
222832 KB |
Output is correct |
44 |
Correct |
1363 ms |
218000 KB |
Output is correct |
45 |
Correct |
1101 ms |
222812 KB |
Output is correct |
46 |
Correct |
1033 ms |
222588 KB |
Output is correct |
47 |
Correct |
1261 ms |
219432 KB |
Output is correct |
48 |
Correct |
1238 ms |
219256 KB |
Output is correct |
49 |
Correct |
1228 ms |
219688 KB |
Output is correct |
50 |
Correct |
1284 ms |
219796 KB |
Output is correct |
51 |
Correct |
984 ms |
226088 KB |
Output is correct |
52 |
Correct |
982 ms |
225924 KB |
Output is correct |