#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <iomanip>
#include <functional>
#include <cassert>
using namespace std;
typedef long long ll;
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
int n;
cin >> n;
vector <int> c(n - 1);
for (int i = 0; i < n - 1; ++i) {
cin >> c[i];
--c[i];
}
vector <set <int>> who(n);
vector <set <int>> where(n);
for (int i = 0; i < n; ++i) {
int k;
cin >> k;
for (int j = 0; j < k; ++j) {
int x;
cin >> x;
--x;
who[i].insert(x);
where[x].insert(i);
}
}
auto have = [&] (int l, int r, int x) {
auto it = where[x].lower_bound(l);
return (it != where[x].end() && (*it) <= r);
};
vector <pair <int, int>> go(n);
auto adjust = [&] (int l, int r) {
while (true) {
if (l > 0 && have(l, r, c[l - 1])) {
--l;
int tl = l;
l = min(l, go[tl].first);
r = max(r, go[tl].second);
} else if (r + 1 < n && have(l, r, c[r])) {
++r;
} else {
break;
}
}
return make_pair(l, r);
};
vector <int> goR(n, -1);
goR[n - 1] = n - 1;
for (int i = n - 2; i >= 0; --i) {
goR[i] = i;
while (goR[i] + 1 < n && have(i, goR[i], c[goR[i]])) {
++goR[i];
goR[i] = max(goR[i], goR[goR[i]]);
}
}
assert((double)clock() / CLOCKS_PER_SEC < 1);
go[0] = {0, goR[0]};
for (int i = 1; i < n; ++i) {
int j = goR[i];
int need = c[i - 1];
if (have(i, j, need)) {
go[i] = go[i - 1];
go[i].second = max(go[i].second, goR[i]);
go[i] = adjust(go[i].first, go[i].second);
} else {
go[i] = {i, goR[i]};
}
}
int q;
cin >> q;
while (q--) {
int x, y;
cin >> x >> y;
--x, --y;
if (go[x].first <= y && y <= go[x].second) {
cout << "YES\n";
} else {
cout << "NO\n";
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
1024 KB |
Output is correct |
2 |
Correct |
4 ms |
1152 KB |
Output is correct |
3 |
Correct |
5 ms |
1408 KB |
Output is correct |
4 |
Correct |
3 ms |
896 KB |
Output is correct |
5 |
Correct |
3 ms |
896 KB |
Output is correct |
6 |
Correct |
5 ms |
896 KB |
Output is correct |
7 |
Correct |
4 ms |
768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
1024 KB |
Output is correct |
2 |
Correct |
4 ms |
1152 KB |
Output is correct |
3 |
Correct |
5 ms |
1408 KB |
Output is correct |
4 |
Correct |
3 ms |
896 KB |
Output is correct |
5 |
Correct |
3 ms |
896 KB |
Output is correct |
6 |
Correct |
5 ms |
896 KB |
Output is correct |
7 |
Correct |
4 ms |
768 KB |
Output is correct |
8 |
Correct |
123 ms |
2552 KB |
Output is correct |
9 |
Correct |
113 ms |
2552 KB |
Output is correct |
10 |
Correct |
122 ms |
2808 KB |
Output is correct |
11 |
Correct |
118 ms |
3168 KB |
Output is correct |
12 |
Correct |
106 ms |
2552 KB |
Output is correct |
13 |
Correct |
110 ms |
2808 KB |
Output is correct |
14 |
Correct |
109 ms |
2680 KB |
Output is correct |
15 |
Correct |
107 ms |
2680 KB |
Output is correct |
16 |
Correct |
100 ms |
2936 KB |
Output is correct |
17 |
Correct |
104 ms |
2680 KB |
Output is correct |
18 |
Correct |
104 ms |
2680 KB |
Output is correct |
19 |
Correct |
104 ms |
2680 KB |
Output is correct |
20 |
Correct |
103 ms |
2808 KB |
Output is correct |
21 |
Correct |
109 ms |
2936 KB |
Output is correct |
22 |
Correct |
105 ms |
2808 KB |
Output is correct |
23 |
Correct |
117 ms |
2424 KB |
Output is correct |
24 |
Correct |
116 ms |
2424 KB |
Output is correct |
25 |
Correct |
111 ms |
2552 KB |
Output is correct |
26 |
Correct |
112 ms |
2296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
457 ms |
60372 KB |
Output is correct |
2 |
Correct |
423 ms |
60280 KB |
Output is correct |
3 |
Correct |
388 ms |
57336 KB |
Output is correct |
4 |
Correct |
466 ms |
61304 KB |
Output is correct |
5 |
Correct |
497 ms |
61176 KB |
Output is correct |
6 |
Correct |
278 ms |
36984 KB |
Output is correct |
7 |
Correct |
269 ms |
37368 KB |
Output is correct |
8 |
Correct |
270 ms |
37496 KB |
Output is correct |
9 |
Correct |
273 ms |
37500 KB |
Output is correct |
10 |
Correct |
254 ms |
37752 KB |
Output is correct |
11 |
Correct |
255 ms |
37624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
1024 KB |
Output is correct |
2 |
Correct |
4 ms |
1152 KB |
Output is correct |
3 |
Correct |
5 ms |
1408 KB |
Output is correct |
4 |
Correct |
3 ms |
896 KB |
Output is correct |
5 |
Correct |
3 ms |
896 KB |
Output is correct |
6 |
Correct |
5 ms |
896 KB |
Output is correct |
7 |
Correct |
4 ms |
768 KB |
Output is correct |
8 |
Correct |
123 ms |
2552 KB |
Output is correct |
9 |
Correct |
113 ms |
2552 KB |
Output is correct |
10 |
Correct |
122 ms |
2808 KB |
Output is correct |
11 |
Correct |
118 ms |
3168 KB |
Output is correct |
12 |
Correct |
106 ms |
2552 KB |
Output is correct |
13 |
Correct |
110 ms |
2808 KB |
Output is correct |
14 |
Correct |
109 ms |
2680 KB |
Output is correct |
15 |
Correct |
107 ms |
2680 KB |
Output is correct |
16 |
Correct |
100 ms |
2936 KB |
Output is correct |
17 |
Correct |
104 ms |
2680 KB |
Output is correct |
18 |
Correct |
104 ms |
2680 KB |
Output is correct |
19 |
Correct |
104 ms |
2680 KB |
Output is correct |
20 |
Correct |
103 ms |
2808 KB |
Output is correct |
21 |
Correct |
109 ms |
2936 KB |
Output is correct |
22 |
Correct |
105 ms |
2808 KB |
Output is correct |
23 |
Correct |
117 ms |
2424 KB |
Output is correct |
24 |
Correct |
116 ms |
2424 KB |
Output is correct |
25 |
Correct |
111 ms |
2552 KB |
Output is correct |
26 |
Correct |
112 ms |
2296 KB |
Output is correct |
27 |
Correct |
457 ms |
60372 KB |
Output is correct |
28 |
Correct |
423 ms |
60280 KB |
Output is correct |
29 |
Correct |
388 ms |
57336 KB |
Output is correct |
30 |
Correct |
466 ms |
61304 KB |
Output is correct |
31 |
Correct |
497 ms |
61176 KB |
Output is correct |
32 |
Correct |
278 ms |
36984 KB |
Output is correct |
33 |
Correct |
269 ms |
37368 KB |
Output is correct |
34 |
Correct |
270 ms |
37496 KB |
Output is correct |
35 |
Correct |
273 ms |
37500 KB |
Output is correct |
36 |
Correct |
254 ms |
37752 KB |
Output is correct |
37 |
Correct |
255 ms |
37624 KB |
Output is correct |
38 |
Correct |
947 ms |
104308 KB |
Output is correct |
39 |
Correct |
881 ms |
115364 KB |
Output is correct |
40 |
Correct |
753 ms |
92280 KB |
Output is correct |
41 |
Correct |
538 ms |
113660 KB |
Output is correct |
42 |
Correct |
248 ms |
35908 KB |
Output is correct |
43 |
Correct |
335 ms |
38136 KB |
Output is correct |
44 |
Correct |
481 ms |
70264 KB |
Output is correct |
45 |
Correct |
427 ms |
70776 KB |
Output is correct |
46 |
Correct |
421 ms |
71624 KB |
Output is correct |
47 |
Correct |
267 ms |
39416 KB |
Output is correct |
48 |
Correct |
317 ms |
39196 KB |
Output is correct |
49 |
Correct |
519 ms |
71672 KB |
Output is correct |
50 |
Correct |
477 ms |
71800 KB |
Output is correct |
51 |
Correct |
419 ms |
72184 KB |
Output is correct |
52 |
Correct |
269 ms |
55544 KB |
Output is correct |
53 |
Correct |
367 ms |
81528 KB |
Output is correct |
54 |
Correct |
381 ms |
99576 KB |
Output is correct |
55 |
Correct |
348 ms |
77816 KB |
Output is correct |