#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 5e5+5;
int n, Q, c[maxn], last[maxn], le[maxn], ri[maxn], st[maxn], en[maxn];
vector<int> q[maxn];
bool cleft(int i) {
return st[i] > 1 && ri[st[i]-1] != -1 && ri[st[i]-1] <= en[i];
}
bool cright(int i) {
return en[i] < n && le[en[i]] != -1 && st[i] <= le[en[i]];
}
int main()
{
cin >> n;
for (int i = 1; i < n; i++) cin >> c[i];
for (int i = 1; i <= n; i++) {
int x, y; cin >> x;
while (x--) {
cin >> y;
q[i].push_back(y);
}
}
memset(last,-1,sizeof(last));
for (int i = 1; i < n; i++) {
for (int j: q[i]) {
last[j] = i;
}
le[i] = last[c[i]];
}
memset(last,-1,sizeof(last));
for (int i = n - 1; i >= 1; i--) {
for (int j: q[i+1]) {
last[j] = i + 1;
}
ri[i] = last[c[i]];
}
for (int i = 1; i <= n; i++) {
st[i] = en[i] = i;
while (true) {
if (cleft(i)) {
en[i] = max(en[i],en[st[i]-1]);
st[i] = min(st[i],st[st[i]-1]);
}
else if (cright(i)) ++en[i];
else break;
}
}
cin >> Q;
while (Q--) {
int a, b; cin >> a >> b;
cout << (st[a] <= b && b <= en[a] ? "YES" : "NO") << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
14200 KB |
Output is correct |
2 |
Correct |
30 ms |
14208 KB |
Output is correct |
3 |
Correct |
35 ms |
14328 KB |
Output is correct |
4 |
Correct |
35 ms |
14208 KB |
Output is correct |
5 |
Correct |
26 ms |
14208 KB |
Output is correct |
6 |
Correct |
29 ms |
14336 KB |
Output is correct |
7 |
Correct |
27 ms |
14208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
14200 KB |
Output is correct |
2 |
Correct |
30 ms |
14208 KB |
Output is correct |
3 |
Correct |
35 ms |
14328 KB |
Output is correct |
4 |
Correct |
35 ms |
14208 KB |
Output is correct |
5 |
Correct |
26 ms |
14208 KB |
Output is correct |
6 |
Correct |
29 ms |
14336 KB |
Output is correct |
7 |
Correct |
27 ms |
14208 KB |
Output is correct |
8 |
Correct |
1071 ms |
16548 KB |
Output is correct |
9 |
Correct |
1066 ms |
16532 KB |
Output is correct |
10 |
Correct |
1143 ms |
16676 KB |
Output is correct |
11 |
Correct |
1022 ms |
16868 KB |
Output is correct |
12 |
Correct |
944 ms |
16740 KB |
Output is correct |
13 |
Correct |
1031 ms |
16692 KB |
Output is correct |
14 |
Correct |
1012 ms |
16680 KB |
Output is correct |
15 |
Correct |
1044 ms |
16820 KB |
Output is correct |
16 |
Correct |
953 ms |
16928 KB |
Output is correct |
17 |
Correct |
1016 ms |
16656 KB |
Output is correct |
18 |
Correct |
1025 ms |
16888 KB |
Output is correct |
19 |
Correct |
1117 ms |
16656 KB |
Output is correct |
20 |
Correct |
1086 ms |
16876 KB |
Output is correct |
21 |
Correct |
992 ms |
16956 KB |
Output is correct |
22 |
Correct |
989 ms |
16556 KB |
Output is correct |
23 |
Correct |
953 ms |
16504 KB |
Output is correct |
24 |
Correct |
1072 ms |
16536 KB |
Output is correct |
25 |
Correct |
996 ms |
16632 KB |
Output is correct |
26 |
Correct |
1067 ms |
16628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1447 ms |
22276 KB |
Output is correct |
2 |
Correct |
1399 ms |
28884 KB |
Output is correct |
3 |
Correct |
1297 ms |
28396 KB |
Output is correct |
4 |
Correct |
1499 ms |
28720 KB |
Output is correct |
5 |
Correct |
1501 ms |
28784 KB |
Output is correct |
6 |
Correct |
1220 ms |
27444 KB |
Output is correct |
7 |
Correct |
1114 ms |
27356 KB |
Output is correct |
8 |
Correct |
1225 ms |
27256 KB |
Output is correct |
9 |
Correct |
1216 ms |
27460 KB |
Output is correct |
10 |
Correct |
1387 ms |
27224 KB |
Output is correct |
11 |
Correct |
1208 ms |
27288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
14200 KB |
Output is correct |
2 |
Correct |
30 ms |
14208 KB |
Output is correct |
3 |
Correct |
35 ms |
14328 KB |
Output is correct |
4 |
Correct |
35 ms |
14208 KB |
Output is correct |
5 |
Correct |
26 ms |
14208 KB |
Output is correct |
6 |
Correct |
29 ms |
14336 KB |
Output is correct |
7 |
Correct |
27 ms |
14208 KB |
Output is correct |
8 |
Correct |
1071 ms |
16548 KB |
Output is correct |
9 |
Correct |
1066 ms |
16532 KB |
Output is correct |
10 |
Correct |
1143 ms |
16676 KB |
Output is correct |
11 |
Correct |
1022 ms |
16868 KB |
Output is correct |
12 |
Correct |
944 ms |
16740 KB |
Output is correct |
13 |
Correct |
1031 ms |
16692 KB |
Output is correct |
14 |
Correct |
1012 ms |
16680 KB |
Output is correct |
15 |
Correct |
1044 ms |
16820 KB |
Output is correct |
16 |
Correct |
953 ms |
16928 KB |
Output is correct |
17 |
Correct |
1016 ms |
16656 KB |
Output is correct |
18 |
Correct |
1025 ms |
16888 KB |
Output is correct |
19 |
Correct |
1117 ms |
16656 KB |
Output is correct |
20 |
Correct |
1086 ms |
16876 KB |
Output is correct |
21 |
Correct |
992 ms |
16956 KB |
Output is correct |
22 |
Correct |
989 ms |
16556 KB |
Output is correct |
23 |
Correct |
953 ms |
16504 KB |
Output is correct |
24 |
Correct |
1072 ms |
16536 KB |
Output is correct |
25 |
Correct |
996 ms |
16632 KB |
Output is correct |
26 |
Correct |
1067 ms |
16628 KB |
Output is correct |
27 |
Correct |
1447 ms |
22276 KB |
Output is correct |
28 |
Correct |
1399 ms |
28884 KB |
Output is correct |
29 |
Correct |
1297 ms |
28396 KB |
Output is correct |
30 |
Correct |
1499 ms |
28720 KB |
Output is correct |
31 |
Correct |
1501 ms |
28784 KB |
Output is correct |
32 |
Correct |
1220 ms |
27444 KB |
Output is correct |
33 |
Correct |
1114 ms |
27356 KB |
Output is correct |
34 |
Correct |
1225 ms |
27256 KB |
Output is correct |
35 |
Correct |
1216 ms |
27460 KB |
Output is correct |
36 |
Correct |
1387 ms |
27224 KB |
Output is correct |
37 |
Correct |
1208 ms |
27288 KB |
Output is correct |
38 |
Correct |
1594 ms |
47752 KB |
Output is correct |
39 |
Correct |
1728 ms |
52588 KB |
Output is correct |
40 |
Correct |
1500 ms |
41348 KB |
Output is correct |
41 |
Correct |
1598 ms |
51028 KB |
Output is correct |
42 |
Correct |
1174 ms |
28376 KB |
Output is correct |
43 |
Correct |
1317 ms |
28484 KB |
Output is correct |
44 |
Correct |
1426 ms |
36140 KB |
Output is correct |
45 |
Correct |
1337 ms |
36172 KB |
Output is correct |
46 |
Correct |
1396 ms |
36472 KB |
Output is correct |
47 |
Correct |
1263 ms |
28460 KB |
Output is correct |
48 |
Correct |
1273 ms |
28280 KB |
Output is correct |
49 |
Correct |
1397 ms |
36196 KB |
Output is correct |
50 |
Correct |
1470 ms |
36312 KB |
Output is correct |
51 |
Correct |
1565 ms |
36576 KB |
Output is correct |
52 |
Correct |
1419 ms |
35080 KB |
Output is correct |
53 |
Correct |
1677 ms |
41920 KB |
Output is correct |
54 |
Correct |
1607 ms |
48428 KB |
Output is correct |
55 |
Correct |
1505 ms |
42156 KB |
Output is correct |