#include "werewolf.h"
#include <bits/stdc++.h>
using namespace std;
struct DSU {
vector<vector<int>> g;
vector<int> link;
vector<int> euler, l, r;
DSU(int n) {
g.resize(n), link.resize(n);
l.resize(n), r.resize(n);
iota(link.begin(), link.end(), 0);
}
int getP(int x) {
if (link[x] == x) return x;
return link[x] = getP(link[x]);
}
void unite(int dad, int son) {
dad = getP(dad), son = getP(son);
if (dad == son) return;
link[son] = dad;
g[dad].push_back(son);
}
void dfs(int v) {
l[v] = euler.size();
euler.push_back(v);
for (int u : g[v])
dfs(u);
r[v] = euler.size();
}
};
struct MST {
vector<vector<int>> t;
MST(vector<int>& a) {
t.resize(a.size() * 4);
build(0, 0, a.size(), a);
}
void build(int v, int tl, int tr, vector<int>& a) {
if (tl + 1 == tr) {
t[v].push_back(a[tl]);
return;
}
int m = (tl + tr) / 2;
build(2 * v + 1, tl, m, a);
build(2 * v + 2, m, tr, a);
t[v].resize(t[2 * v + 1].size() + t[2 * v + 2].size());
merge(t[2 * v + 1].begin(), t[2 * v + 1].end(), t[2 * v + 2].begin(), t[2 * v + 2].end(), t[v].begin());
}
int get(int v, int tl, int tr, int l, int r, int lo, int hi) {
if (tl >= r || tr <= l)
return 0;
if (tl >= l && tr <= r) {
return lower_bound(t[v].begin(), t[v].end(), hi) - lower_bound(t[v].begin(), t[v].end(), lo);
}
int m = (tl + tr) / 2;
return get(2 * v + 1, tl, m, l, r, lo, hi) + get(2 * v + 2, m, tr, l, r, lo, hi);
}
};
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 = S.size();
vector<vector<int>> g(n);
for (int i = 0; i < m; i++)
g[X[i]].push_back(Y[i]), g[Y[i]].push_back(X[i]);
vector<vector<int>> le(n), re(n);
for (int i = 0; i < q; i++)
le[L[i]].push_back(i), re[R[i]].push_back(i);
vector<int> lp(q);
DSU dl(n);
for (int i = n - 1; i >= 0; i--) {
for (int j : g[i])
if (j > i)
dl.unite(i, j);
for (int ind : le[i])
lp[ind] = dl.getP(S[ind]);
}
int dl_root = dl.getP(0);
dl.dfs(dl_root);
vector<int> rp(q);
DSU dr(n);
for (int i = 0; i < n; i++) {
for (int j : g[i])
if (j < i)
dr.unite(i, j);
for (int ind : re[i])
rp[ind] = dr.getP(E[ind]);
}
int dr_root = dr.getP(0);
dr.dfs(dr_root);
vector<int> p_dl(n);
for (int i = 0; i < n; i++)
p_dl[dl.euler[i]] = i;
vector<int> a(n);
for (int i = 0; i < n; i++)
a[i] = p_dl[dr.euler[i]];
vector<int> ans(q);
MST t(a);
for (int i = 0; i < q; i++) {
int l1 = dl.l[lp[i]], r1 = dl.r[lp[i]];
int l2 = dr.l[rp[i]], r2 = dr.r[rp[i]];
int x = t.get(0, 0, n, l2, r2, l1, r1);
ans[i] = x > 0;
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
9 ms |
1876 KB |
Output is correct |
11 |
Correct |
7 ms |
1876 KB |
Output is correct |
12 |
Correct |
5 ms |
1876 KB |
Output is correct |
13 |
Correct |
9 ms |
2004 KB |
Output is correct |
14 |
Correct |
7 ms |
2004 KB |
Output is correct |
15 |
Correct |
7 ms |
2004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
579 ms |
106916 KB |
Output is correct |
2 |
Correct |
830 ms |
110148 KB |
Output is correct |
3 |
Correct |
805 ms |
108784 KB |
Output is correct |
4 |
Correct |
890 ms |
108228 KB |
Output is correct |
5 |
Correct |
843 ms |
108192 KB |
Output is correct |
6 |
Correct |
718 ms |
108116 KB |
Output is correct |
7 |
Correct |
566 ms |
105840 KB |
Output is correct |
8 |
Correct |
821 ms |
110156 KB |
Output is correct |
9 |
Correct |
706 ms |
107680 KB |
Output is correct |
10 |
Correct |
465 ms |
106952 KB |
Output is correct |
11 |
Correct |
570 ms |
107092 KB |
Output is correct |
12 |
Correct |
719 ms |
107812 KB |
Output is correct |
13 |
Correct |
711 ms |
114948 KB |
Output is correct |
14 |
Correct |
690 ms |
115000 KB |
Output is correct |
15 |
Correct |
872 ms |
115048 KB |
Output is correct |
16 |
Correct |
663 ms |
115076 KB |
Output is correct |
17 |
Correct |
592 ms |
105732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
9 ms |
1876 KB |
Output is correct |
11 |
Correct |
7 ms |
1876 KB |
Output is correct |
12 |
Correct |
5 ms |
1876 KB |
Output is correct |
13 |
Correct |
9 ms |
2004 KB |
Output is correct |
14 |
Correct |
7 ms |
2004 KB |
Output is correct |
15 |
Correct |
7 ms |
2004 KB |
Output is correct |
16 |
Correct |
579 ms |
106916 KB |
Output is correct |
17 |
Correct |
830 ms |
110148 KB |
Output is correct |
18 |
Correct |
805 ms |
108784 KB |
Output is correct |
19 |
Correct |
890 ms |
108228 KB |
Output is correct |
20 |
Correct |
843 ms |
108192 KB |
Output is correct |
21 |
Correct |
718 ms |
108116 KB |
Output is correct |
22 |
Correct |
566 ms |
105840 KB |
Output is correct |
23 |
Correct |
821 ms |
110156 KB |
Output is correct |
24 |
Correct |
706 ms |
107680 KB |
Output is correct |
25 |
Correct |
465 ms |
106952 KB |
Output is correct |
26 |
Correct |
570 ms |
107092 KB |
Output is correct |
27 |
Correct |
719 ms |
107812 KB |
Output is correct |
28 |
Correct |
711 ms |
114948 KB |
Output is correct |
29 |
Correct |
690 ms |
115000 KB |
Output is correct |
30 |
Correct |
872 ms |
115048 KB |
Output is correct |
31 |
Correct |
663 ms |
115076 KB |
Output is correct |
32 |
Correct |
592 ms |
105732 KB |
Output is correct |
33 |
Correct |
807 ms |
115464 KB |
Output is correct |
34 |
Correct |
267 ms |
35408 KB |
Output is correct |
35 |
Correct |
1016 ms |
117388 KB |
Output is correct |
36 |
Correct |
730 ms |
115740 KB |
Output is correct |
37 |
Correct |
908 ms |
116660 KB |
Output is correct |
38 |
Correct |
798 ms |
116116 KB |
Output is correct |
39 |
Correct |
829 ms |
122220 KB |
Output is correct |
40 |
Correct |
720 ms |
123100 KB |
Output is correct |
41 |
Correct |
801 ms |
116120 KB |
Output is correct |
42 |
Correct |
463 ms |
114504 KB |
Output is correct |
43 |
Correct |
1148 ms |
121964 KB |
Output is correct |
44 |
Correct |
914 ms |
116648 KB |
Output is correct |
45 |
Correct |
683 ms |
121440 KB |
Output is correct |
46 |
Correct |
737 ms |
121196 KB |
Output is correct |
47 |
Correct |
643 ms |
122400 KB |
Output is correct |
48 |
Correct |
648 ms |
122308 KB |
Output is correct |
49 |
Correct |
641 ms |
122312 KB |
Output is correct |
50 |
Correct |
644 ms |
122184 KB |
Output is correct |
51 |
Correct |
609 ms |
120948 KB |
Output is correct |
52 |
Correct |
627 ms |
120844 KB |
Output is correct |