#include "werewolf.h"
#include <bits/stdc++.h>
using namespace std;
const int maxN = 2e5 + 20;
vector<int> adj[maxN][2];
vector<pair<int, int>> ch[maxN][2];
pair<int, int> par[maxN][2];
int depth[maxN][2];
int bit[maxN];
int root[2];
int Tin[maxN];
int Tout[maxN];
vector<vector<pair<int, pair<int, int>>>> queries[maxN];
vector<int> res;
int N, T;
int get_root(int u, int type) {
if (!par[u][type].second) {
return u;
}
else {
return get_root(par[u][type].second, type);
}
}
void join(int u, int v, int w, int type) {
int ru = get_root(u, type);
int rv = get_root(v, type);
if (ru == rv) {
return;
}
if (depth[ru][type] > depth[rv][type]) {
swap(ru, rv);
}
ch[rv][type].emplace_back(w, ru);
par[ru][type] = make_pair(w, rv);
if (depth[ru][type] == depth[rv][type]) {
depth[rv][type]++;
}
}
void dfs1(int u) {
Tin[u] = ++T;
for (auto e: ch[u][1]) {
dfs1(e.second);
}
Tout[u] = T;
}
void update(int pos, int val) {
for (int i = pos; i <= N; i += i & (-i)) {
bit[i] += val;
}
}
int get(int pos) {
int res = 0;
for (int i = pos; i > 0; i -= i & (-i)) {
res += bit[i];
}
return res;
}
void dfs2(int u, int add) {
update(Tin[u], add);
for (auto e: ch[u][0]) {
dfs2(e.second, add);
}
}
void solve(int u) {
for (int iter = 0; iter < 2; iter++) {
int add = (iter ? -1 : 1);
for (int i = 0; i <= (int)ch[u][0].size(); i++) {
if (i == 0) {
update(Tin[u], add);
}
else {
dfs2(ch[u][0][i - 1].second, add);
}
if (iter == 0) {
for (auto q: queries[u][i]) {
int id = q.first;
int lt = q.second.first;
int rt = q.second.second;
res[id] = get(rt) > get(lt - 1);
}
}
}
}
}
vector<int> check_validity(int _N, vector<int> X, vector<int> Y, vector<int> S, vector<int> E, vector<int> L, vector<int> R) {
N = _N;
int M = X.size();
int Q = S.size();
for (int i = 0; i < M; i++) {
X[i]++;
Y[i]++;
if (X[i] > Y[i]) {
swap(X[i], Y[i]);
}
adj[X[i]][0].push_back(Y[i]);
adj[Y[i]][1].push_back(X[i]);
}
for (int type = 0; type < 2; type++) {
for (int u = (type ? 1 : N); (type ? u <= N : u >= 1); u += (type ? 1 : -1)) {
for (auto v: adj[u][type]) {
join(u, v, u, type);
}
}
root[type] = get_root(1, type);
}
dfs1(root[1]);
for (int u = 1; u <= N; u++) {
queries[u].resize(ch[u][0].size() + 1);
}
res.resize(Q);
for (int id = 0; id < Q; id++) {
L[id]++;
R[id]++;
int u = S[id] + 1;
int v = E[id] + 1;
while (u != root[0] && par[u][0].first >= L[id]) {
u = par[u][0].second;
}
int pref_u = upper_bound(ch[u][0].begin(), ch[u][0].end(), make_pair(L[id], 0), greater<pair<int, int>>()) - ch[u][0].begin();
while (v != root[1] && par[v][1].first <= R[id]) {
v = par[v][1].second;
}
int pref_v = upper_bound(ch[v][1].begin(), ch[v][1].end(), make_pair(R[id], N + 1)) - ch[v][1].begin();
int lt = Tin[v];
int rt = (pref_v ? Tout[ch[v][1][pref_v - 1].second] : lt);
queries[u][pref_u].emplace_back(id, make_pair(lt, rt));
}
for (int u = 1; u <= N; u++) {
solve(u);
}
return res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23964 KB |
Output is correct |
2 |
Correct |
10 ms |
23792 KB |
Output is correct |
3 |
Correct |
14 ms |
23764 KB |
Output is correct |
4 |
Correct |
12 ms |
23764 KB |
Output is correct |
5 |
Correct |
11 ms |
23764 KB |
Output is correct |
6 |
Correct |
11 ms |
23760 KB |
Output is correct |
7 |
Correct |
11 ms |
23748 KB |
Output is correct |
8 |
Correct |
11 ms |
23752 KB |
Output is correct |
9 |
Correct |
10 ms |
23764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23964 KB |
Output is correct |
2 |
Correct |
10 ms |
23792 KB |
Output is correct |
3 |
Correct |
14 ms |
23764 KB |
Output is correct |
4 |
Correct |
12 ms |
23764 KB |
Output is correct |
5 |
Correct |
11 ms |
23764 KB |
Output is correct |
6 |
Correct |
11 ms |
23760 KB |
Output is correct |
7 |
Correct |
11 ms |
23748 KB |
Output is correct |
8 |
Correct |
11 ms |
23752 KB |
Output is correct |
9 |
Correct |
10 ms |
23764 KB |
Output is correct |
10 |
Correct |
15 ms |
24544 KB |
Output is correct |
11 |
Correct |
14 ms |
24432 KB |
Output is correct |
12 |
Correct |
14 ms |
24532 KB |
Output is correct |
13 |
Correct |
14 ms |
24440 KB |
Output is correct |
14 |
Correct |
14 ms |
24544 KB |
Output is correct |
15 |
Correct |
15 ms |
24620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
553 ms |
71296 KB |
Output is correct |
2 |
Correct |
309 ms |
70196 KB |
Output is correct |
3 |
Correct |
354 ms |
78768 KB |
Output is correct |
4 |
Correct |
401 ms |
78596 KB |
Output is correct |
5 |
Correct |
394 ms |
78888 KB |
Output is correct |
6 |
Correct |
439 ms |
79564 KB |
Output is correct |
7 |
Correct |
454 ms |
78876 KB |
Output is correct |
8 |
Correct |
315 ms |
78616 KB |
Output is correct |
9 |
Correct |
304 ms |
78188 KB |
Output is correct |
10 |
Correct |
330 ms |
78596 KB |
Output is correct |
11 |
Correct |
343 ms |
78404 KB |
Output is correct |
12 |
Correct |
421 ms |
78452 KB |
Output is correct |
13 |
Correct |
322 ms |
79332 KB |
Output is correct |
14 |
Correct |
307 ms |
79396 KB |
Output is correct |
15 |
Correct |
334 ms |
79664 KB |
Output is correct |
16 |
Correct |
313 ms |
79424 KB |
Output is correct |
17 |
Correct |
423 ms |
78696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
23964 KB |
Output is correct |
2 |
Correct |
10 ms |
23792 KB |
Output is correct |
3 |
Correct |
14 ms |
23764 KB |
Output is correct |
4 |
Correct |
12 ms |
23764 KB |
Output is correct |
5 |
Correct |
11 ms |
23764 KB |
Output is correct |
6 |
Correct |
11 ms |
23760 KB |
Output is correct |
7 |
Correct |
11 ms |
23748 KB |
Output is correct |
8 |
Correct |
11 ms |
23752 KB |
Output is correct |
9 |
Correct |
10 ms |
23764 KB |
Output is correct |
10 |
Correct |
15 ms |
24544 KB |
Output is correct |
11 |
Correct |
14 ms |
24432 KB |
Output is correct |
12 |
Correct |
14 ms |
24532 KB |
Output is correct |
13 |
Correct |
14 ms |
24440 KB |
Output is correct |
14 |
Correct |
14 ms |
24544 KB |
Output is correct |
15 |
Correct |
15 ms |
24620 KB |
Output is correct |
16 |
Correct |
553 ms |
71296 KB |
Output is correct |
17 |
Correct |
309 ms |
70196 KB |
Output is correct |
18 |
Correct |
354 ms |
78768 KB |
Output is correct |
19 |
Correct |
401 ms |
78596 KB |
Output is correct |
20 |
Correct |
394 ms |
78888 KB |
Output is correct |
21 |
Correct |
439 ms |
79564 KB |
Output is correct |
22 |
Correct |
454 ms |
78876 KB |
Output is correct |
23 |
Correct |
315 ms |
78616 KB |
Output is correct |
24 |
Correct |
304 ms |
78188 KB |
Output is correct |
25 |
Correct |
330 ms |
78596 KB |
Output is correct |
26 |
Correct |
343 ms |
78404 KB |
Output is correct |
27 |
Correct |
421 ms |
78452 KB |
Output is correct |
28 |
Correct |
322 ms |
79332 KB |
Output is correct |
29 |
Correct |
307 ms |
79396 KB |
Output is correct |
30 |
Correct |
334 ms |
79664 KB |
Output is correct |
31 |
Correct |
313 ms |
79424 KB |
Output is correct |
32 |
Correct |
423 ms |
78696 KB |
Output is correct |
33 |
Correct |
411 ms |
78976 KB |
Output is correct |
34 |
Correct |
195 ms |
57824 KB |
Output is correct |
35 |
Correct |
394 ms |
79424 KB |
Output is correct |
36 |
Correct |
434 ms |
79724 KB |
Output is correct |
37 |
Correct |
390 ms |
79116 KB |
Output is correct |
38 |
Correct |
439 ms |
79896 KB |
Output is correct |
39 |
Correct |
463 ms |
79708 KB |
Output is correct |
40 |
Correct |
404 ms |
85440 KB |
Output is correct |
41 |
Correct |
342 ms |
78196 KB |
Output is correct |
42 |
Correct |
336 ms |
78716 KB |
Output is correct |
43 |
Correct |
402 ms |
82492 KB |
Output is correct |
44 |
Correct |
362 ms |
78684 KB |
Output is correct |
45 |
Correct |
366 ms |
79524 KB |
Output is correct |
46 |
Correct |
359 ms |
78996 KB |
Output is correct |
47 |
Correct |
303 ms |
79464 KB |
Output is correct |
48 |
Correct |
349 ms |
79656 KB |
Output is correct |
49 |
Correct |
310 ms |
79584 KB |
Output is correct |
50 |
Correct |
305 ms |
79356 KB |
Output is correct |
51 |
Correct |
383 ms |
85436 KB |
Output is correct |
52 |
Correct |
416 ms |
85532 KB |
Output is correct |