#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 sp[mxN][20];
pii a[mxN];
int corr[mxN];
vector<int> keys[mxN];
int n;
int flog2(int x) {
return 31 - __builtin_clz(x);
}
void build() {
for (int i = 1; i <= n; i++) {
sp[i][0] = a[i];
}
for (int bit = 1; bit < 20; bit++) {
for (int i = 1; i + (1 << bit) - 1 <= n; i++) {
sp[i][bit].ff = min(sp[i][bit - 1].ff, sp[i + (1 << (bit - 1))][bit - 1].ff);
sp[i][bit].ss = max(sp[i][bit - 1].ss, sp[i + (1 << (bit - 1))][bit - 1].ss);
}
}
}
int querymin(int l, int r) {
int bit = flog2(r - l + 1);
return min(sp[l][bit].ff, sp[r - (1 << bit) + 1][bit].ff);
}
int querymax(int l, int r) {
int bit = flog2(r - l + 1);
return max(sp[l][bit].ss, sp[r - (1 << bit) + 1][bit].ss);
}
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 |
10 ms |
31580 KB |
Output is correct |
2 |
Correct |
10 ms |
31576 KB |
Output is correct |
3 |
Correct |
10 ms |
31580 KB |
Output is correct |
4 |
Correct |
9 ms |
31580 KB |
Output is correct |
5 |
Correct |
9 ms |
31576 KB |
Output is correct |
6 |
Correct |
9 ms |
31580 KB |
Output is correct |
7 |
Correct |
85 ms |
31576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
31580 KB |
Output is correct |
2 |
Correct |
10 ms |
31576 KB |
Output is correct |
3 |
Correct |
10 ms |
31580 KB |
Output is correct |
4 |
Correct |
9 ms |
31580 KB |
Output is correct |
5 |
Correct |
9 ms |
31576 KB |
Output is correct |
6 |
Correct |
9 ms |
31580 KB |
Output is correct |
7 |
Correct |
85 ms |
31576 KB |
Output is correct |
8 |
Correct |
187 ms |
42072 KB |
Output is correct |
9 |
Correct |
203 ms |
41808 KB |
Output is correct |
10 |
Correct |
167 ms |
42324 KB |
Output is correct |
11 |
Correct |
165 ms |
42096 KB |
Output is correct |
12 |
Correct |
163 ms |
42064 KB |
Output is correct |
13 |
Correct |
107 ms |
42316 KB |
Output is correct |
14 |
Correct |
91 ms |
42304 KB |
Output is correct |
15 |
Correct |
92 ms |
42600 KB |
Output is correct |
16 |
Correct |
88 ms |
42440 KB |
Output is correct |
17 |
Correct |
95 ms |
42324 KB |
Output is correct |
18 |
Correct |
93 ms |
42424 KB |
Output is correct |
19 |
Correct |
104 ms |
42464 KB |
Output is correct |
20 |
Correct |
112 ms |
41748 KB |
Output is correct |
21 |
Correct |
90 ms |
42512 KB |
Output is correct |
22 |
Correct |
112 ms |
42604 KB |
Output is correct |
23 |
Correct |
180 ms |
42324 KB |
Output is correct |
24 |
Correct |
194 ms |
42080 KB |
Output is correct |
25 |
Correct |
229 ms |
42204 KB |
Output is correct |
26 |
Correct |
156 ms |
42064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
312 ms |
61336 KB |
Output is correct |
2 |
Correct |
248 ms |
61264 KB |
Output is correct |
3 |
Correct |
318 ms |
61264 KB |
Output is correct |
4 |
Correct |
245 ms |
61268 KB |
Output is correct |
5 |
Correct |
269 ms |
61128 KB |
Output is correct |
6 |
Correct |
192 ms |
60244 KB |
Output is correct |
7 |
Correct |
166 ms |
60828 KB |
Output is correct |
8 |
Correct |
174 ms |
60756 KB |
Output is correct |
9 |
Correct |
163 ms |
60756 KB |
Output is correct |
10 |
Correct |
166 ms |
60988 KB |
Output is correct |
11 |
Correct |
176 ms |
60928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
31580 KB |
Output is correct |
2 |
Correct |
10 ms |
31576 KB |
Output is correct |
3 |
Correct |
10 ms |
31580 KB |
Output is correct |
4 |
Correct |
9 ms |
31580 KB |
Output is correct |
5 |
Correct |
9 ms |
31576 KB |
Output is correct |
6 |
Correct |
9 ms |
31580 KB |
Output is correct |
7 |
Correct |
85 ms |
31576 KB |
Output is correct |
8 |
Correct |
187 ms |
42072 KB |
Output is correct |
9 |
Correct |
203 ms |
41808 KB |
Output is correct |
10 |
Correct |
167 ms |
42324 KB |
Output is correct |
11 |
Correct |
165 ms |
42096 KB |
Output is correct |
12 |
Correct |
163 ms |
42064 KB |
Output is correct |
13 |
Correct |
107 ms |
42316 KB |
Output is correct |
14 |
Correct |
91 ms |
42304 KB |
Output is correct |
15 |
Correct |
92 ms |
42600 KB |
Output is correct |
16 |
Correct |
88 ms |
42440 KB |
Output is correct |
17 |
Correct |
95 ms |
42324 KB |
Output is correct |
18 |
Correct |
93 ms |
42424 KB |
Output is correct |
19 |
Correct |
104 ms |
42464 KB |
Output is correct |
20 |
Correct |
112 ms |
41748 KB |
Output is correct |
21 |
Correct |
90 ms |
42512 KB |
Output is correct |
22 |
Correct |
112 ms |
42604 KB |
Output is correct |
23 |
Correct |
180 ms |
42324 KB |
Output is correct |
24 |
Correct |
194 ms |
42080 KB |
Output is correct |
25 |
Correct |
229 ms |
42204 KB |
Output is correct |
26 |
Correct |
156 ms |
42064 KB |
Output is correct |
27 |
Correct |
312 ms |
61336 KB |
Output is correct |
28 |
Correct |
248 ms |
61264 KB |
Output is correct |
29 |
Correct |
318 ms |
61264 KB |
Output is correct |
30 |
Correct |
245 ms |
61268 KB |
Output is correct |
31 |
Correct |
269 ms |
61128 KB |
Output is correct |
32 |
Correct |
192 ms |
60244 KB |
Output is correct |
33 |
Correct |
166 ms |
60828 KB |
Output is correct |
34 |
Correct |
174 ms |
60756 KB |
Output is correct |
35 |
Correct |
163 ms |
60756 KB |
Output is correct |
36 |
Correct |
166 ms |
60988 KB |
Output is correct |
37 |
Correct |
176 ms |
60928 KB |
Output is correct |
38 |
Correct |
556 ms |
123728 KB |
Output is correct |
39 |
Correct |
583 ms |
142772 KB |
Output is correct |
40 |
Correct |
477 ms |
104948 KB |
Output is correct |
41 |
Correct |
614 ms |
143208 KB |
Output is correct |
42 |
Correct |
174 ms |
59996 KB |
Output is correct |
43 |
Correct |
187 ms |
60244 KB |
Output is correct |
44 |
Correct |
246 ms |
81456 KB |
Output is correct |
45 |
Correct |
262 ms |
81488 KB |
Output is correct |
46 |
Correct |
262 ms |
81488 KB |
Output is correct |
47 |
Correct |
164 ms |
60324 KB |
Output is correct |
48 |
Correct |
178 ms |
60188 KB |
Output is correct |
49 |
Correct |
254 ms |
81572 KB |
Output is correct |
50 |
Correct |
261 ms |
81600 KB |
Output is correct |
51 |
Correct |
252 ms |
81516 KB |
Output is correct |
52 |
Execution timed out |
3094 ms |
100848 KB |
Time limit exceeded |
53 |
Halted |
0 ms |
0 KB |
- |