#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <deque>
#include <map>
#include <set>
#include <complex>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <chrono>
#define ft first
#define sc second
#define pb push_back
#define len(v) (int)v.size()
// #define int ll
using namespace std;
typedef long long ll;
typedef long double ld;
vector<int> tree_l;
vector<int> tree_r;
int N = 1;
int inf = 1e9;
void upd_l(int v) {
if(v == 0)
return;
tree_l[v] = min(tree_l[v * 2], tree_l[v * 2 + 1]);
upd_l(v / 2);
}
void upd_r(int v) {
if(v == 0)
return;
tree_r[v] = max(tree_r[v * 2], tree_r[v * 2 + 1]);
upd_r(v / 2);
}
int get_l(int v, int l, int r, int vl, int vr) {
if(vr <= l || r <= vl)
return inf;
if(vl <= l && r <= vr)
return tree_l[v];
int m = (l + r) / 2;
return min(get_l(v * 2, l, m, vl, vr), get_l(v * 2 + 1, m, r, vl, vr));
}
int get_r(int v, int l, int r, int vl, int vr) {
if(vr <= l || r <= vl)
return -inf;
if(vl <= l && r <= vr)
return tree_r[v];
int m = (l + r) / 2;
return max(get_r(v * 2, l, m, vl, vr), get_r(v * 2 + 1, m, r, vl, vr));
}
signed main() {
#ifdef PC
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
while(N < (n + 1))
N *= 2;
tree_l = vector<int> (2 * N, inf);
tree_r = vector<int> (2 * N, -inf);
vector<int> per(n);
vector<vector<int>> keys(n);
vector<vector<int>> types(n);
vector<int> l(n);
for (int i = 1; i < n; i++) {
cin >> per[i];
per[i]--;
}
for (int i = 0; i < n; i++) {
int k;
cin >> k;
for (int j = 0; j < k; j++) {
int x;
cin >> x;
x--;
types[x].pb(i);
keys[i].pb(x);
}
}
//calc v<>l
for (int i = 0; i < n; i++) {
l[i] = i;
int ln = i;
while(1) {
bool flag = 1;
ln = min(ln, get_l(1, 0, N, ln, i + 1));
if(ln > 0) {
auto uk = lower_bound(types[per[ln]].begin(), types[per[ln]].end(), ln);
if(uk != types[per[ln]].end() && (*uk <= i)) {
ln--;
flag = 0;
}
}
if(flag)
break;
}
l[i] = ln;
tree_l[N + i] = l[i];
upd_l((N + i) / 2);
}
// calc ans
vector<pair<int, int>> ans(n);
for (int i = n - 1; i >= 0; i--) {
int ln = i, rn = i + 1;
while(1) {
bool flag = 1;
ln = min(ln, get_l(1, 0, N, ln, rn));
rn = max(rn, get_r(1, 0, N, ln, rn));
if(ln > 0) {
auto uk = lower_bound(types[per[ln]].begin(), types[per[ln]].end(), ln);
if(uk != types[per[ln]].end() && (*uk < rn)) {
ln--;
flag = 0;
}
}
if(rn < n) {
auto uk = lower_bound(types[per[rn]].begin(), types[per[rn]].end(), rn);
if(uk != types[per[rn]].begin()) {
uk--;
if(*uk >= ln) {
rn++;
flag = 0;
}
}
}
if(flag)
break;
}
ans[i] = {ln, rn - 1};
tree_l[N + i] = ans[i].ft;
tree_r[N + i] = ans[i].sc + 1;
upd_l((N + i) / 2);
upd_l((N + i) / 2);
// cout << i << " " << ans[i].ft << " " << ans[i].sc << endl;
}
int q;
cin >> q;
for (int i = 0; i < q; i++) {
int a, b;
cin >> a >> b;
a--, b--;
if(ans[a].ft <= b && b <= ans[a].sc)
cout << "YES" << '\n';
else
cout << "NO" << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
632 KB |
Output is correct |
2 |
Correct |
11 ms |
888 KB |
Output is correct |
3 |
Correct |
14 ms |
1016 KB |
Output is correct |
4 |
Correct |
9 ms |
632 KB |
Output is correct |
5 |
Correct |
9 ms |
632 KB |
Output is correct |
6 |
Correct |
10 ms |
760 KB |
Output is correct |
7 |
Correct |
10 ms |
888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
632 KB |
Output is correct |
2 |
Correct |
11 ms |
888 KB |
Output is correct |
3 |
Correct |
14 ms |
1016 KB |
Output is correct |
4 |
Correct |
9 ms |
632 KB |
Output is correct |
5 |
Correct |
9 ms |
632 KB |
Output is correct |
6 |
Correct |
10 ms |
760 KB |
Output is correct |
7 |
Correct |
10 ms |
888 KB |
Output is correct |
8 |
Correct |
131 ms |
3960 KB |
Output is correct |
9 |
Correct |
136 ms |
3960 KB |
Output is correct |
10 |
Correct |
145 ms |
4296 KB |
Output is correct |
11 |
Correct |
132 ms |
4600 KB |
Output is correct |
12 |
Correct |
118 ms |
3960 KB |
Output is correct |
13 |
Correct |
126 ms |
4216 KB |
Output is correct |
14 |
Correct |
128 ms |
4088 KB |
Output is correct |
15 |
Correct |
130 ms |
4216 KB |
Output is correct |
16 |
Correct |
127 ms |
4600 KB |
Output is correct |
17 |
Correct |
126 ms |
4088 KB |
Output is correct |
18 |
Correct |
135 ms |
4088 KB |
Output is correct |
19 |
Correct |
127 ms |
4344 KB |
Output is correct |
20 |
Correct |
123 ms |
4344 KB |
Output is correct |
21 |
Correct |
125 ms |
4344 KB |
Output is correct |
22 |
Correct |
131 ms |
4216 KB |
Output is correct |
23 |
Correct |
128 ms |
3960 KB |
Output is correct |
24 |
Correct |
135 ms |
3960 KB |
Output is correct |
25 |
Correct |
128 ms |
4088 KB |
Output is correct |
26 |
Correct |
134 ms |
3960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
410 ms |
18148 KB |
Output is correct |
2 |
Correct |
404 ms |
16888 KB |
Output is correct |
3 |
Correct |
388 ms |
16604 KB |
Output is correct |
4 |
Correct |
415 ms |
17144 KB |
Output is correct |
5 |
Correct |
401 ms |
17016 KB |
Output is correct |
6 |
Correct |
343 ms |
15608 KB |
Output is correct |
7 |
Correct |
336 ms |
15736 KB |
Output is correct |
8 |
Correct |
318 ms |
15864 KB |
Output is correct |
9 |
Correct |
324 ms |
15736 KB |
Output is correct |
10 |
Correct |
325 ms |
15764 KB |
Output is correct |
11 |
Correct |
320 ms |
15868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
632 KB |
Output is correct |
2 |
Correct |
11 ms |
888 KB |
Output is correct |
3 |
Correct |
14 ms |
1016 KB |
Output is correct |
4 |
Correct |
9 ms |
632 KB |
Output is correct |
5 |
Correct |
9 ms |
632 KB |
Output is correct |
6 |
Correct |
10 ms |
760 KB |
Output is correct |
7 |
Correct |
10 ms |
888 KB |
Output is correct |
8 |
Correct |
131 ms |
3960 KB |
Output is correct |
9 |
Correct |
136 ms |
3960 KB |
Output is correct |
10 |
Correct |
145 ms |
4296 KB |
Output is correct |
11 |
Correct |
132 ms |
4600 KB |
Output is correct |
12 |
Correct |
118 ms |
3960 KB |
Output is correct |
13 |
Correct |
126 ms |
4216 KB |
Output is correct |
14 |
Correct |
128 ms |
4088 KB |
Output is correct |
15 |
Correct |
130 ms |
4216 KB |
Output is correct |
16 |
Correct |
127 ms |
4600 KB |
Output is correct |
17 |
Correct |
126 ms |
4088 KB |
Output is correct |
18 |
Correct |
135 ms |
4088 KB |
Output is correct |
19 |
Correct |
127 ms |
4344 KB |
Output is correct |
20 |
Correct |
123 ms |
4344 KB |
Output is correct |
21 |
Correct |
125 ms |
4344 KB |
Output is correct |
22 |
Correct |
131 ms |
4216 KB |
Output is correct |
23 |
Correct |
128 ms |
3960 KB |
Output is correct |
24 |
Correct |
135 ms |
3960 KB |
Output is correct |
25 |
Correct |
128 ms |
4088 KB |
Output is correct |
26 |
Correct |
134 ms |
3960 KB |
Output is correct |
27 |
Correct |
410 ms |
18148 KB |
Output is correct |
28 |
Correct |
404 ms |
16888 KB |
Output is correct |
29 |
Correct |
388 ms |
16604 KB |
Output is correct |
30 |
Correct |
415 ms |
17144 KB |
Output is correct |
31 |
Correct |
401 ms |
17016 KB |
Output is correct |
32 |
Correct |
343 ms |
15608 KB |
Output is correct |
33 |
Correct |
336 ms |
15736 KB |
Output is correct |
34 |
Correct |
318 ms |
15864 KB |
Output is correct |
35 |
Correct |
324 ms |
15736 KB |
Output is correct |
36 |
Correct |
325 ms |
15764 KB |
Output is correct |
37 |
Correct |
320 ms |
15868 KB |
Output is correct |
38 |
Correct |
683 ms |
51832 KB |
Output is correct |
39 |
Correct |
737 ms |
60664 KB |
Output is correct |
40 |
Correct |
565 ms |
42104 KB |
Output is correct |
41 |
Correct |
1155 ms |
60256 KB |
Output is correct |
42 |
Correct |
356 ms |
15864 KB |
Output is correct |
43 |
Correct |
362 ms |
15616 KB |
Output is correct |
44 |
Correct |
661 ms |
29436 KB |
Output is correct |
45 |
Correct |
676 ms |
29432 KB |
Output is correct |
46 |
Correct |
644 ms |
29688 KB |
Output is correct |
47 |
Correct |
385 ms |
16248 KB |
Output is correct |
48 |
Correct |
361 ms |
15736 KB |
Output is correct |
49 |
Correct |
655 ms |
28408 KB |
Output is correct |
50 |
Correct |
673 ms |
28664 KB |
Output is correct |
51 |
Correct |
719 ms |
29812 KB |
Output is correct |
52 |
Correct |
494 ms |
31224 KB |
Output is correct |
53 |
Correct |
660 ms |
47476 KB |
Output is correct |
54 |
Correct |
838 ms |
60408 KB |
Output is correct |
55 |
Correct |
660 ms |
47736 KB |
Output is correct |