#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define ff first
#define ss second
#define pb push_back
const int mxN = 5e5 + 1;
const int MAX = 1e9, MIN = -1e9;
pii seg[(1 << 20)];
pii a[mxN];
int corr[mxN];
vector<int> keys[mxN];
int n;
void build(int l = 1, int r = n, int idx = 1) {
if (l == r) {
seg[idx] = a[l];
return;
}
int mid = (l + r) >> 1;
build(l, mid, (idx << 1));
build(mid + 1, r, (idx << 1) | 1);
seg[idx].ff = min(seg[(idx << 1)].ff, seg[(idx << 1) | 1].ff);
seg[idx].ss = max(seg[(idx << 1)].ss, seg[(idx << 1) | 1].ss);
}
int querymin(int tl, int tr, int l = 1, int r = n, int idx = 1) {
if (tl <= l && r <= tr) return seg[idx].ff;
int mid = (l + r) >> 1;
int res = MAX;
if (tl <= mid) res = min(res, querymin(tl, tr, l, mid, (idx << 1)));
if (tr > mid) res = min(res, querymin(tl, tr, mid + 1, r, (idx << 1) | 1));
return res;
}
int querymax(int tl, int tr, int l = 1, int r = n, int idx = 1) {
if (tl <= l && r <= tr) return seg[idx].ss;
int mid = (l + r) >> 1;
int res = MIN;
if (tl <= mid) res = max(res, querymax(tl, tr, l, mid, (idx << 1)));
if (tr > mid) res = max(res, querymax(tl, tr, mid + 1, r, (idx << 1) | 1));
return res;
}
map<int, bool> sto;
bool recur(int x, int y) {
// cout << x << ' ' << y << endl;
if (sto.find(y) != sto.end()) return sto[y];
sto[y] = false;
if (x < y) {
int req = querymin(x + 1, y);
if (x <= req) { sto.erase(y); return true; }
if (req == MIN) { sto.erase(y); return false; }
return sto[y] = recur(x, req);
} else if (x > y) {
int req = querymax(y, x - 1);
if (req <= x) { sto.erase(y); return true; }
if (req == MAX) { sto.erase(y); return false; }
return sto[y] = recur(x, req);
} else return true;
}
int occur[mxN];
vector<int> qfreq[mxN];
void solve() {
cin >> n;
for (int i = 1; i < n; i++) cin >> corr[i];
for (int i = 1; i <= n; i++) {
int sz; cin >> sz;
keys[i].resize(sz);
for (int &x : keys[i]) cin >> x;
}
// sweep from left to right, take min of max
for (int i = 1; i <= n; i++) occur[i] = MIN;
a[1].ff = MIN;
for (int i = 1; i < n; i++) {
for (int x : keys[i]) occur[x] = i;
a[i + 1].ff = occur[corr[i]];
}
// sweep from right to left, take max of min
for (int i = 1; i <= n; i++) occur[i] = MAX;
a[n].ss = MAX;
for (int i = n; i > 1; i--) {
for (int x : keys[i]) occur[x] = i;
a[i - 1].ss = occur[corr[i - 1]];
}
// for (int i = 1; i <= n; i++) {
// cout << i << ": " << a[i].ff << ' ' << a[i].ss << endl;
// }
build();
int q; cin >> q;
vector<pair<pii, bool>> queries(q);
for (int i = 0; i < q; i++) {
cin >> queries[i].ff.ff >> queries[i].ff.ss;
qfreq[queries[i].ff.ff].pb(i);
}
for (int i = 1; i <= n; i++) {
sto.clear();
for (int ptr : qfreq[i]) {
queries[ptr].ss = recur(queries[ptr].ff.ff, queries[ptr].ff.ss);
}
}
for (int i = 0; i < q; i++) cout << (queries[i].ss ? "YES\n" : "NO\n");
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
31576 KB |
Output is correct |
2 |
Correct |
11 ms |
31580 KB |
Output is correct |
3 |
Correct |
12 ms |
31576 KB |
Output is correct |
4 |
Correct |
9 ms |
31580 KB |
Output is correct |
5 |
Correct |
9 ms |
31580 KB |
Output is correct |
6 |
Correct |
9 ms |
31580 KB |
Output is correct |
7 |
Correct |
122 ms |
31580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
31576 KB |
Output is correct |
2 |
Correct |
11 ms |
31580 KB |
Output is correct |
3 |
Correct |
12 ms |
31576 KB |
Output is correct |
4 |
Correct |
9 ms |
31580 KB |
Output is correct |
5 |
Correct |
9 ms |
31580 KB |
Output is correct |
6 |
Correct |
9 ms |
31580 KB |
Output is correct |
7 |
Correct |
122 ms |
31580 KB |
Output is correct |
8 |
Correct |
230 ms |
42028 KB |
Output is correct |
9 |
Correct |
243 ms |
42000 KB |
Output is correct |
10 |
Correct |
237 ms |
42324 KB |
Output is correct |
11 |
Correct |
258 ms |
42064 KB |
Output is correct |
12 |
Correct |
201 ms |
42060 KB |
Output is correct |
13 |
Correct |
109 ms |
42496 KB |
Output is correct |
14 |
Correct |
114 ms |
42320 KB |
Output is correct |
15 |
Correct |
103 ms |
42580 KB |
Output is correct |
16 |
Correct |
102 ms |
42576 KB |
Output is correct |
17 |
Correct |
99 ms |
42320 KB |
Output is correct |
18 |
Correct |
119 ms |
42248 KB |
Output is correct |
19 |
Correct |
113 ms |
42320 KB |
Output is correct |
20 |
Correct |
99 ms |
41860 KB |
Output is correct |
21 |
Correct |
112 ms |
42580 KB |
Output is correct |
22 |
Correct |
123 ms |
42320 KB |
Output is correct |
23 |
Correct |
239 ms |
42200 KB |
Output is correct |
24 |
Correct |
230 ms |
42060 KB |
Output is correct |
25 |
Correct |
270 ms |
42204 KB |
Output is correct |
26 |
Correct |
201 ms |
42168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
357 ms |
48976 KB |
Output is correct |
2 |
Correct |
350 ms |
49012 KB |
Output is correct |
3 |
Correct |
335 ms |
48980 KB |
Output is correct |
4 |
Correct |
353 ms |
48976 KB |
Output is correct |
5 |
Correct |
363 ms |
49104 KB |
Output is correct |
6 |
Correct |
208 ms |
47956 KB |
Output is correct |
7 |
Correct |
186 ms |
48472 KB |
Output is correct |
8 |
Correct |
192 ms |
48500 KB |
Output is correct |
9 |
Correct |
197 ms |
48464 KB |
Output is correct |
10 |
Correct |
187 ms |
48468 KB |
Output is correct |
11 |
Correct |
181 ms |
48468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
31576 KB |
Output is correct |
2 |
Correct |
11 ms |
31580 KB |
Output is correct |
3 |
Correct |
12 ms |
31576 KB |
Output is correct |
4 |
Correct |
9 ms |
31580 KB |
Output is correct |
5 |
Correct |
9 ms |
31580 KB |
Output is correct |
6 |
Correct |
9 ms |
31580 KB |
Output is correct |
7 |
Correct |
122 ms |
31580 KB |
Output is correct |
8 |
Correct |
230 ms |
42028 KB |
Output is correct |
9 |
Correct |
243 ms |
42000 KB |
Output is correct |
10 |
Correct |
237 ms |
42324 KB |
Output is correct |
11 |
Correct |
258 ms |
42064 KB |
Output is correct |
12 |
Correct |
201 ms |
42060 KB |
Output is correct |
13 |
Correct |
109 ms |
42496 KB |
Output is correct |
14 |
Correct |
114 ms |
42320 KB |
Output is correct |
15 |
Correct |
103 ms |
42580 KB |
Output is correct |
16 |
Correct |
102 ms |
42576 KB |
Output is correct |
17 |
Correct |
99 ms |
42320 KB |
Output is correct |
18 |
Correct |
119 ms |
42248 KB |
Output is correct |
19 |
Correct |
113 ms |
42320 KB |
Output is correct |
20 |
Correct |
99 ms |
41860 KB |
Output is correct |
21 |
Correct |
112 ms |
42580 KB |
Output is correct |
22 |
Correct |
123 ms |
42320 KB |
Output is correct |
23 |
Correct |
239 ms |
42200 KB |
Output is correct |
24 |
Correct |
230 ms |
42060 KB |
Output is correct |
25 |
Correct |
270 ms |
42204 KB |
Output is correct |
26 |
Correct |
201 ms |
42168 KB |
Output is correct |
27 |
Correct |
357 ms |
48976 KB |
Output is correct |
28 |
Correct |
350 ms |
49012 KB |
Output is correct |
29 |
Correct |
335 ms |
48980 KB |
Output is correct |
30 |
Correct |
353 ms |
48976 KB |
Output is correct |
31 |
Correct |
363 ms |
49104 KB |
Output is correct |
32 |
Correct |
208 ms |
47956 KB |
Output is correct |
33 |
Correct |
186 ms |
48472 KB |
Output is correct |
34 |
Correct |
192 ms |
48500 KB |
Output is correct |
35 |
Correct |
197 ms |
48464 KB |
Output is correct |
36 |
Correct |
187 ms |
48468 KB |
Output is correct |
37 |
Correct |
181 ms |
48468 KB |
Output is correct |
38 |
Correct |
606 ms |
68628 KB |
Output is correct |
39 |
Correct |
643 ms |
72784 KB |
Output is correct |
40 |
Correct |
533 ms |
64340 KB |
Output is correct |
41 |
Correct |
582 ms |
73036 KB |
Output is correct |
42 |
Correct |
214 ms |
47704 KB |
Output is correct |
43 |
Correct |
208 ms |
47440 KB |
Output is correct |
44 |
Correct |
265 ms |
54868 KB |
Output is correct |
45 |
Correct |
260 ms |
54868 KB |
Output is correct |
46 |
Correct |
268 ms |
54884 KB |
Output is correct |
47 |
Correct |
241 ms |
48344 KB |
Output is correct |
48 |
Correct |
231 ms |
47788 KB |
Output is correct |
49 |
Correct |
304 ms |
55180 KB |
Output is correct |
50 |
Correct |
275 ms |
55128 KB |
Output is correct |
51 |
Correct |
268 ms |
54864 KB |
Output is correct |
52 |
Execution timed out |
3099 ms |
83280 KB |
Time limit exceeded |
53 |
Halted |
0 ms |
0 KB |
- |