#include<bits/stdc++.h>
using namespace std;
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC
const int N = 1 << 20;
int last[N];
int l[N << 2], r[N << 2];
void upd(int v, int tl, int tr, int ql, int qr) {
if (tr < ql || qr < tl) {
return;
}
if (ql <= tl && tr <= qr) {
l[v] = max(l[v], ql);
r[v] = min(r[v], qr);
return;
}
int tm = (tl + tr) >> 1;
upd(v * 2 + 1, tl, tm, ql, qr);
upd(v * 2 + 2, tm + 1, tr, ql, qr);
}
void write(int v, int tl, int tr, vector <int> &al, vector <int> &ar) {
if (tl == tr) {
al[tl] = l[v];
ar[tl] = r[v];
return;
}
int tm = (tl + tr) >> 1;
l[v * 2 + 1] = max(l[v * 2 + 1], l[v]);
l[v * 2 + 2] = max(l[v * 2 + 2], l[v]);
r[v * 2 + 1] = min(r[v * 2 + 1], r[v]);
r[v * 2 + 2] = min(r[v * 2 + 2], r[v]);
write(v * 2 + 1, tl, tm, al, ar);
write(v * 2 + 2, tm + 1, tr, al, ar);
}
void build(int v, int tl, int tr, vector <int> &a, int tree[N << 2], bool (*comp)(int , int)) {
if (tl == tr) {
tree[v] = a[tl];
return;
}
int tm = (tl + tr) >> 1;
build(v * 2 + 1, tl, tm, a, tree, comp);
build(v * 2 + 2, tm + 1, tr, a, tree, comp);
if (comp(tree[v * 2 + 1], tree[v * 2 + 2]))
tree[v] = tree[v * 2 + 1];
else
tree[v] = tree[v * 2 + 2];
}
int getl(int v, int tl, int tr, int l, int r, int x, int tree[N << 2], bool (*comp)(int, int)) {
if (comp(x, tree[v]) || r < tl || tr < l)
return -1;
if (tl == tr)
return tl;
int tm = (tl + tr) >> 1;
int t = getl(v * 2 + 1, tl, tm, l, r, x, tree, comp);
if (t != -1)
return t;
else
return getl(v * 2 + 2, tm + 1, tr, l, r, x, tree, comp);
}
int getr(int v, int tl, int tr, int l, int r, int x, int tree[N << 2], bool (*comp)(int, int)) {
if (comp(x, tree[v]) || r < tl || tr < l)
return -1;
if (tl == tr)
return tl;
int tm = (tl + tr) >> 1;
int t = getr(v * 2 + 2, tm + 1, tr, l, r, x, tree, comp);
if (t != -1)
return t;
else
return getr(v * 2 + 1, tl, tm, l, r, x, tree, comp);
}
bool ls(int a, int b) { return a < b; }
bool bg(int a, int b) { return a > b; }
int mn[N << 2], mx[N << 2];
signed main() {
#ifdef HOME
freopen("input.txt", "r", stdin);
#else
#define endl '\n'
ios_base::sync_with_stdio(0); cin.tie(0);
#endif
int n;
cin >> n;
for (int i = 0; i < (N << 2); ++i)
r[i] = n - 1;
vector <int> c(n-1);
for (int i = 0; i < n - 1; ++i)
cin >> c[i];
vector <vector <int> > a(n);
for (int i = 0; i < n; ++i) {
int k;
cin >> k;
a[i].resize(k);
for (int j = 0; j < k; ++j)
cin >> a[i][j];
}
vector <int> l(n), r(n);
for (int i = 0; i < n; ++i) {
l[i] = 0;
r[i] = n - 1;
}
vector <int> linkl(n), linkr(n);
for (int i = 0; i < N; ++i)
last[i] = -1;
for (int i = 0; i < n - 1; ++i) {
for (auto e : a[i])
last[e] = i;
linkl[i] = last[c[i]]+1;
}
for (int i = 0; i < N; ++i)
last[i] = n;
for (int i = n - 1; i >= 0; --i) {
if (i < n - 1) {
linkr[i] = last[c[i]]-1;
}
for (auto e : a[i])
last[e] = i;
}
build(0, 0, n - 1, linkl, mn, ls);
for (int i = 0; i < n; ++i) {
int j = getr(0, 0, n - 1, i, linkr[i], i+1, mn, ls);
if (j != -1) {
upd(0, 0, n - 1, i+1, j);
}
}
build(0, 0, n - 1, linkr, mx, bg);
for (int j = 0; j < n; ++j) {
int i = getl(0, 0, n - 1, linkl[j]-1, j, j, mx, bg);
if (i != -1) {
upd(0, 0, n - 1, i+1, j);
}
}
for (int i = 0; i < n; ++i) {
if (linkr[i] == n - 1) {
upd(0, 0, n - 1, i+1, n - 1);
}
}
for (int i = 0; i < n; ++i) {
if (linkl[i] == 0) {
upd(0, 0, n - 1, 0, i);
}
}
write(0, 0, n - 1, l, r);
int q;
cin >> q;
while (q--) {
int i, j;
cin >> i >> j;
--i; --j;
if (l[i] <= j && j <= r[i]) {
cout << "YES" << endl;
}
else {
cout << "NO" << endl;
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
21248 KB |
Output is correct |
2 |
Correct |
21 ms |
21248 KB |
Output is correct |
3 |
Correct |
21 ms |
21632 KB |
Output is correct |
4 |
Correct |
21 ms |
21120 KB |
Output is correct |
5 |
Correct |
21 ms |
21248 KB |
Output is correct |
6 |
Correct |
20 ms |
21248 KB |
Output is correct |
7 |
Correct |
20 ms |
21120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
21248 KB |
Output is correct |
2 |
Correct |
21 ms |
21248 KB |
Output is correct |
3 |
Correct |
21 ms |
21632 KB |
Output is correct |
4 |
Correct |
21 ms |
21120 KB |
Output is correct |
5 |
Correct |
21 ms |
21248 KB |
Output is correct |
6 |
Correct |
20 ms |
21248 KB |
Output is correct |
7 |
Correct |
20 ms |
21120 KB |
Output is correct |
8 |
Correct |
145 ms |
23856 KB |
Output is correct |
9 |
Correct |
141 ms |
23800 KB |
Output is correct |
10 |
Correct |
143 ms |
24056 KB |
Output is correct |
11 |
Correct |
138 ms |
24440 KB |
Output is correct |
12 |
Correct |
128 ms |
23928 KB |
Output is correct |
13 |
Correct |
145 ms |
23928 KB |
Output is correct |
14 |
Correct |
144 ms |
23928 KB |
Output is correct |
15 |
Correct |
138 ms |
24056 KB |
Output is correct |
16 |
Correct |
136 ms |
24312 KB |
Output is correct |
17 |
Correct |
140 ms |
23928 KB |
Output is correct |
18 |
Correct |
133 ms |
24056 KB |
Output is correct |
19 |
Correct |
139 ms |
24056 KB |
Output is correct |
20 |
Correct |
132 ms |
24184 KB |
Output is correct |
21 |
Correct |
135 ms |
24312 KB |
Output is correct |
22 |
Correct |
145 ms |
24056 KB |
Output is correct |
23 |
Correct |
144 ms |
23800 KB |
Output is correct |
24 |
Correct |
141 ms |
24020 KB |
Output is correct |
25 |
Correct |
139 ms |
23900 KB |
Output is correct |
26 |
Correct |
145 ms |
23800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
287 ms |
34424 KB |
Output is correct |
2 |
Correct |
283 ms |
34280 KB |
Output is correct |
3 |
Correct |
282 ms |
34424 KB |
Output is correct |
4 |
Correct |
284 ms |
34296 KB |
Output is correct |
5 |
Correct |
288 ms |
34296 KB |
Output is correct |
6 |
Correct |
258 ms |
34424 KB |
Output is correct |
7 |
Correct |
246 ms |
34756 KB |
Output is correct |
8 |
Correct |
250 ms |
34552 KB |
Output is correct |
9 |
Correct |
259 ms |
34680 KB |
Output is correct |
10 |
Correct |
246 ms |
34552 KB |
Output is correct |
11 |
Correct |
253 ms |
34552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
21248 KB |
Output is correct |
2 |
Correct |
21 ms |
21248 KB |
Output is correct |
3 |
Correct |
21 ms |
21632 KB |
Output is correct |
4 |
Correct |
21 ms |
21120 KB |
Output is correct |
5 |
Correct |
21 ms |
21248 KB |
Output is correct |
6 |
Correct |
20 ms |
21248 KB |
Output is correct |
7 |
Correct |
20 ms |
21120 KB |
Output is correct |
8 |
Correct |
145 ms |
23856 KB |
Output is correct |
9 |
Correct |
141 ms |
23800 KB |
Output is correct |
10 |
Correct |
143 ms |
24056 KB |
Output is correct |
11 |
Correct |
138 ms |
24440 KB |
Output is correct |
12 |
Correct |
128 ms |
23928 KB |
Output is correct |
13 |
Correct |
145 ms |
23928 KB |
Output is correct |
14 |
Correct |
144 ms |
23928 KB |
Output is correct |
15 |
Correct |
138 ms |
24056 KB |
Output is correct |
16 |
Correct |
136 ms |
24312 KB |
Output is correct |
17 |
Correct |
140 ms |
23928 KB |
Output is correct |
18 |
Correct |
133 ms |
24056 KB |
Output is correct |
19 |
Correct |
139 ms |
24056 KB |
Output is correct |
20 |
Correct |
132 ms |
24184 KB |
Output is correct |
21 |
Correct |
135 ms |
24312 KB |
Output is correct |
22 |
Correct |
145 ms |
24056 KB |
Output is correct |
23 |
Correct |
144 ms |
23800 KB |
Output is correct |
24 |
Correct |
141 ms |
24020 KB |
Output is correct |
25 |
Correct |
139 ms |
23900 KB |
Output is correct |
26 |
Correct |
145 ms |
23800 KB |
Output is correct |
27 |
Correct |
287 ms |
34424 KB |
Output is correct |
28 |
Correct |
283 ms |
34280 KB |
Output is correct |
29 |
Correct |
282 ms |
34424 KB |
Output is correct |
30 |
Correct |
284 ms |
34296 KB |
Output is correct |
31 |
Correct |
288 ms |
34296 KB |
Output is correct |
32 |
Correct |
258 ms |
34424 KB |
Output is correct |
33 |
Correct |
246 ms |
34756 KB |
Output is correct |
34 |
Correct |
250 ms |
34552 KB |
Output is correct |
35 |
Correct |
259 ms |
34680 KB |
Output is correct |
36 |
Correct |
246 ms |
34552 KB |
Output is correct |
37 |
Correct |
253 ms |
34552 KB |
Output is correct |
38 |
Correct |
766 ms |
65548 KB |
Output is correct |
39 |
Correct |
827 ms |
72756 KB |
Output is correct |
40 |
Correct |
578 ms |
57848 KB |
Output is correct |
41 |
Correct |
640 ms |
73080 KB |
Output is correct |
42 |
Correct |
271 ms |
33912 KB |
Output is correct |
43 |
Correct |
285 ms |
33912 KB |
Output is correct |
44 |
Correct |
395 ms |
44604 KB |
Output is correct |
45 |
Correct |
405 ms |
44440 KB |
Output is correct |
46 |
Correct |
394 ms |
44536 KB |
Output is correct |
47 |
Correct |
261 ms |
34040 KB |
Output is correct |
48 |
Correct |
258 ms |
34040 KB |
Output is correct |
49 |
Correct |
385 ms |
44500 KB |
Output is correct |
50 |
Correct |
403 ms |
44536 KB |
Output is correct |
51 |
Correct |
396 ms |
44536 KB |
Output is correct |
52 |
Correct |
451 ms |
50424 KB |
Output is correct |
53 |
Correct |
601 ms |
67960 KB |
Output is correct |
54 |
Correct |
782 ms |
77048 KB |
Output is correct |
55 |
Correct |
599 ms |
68088 KB |
Output is correct |