// rama_pang orz
// solution and codestyle adapted from his/her submission
#include<bits/stdc++.h>
using namespace std;
struct DisjointSet {
int n;
vector<int> f;
DisjointSet () = default;
DisjointSet (const int& _n): n(_n), f(n) { iota(begin(f), end(f), 0); }
int F(int u) { return u == f[u] ? u : f[u] = F(f[u]); }
bool M(int u, int v) {
u = F(u); v = F(v);
if (u != v) f[v] = u;
return u != v;
}
};
const int kN = 500005;
int N, Q, C[kN];
vector<int> key_pos[kN];
bool can_unlock(int l, int r, int c) {
return *lower_bound(begin(key_pos[c]), end(key_pos[c]), l) <= r;
}
pair<int, int> memo[kN];
bool in_stack[kN];
DisjointSet dsu(kN);
array<int, 3> solve(int u) {
if (in_stack[u]) return {u, u, u};
if (dsu.F(u) != u) return solve(dsu.F(u));
if (memo[u].first != 0) return {0, memo[u].first, memo[u].second};
in_stack[u] = true;
int lb = u, rb = u;
while (true) {
if (lb > 1 and can_unlock(lb, rb, C[lb - 1])) {
auto nxt = solve(lb - 1);
lb = min(lb, nxt[1]);
rb = max(rb, nxt[2]);
if (nxt[0] != 0 and dsu.M(nxt[0], u)) {
in_stack[u] = false;
return {nxt[0], lb, rb};
}
} else if (rb < N and can_unlock(lb, rb, C[rb])) {
auto nxt = solve(rb + 1);
lb = min(lb, nxt[1]);
rb = max(rb, nxt[2]);
if (nxt[0] != 0 and dsu.M(nxt[0], u)) {
in_stack[u] = false;
return {nxt[0], lb, rb};
}
} else break;
}
in_stack[u] = false;
memo[u] = {lb, rb};
return {0, lb, rb};
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> N;
for (int i = 1; i < N; ++i)
cin >> C[i];
for (int B, A, i = 1; i <= N; ++i) {
cin >> B;
while (B --> 0) {
cin >> A;
key_pos[A].emplace_back(i);
}
}
for (int i = 1; i <= N; ++i)
key_pos[i].emplace_back(N + 1);
cin >> Q;
for (int X, Y, i = 0; i < Q; ++i) {
cin >> X >> Y;
const auto ans = solve(X);
cout << (ans[1] <= Y and Y <= ans[2] ? "YES\n" : "NO\n");
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
14164 KB |
Output is correct |
2 |
Correct |
10 ms |
14164 KB |
Output is correct |
3 |
Correct |
10 ms |
14420 KB |
Output is correct |
4 |
Correct |
10 ms |
14080 KB |
Output is correct |
5 |
Correct |
10 ms |
14164 KB |
Output is correct |
6 |
Correct |
10 ms |
14164 KB |
Output is correct |
7 |
Correct |
10 ms |
14164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
14164 KB |
Output is correct |
2 |
Correct |
10 ms |
14164 KB |
Output is correct |
3 |
Correct |
10 ms |
14420 KB |
Output is correct |
4 |
Correct |
10 ms |
14080 KB |
Output is correct |
5 |
Correct |
10 ms |
14164 KB |
Output is correct |
6 |
Correct |
10 ms |
14164 KB |
Output is correct |
7 |
Correct |
10 ms |
14164 KB |
Output is correct |
8 |
Correct |
104 ms |
15656 KB |
Output is correct |
9 |
Correct |
98 ms |
15564 KB |
Output is correct |
10 |
Correct |
100 ms |
16004 KB |
Output is correct |
11 |
Correct |
95 ms |
16232 KB |
Output is correct |
12 |
Correct |
88 ms |
15872 KB |
Output is correct |
13 |
Correct |
97 ms |
15780 KB |
Output is correct |
14 |
Correct |
94 ms |
15820 KB |
Output is correct |
15 |
Correct |
93 ms |
16028 KB |
Output is correct |
16 |
Correct |
91 ms |
16208 KB |
Output is correct |
17 |
Correct |
94 ms |
15864 KB |
Output is correct |
18 |
Correct |
93 ms |
15828 KB |
Output is correct |
19 |
Correct |
94 ms |
15880 KB |
Output is correct |
20 |
Correct |
96 ms |
16068 KB |
Output is correct |
21 |
Correct |
93 ms |
16432 KB |
Output is correct |
22 |
Correct |
94 ms |
15916 KB |
Output is correct |
23 |
Correct |
95 ms |
15772 KB |
Output is correct |
24 |
Correct |
92 ms |
15684 KB |
Output is correct |
25 |
Correct |
93 ms |
15652 KB |
Output is correct |
26 |
Correct |
91 ms |
15604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
168 ms |
22468 KB |
Output is correct |
2 |
Correct |
224 ms |
23976 KB |
Output is correct |
3 |
Correct |
159 ms |
22604 KB |
Output is correct |
4 |
Correct |
163 ms |
22304 KB |
Output is correct |
5 |
Correct |
164 ms |
22348 KB |
Output is correct |
6 |
Correct |
150 ms |
22988 KB |
Output is correct |
7 |
Correct |
145 ms |
29268 KB |
Output is correct |
8 |
Correct |
141 ms |
34124 KB |
Output is correct |
9 |
Correct |
146 ms |
30188 KB |
Output is correct |
10 |
Correct |
146 ms |
26616 KB |
Output is correct |
11 |
Correct |
141 ms |
22844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
14164 KB |
Output is correct |
2 |
Correct |
10 ms |
14164 KB |
Output is correct |
3 |
Correct |
10 ms |
14420 KB |
Output is correct |
4 |
Correct |
10 ms |
14080 KB |
Output is correct |
5 |
Correct |
10 ms |
14164 KB |
Output is correct |
6 |
Correct |
10 ms |
14164 KB |
Output is correct |
7 |
Correct |
10 ms |
14164 KB |
Output is correct |
8 |
Correct |
104 ms |
15656 KB |
Output is correct |
9 |
Correct |
98 ms |
15564 KB |
Output is correct |
10 |
Correct |
100 ms |
16004 KB |
Output is correct |
11 |
Correct |
95 ms |
16232 KB |
Output is correct |
12 |
Correct |
88 ms |
15872 KB |
Output is correct |
13 |
Correct |
97 ms |
15780 KB |
Output is correct |
14 |
Correct |
94 ms |
15820 KB |
Output is correct |
15 |
Correct |
93 ms |
16028 KB |
Output is correct |
16 |
Correct |
91 ms |
16208 KB |
Output is correct |
17 |
Correct |
94 ms |
15864 KB |
Output is correct |
18 |
Correct |
93 ms |
15828 KB |
Output is correct |
19 |
Correct |
94 ms |
15880 KB |
Output is correct |
20 |
Correct |
96 ms |
16068 KB |
Output is correct |
21 |
Correct |
93 ms |
16432 KB |
Output is correct |
22 |
Correct |
94 ms |
15916 KB |
Output is correct |
23 |
Correct |
95 ms |
15772 KB |
Output is correct |
24 |
Correct |
92 ms |
15684 KB |
Output is correct |
25 |
Correct |
93 ms |
15652 KB |
Output is correct |
26 |
Correct |
91 ms |
15604 KB |
Output is correct |
27 |
Correct |
168 ms |
22468 KB |
Output is correct |
28 |
Correct |
224 ms |
23976 KB |
Output is correct |
29 |
Correct |
159 ms |
22604 KB |
Output is correct |
30 |
Correct |
163 ms |
22304 KB |
Output is correct |
31 |
Correct |
164 ms |
22348 KB |
Output is correct |
32 |
Correct |
150 ms |
22988 KB |
Output is correct |
33 |
Correct |
145 ms |
29268 KB |
Output is correct |
34 |
Correct |
141 ms |
34124 KB |
Output is correct |
35 |
Correct |
146 ms |
30188 KB |
Output is correct |
36 |
Correct |
146 ms |
26616 KB |
Output is correct |
37 |
Correct |
141 ms |
22844 KB |
Output is correct |
38 |
Correct |
314 ms |
35596 KB |
Output is correct |
39 |
Correct |
320 ms |
40156 KB |
Output is correct |
40 |
Correct |
268 ms |
31440 KB |
Output is correct |
41 |
Correct |
294 ms |
47336 KB |
Output is correct |
42 |
Correct |
155 ms |
20484 KB |
Output is correct |
43 |
Correct |
158 ms |
21008 KB |
Output is correct |
44 |
Correct |
242 ms |
25900 KB |
Output is correct |
45 |
Correct |
223 ms |
25788 KB |
Output is correct |
46 |
Correct |
230 ms |
25716 KB |
Output is correct |
47 |
Correct |
151 ms |
27872 KB |
Output is correct |
48 |
Correct |
144 ms |
22368 KB |
Output is correct |
49 |
Correct |
208 ms |
28404 KB |
Output is correct |
50 |
Correct |
217 ms |
28484 KB |
Output is correct |
51 |
Correct |
244 ms |
36032 KB |
Output is correct |
52 |
Correct |
184 ms |
26504 KB |
Output is correct |
53 |
Correct |
212 ms |
37060 KB |
Output is correct |
54 |
Correct |
251 ms |
35856 KB |
Output is correct |
55 |
Correct |
201 ms |
29680 KB |
Output is correct |