#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 5e5 + 10;
template<typename T>
struct SparseTable {
vector<vector<T>> st;
T (*F) (T, T);
int len;
void init(vector<T> &a, T(*f)(T, T)) {
len = (int)a.size();
int mxpow = 32 - __builtin_clz(len);
F = f;
st.resize(mxpow);
st[0] = a;
for(int k = 1; k < mxpow; k++) {
st[k].resize(len - (1 << k) + 1);
for(int i = 0; i < (int)st[k].size(); i++)
st[k][i] = F(st[k - 1][i], st[k - 1][i + (1 << (k - 1))]);
}
}
T get(int l, int r) {
if(l > r || l < 0 || r >= len) return -1;
int k = 31 - __builtin_clz(r - l + 1);
return F(st[k][l], st[k][r - (1 << k) + 1]);
}
};
int n;
int C[N];
vector<int> keys[N];
vector<int> Rb(N), Lb(N);
void precalc() {
vector<int> lst(N, -1);
for(int i = 1; i < n; i++) {
for(int x : keys[i]) lst[x] = i;
Lb[i] = lst[C[i]];
}
lst.assign(N, N);
for(int i = n; i > 1; i--) {
for(int x : keys[i]) lst[x] = i;
Rb[i - 1] = lst[C[i - 1]];
}
}
SparseTable<int> mn, mx;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin >> n;
for(int i = 1; i < n; i++)
cin >> C[i];
for(int i = 1; i <= n; i++) {
int k;
cin >> k;
keys[i].resize(k);
for(int &c : keys[i]) {
cin >> c;
}
}
precalc();
mn.init(Lb, [](int x, int y){return (x < y ? x : y);});
mx.init(Rb, [](int x, int y){return (x > y ? x : y);});
vector<int> G(n), T(n);
// for(int i = 1; i < n; i++) {
// cout << Lb[i] << ' ';
// }
// cout << '\n';
// for(int i = 1; i < n; i++) {
// cout << Rb[i] << ' ';
// }
// cout << '\n';
for(int i = 1; i < n; i++) {
int l = max(Lb[i] - 1, 0), r = i;
while(r - l > 1) {
int mid = (l + r) / 2;
if(mx.get(max(Lb[i], 1), mid) > i) r = mid;
else l = mid;
}
T[i] = r;
l = i, r = min(Rb[i], n + 1);
while(r - l > 1) {
int mid = (l + r) / 2;
if(mn.get(mid, min(Rb[i] - 1, n)) <= i) l = mid;
else r = mid;
}
G[i] = l;
}
// for(int i = 1; i < n; i++) {
// cout << G[i] << ' ';
// }
// cout << '\n';
// for(int i = 1; i < n; i++) {
// cout << T[i] << ' ';
// }
// cout << '\n';
SparseTable<int> gg, tt;
gg.init(G, [](int x, int y){return (x > y ? x : y);});
tt.init(T, [](int x, int y){return (x < y ? x : y);});
int q;
cin >> q;
while(q--) {
int s, t;
cin >> s >> t;
if(s < t) {
cout << ((tt.get(s, t - 1) >= s && mn.get(s, t - 1) >= 1) ? "YES\n" : "NO\n");
} else {
cout << ((gg.get(t, s - 1) >= s || mx.get(t, s - 1) > n) ? "NO\n" : "YES\n");
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
86564 KB |
Output is correct |
2 |
Correct |
72 ms |
86604 KB |
Output is correct |
3 |
Correct |
74 ms |
86960 KB |
Output is correct |
4 |
Correct |
81 ms |
86564 KB |
Output is correct |
5 |
Correct |
71 ms |
86548 KB |
Output is correct |
6 |
Correct |
74 ms |
86548 KB |
Output is correct |
7 |
Correct |
79 ms |
86480 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
86564 KB |
Output is correct |
2 |
Correct |
72 ms |
86604 KB |
Output is correct |
3 |
Correct |
74 ms |
86960 KB |
Output is correct |
4 |
Correct |
81 ms |
86564 KB |
Output is correct |
5 |
Correct |
71 ms |
86548 KB |
Output is correct |
6 |
Correct |
74 ms |
86548 KB |
Output is correct |
7 |
Correct |
79 ms |
86480 KB |
Output is correct |
8 |
Correct |
137 ms |
92356 KB |
Output is correct |
9 |
Correct |
140 ms |
92276 KB |
Output is correct |
10 |
Correct |
155 ms |
92784 KB |
Output is correct |
11 |
Correct |
144 ms |
93404 KB |
Output is correct |
12 |
Correct |
136 ms |
91904 KB |
Output is correct |
13 |
Correct |
149 ms |
92576 KB |
Output is correct |
14 |
Correct |
141 ms |
92508 KB |
Output is correct |
15 |
Correct |
138 ms |
92508 KB |
Output is correct |
16 |
Correct |
134 ms |
92348 KB |
Output is correct |
17 |
Correct |
141 ms |
92592 KB |
Output is correct |
18 |
Correct |
144 ms |
92568 KB |
Output is correct |
19 |
Correct |
139 ms |
92616 KB |
Output is correct |
20 |
Correct |
136 ms |
92588 KB |
Output is correct |
21 |
Correct |
150 ms |
92328 KB |
Output is correct |
22 |
Correct |
143 ms |
92444 KB |
Output is correct |
23 |
Correct |
141 ms |
92332 KB |
Output is correct |
24 |
Correct |
137 ms |
92380 KB |
Output is correct |
25 |
Correct |
137 ms |
92316 KB |
Output is correct |
26 |
Correct |
138 ms |
92348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
203 ms |
104460 KB |
Output is correct |
2 |
Correct |
219 ms |
111664 KB |
Output is correct |
3 |
Correct |
223 ms |
111552 KB |
Output is correct |
4 |
Correct |
208 ms |
111760 KB |
Output is correct |
5 |
Correct |
238 ms |
111760 KB |
Output is correct |
6 |
Correct |
200 ms |
111012 KB |
Output is correct |
7 |
Correct |
207 ms |
110892 KB |
Output is correct |
8 |
Correct |
176 ms |
110756 KB |
Output is correct |
9 |
Correct |
178 ms |
110764 KB |
Output is correct |
10 |
Correct |
194 ms |
110876 KB |
Output is correct |
11 |
Correct |
180 ms |
110752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
86564 KB |
Output is correct |
2 |
Correct |
72 ms |
86604 KB |
Output is correct |
3 |
Correct |
74 ms |
86960 KB |
Output is correct |
4 |
Correct |
81 ms |
86564 KB |
Output is correct |
5 |
Correct |
71 ms |
86548 KB |
Output is correct |
6 |
Correct |
74 ms |
86548 KB |
Output is correct |
7 |
Correct |
79 ms |
86480 KB |
Output is correct |
8 |
Correct |
137 ms |
92356 KB |
Output is correct |
9 |
Correct |
140 ms |
92276 KB |
Output is correct |
10 |
Correct |
155 ms |
92784 KB |
Output is correct |
11 |
Correct |
144 ms |
93404 KB |
Output is correct |
12 |
Correct |
136 ms |
91904 KB |
Output is correct |
13 |
Correct |
149 ms |
92576 KB |
Output is correct |
14 |
Correct |
141 ms |
92508 KB |
Output is correct |
15 |
Correct |
138 ms |
92508 KB |
Output is correct |
16 |
Correct |
134 ms |
92348 KB |
Output is correct |
17 |
Correct |
141 ms |
92592 KB |
Output is correct |
18 |
Correct |
144 ms |
92568 KB |
Output is correct |
19 |
Correct |
139 ms |
92616 KB |
Output is correct |
20 |
Correct |
136 ms |
92588 KB |
Output is correct |
21 |
Correct |
150 ms |
92328 KB |
Output is correct |
22 |
Correct |
143 ms |
92444 KB |
Output is correct |
23 |
Correct |
141 ms |
92332 KB |
Output is correct |
24 |
Correct |
137 ms |
92380 KB |
Output is correct |
25 |
Correct |
137 ms |
92316 KB |
Output is correct |
26 |
Correct |
138 ms |
92348 KB |
Output is correct |
27 |
Correct |
203 ms |
104460 KB |
Output is correct |
28 |
Correct |
219 ms |
111664 KB |
Output is correct |
29 |
Correct |
223 ms |
111552 KB |
Output is correct |
30 |
Correct |
208 ms |
111760 KB |
Output is correct |
31 |
Correct |
238 ms |
111760 KB |
Output is correct |
32 |
Correct |
200 ms |
111012 KB |
Output is correct |
33 |
Correct |
207 ms |
110892 KB |
Output is correct |
34 |
Correct |
176 ms |
110756 KB |
Output is correct |
35 |
Correct |
178 ms |
110764 KB |
Output is correct |
36 |
Correct |
194 ms |
110876 KB |
Output is correct |
37 |
Correct |
180 ms |
110752 KB |
Output is correct |
38 |
Correct |
574 ms |
171988 KB |
Output is correct |
39 |
Correct |
484 ms |
190868 KB |
Output is correct |
40 |
Correct |
378 ms |
151676 KB |
Output is correct |
41 |
Correct |
379 ms |
189268 KB |
Output is correct |
42 |
Correct |
226 ms |
111952 KB |
Output is correct |
43 |
Correct |
232 ms |
111972 KB |
Output is correct |
44 |
Correct |
301 ms |
132772 KB |
Output is correct |
45 |
Correct |
276 ms |
132944 KB |
Output is correct |
46 |
Correct |
285 ms |
132980 KB |
Output is correct |
47 |
Correct |
184 ms |
112080 KB |
Output is correct |
48 |
Correct |
184 ms |
111856 KB |
Output is correct |
49 |
Correct |
243 ms |
132700 KB |
Output is correct |
50 |
Correct |
237 ms |
132976 KB |
Output is correct |
51 |
Correct |
260 ms |
133192 KB |
Output is correct |
52 |
Correct |
281 ms |
131736 KB |
Output is correct |
53 |
Correct |
336 ms |
152144 KB |
Output is correct |
54 |
Correct |
415 ms |
172844 KB |
Output is correct |
55 |
Correct |
343 ms |
152216 KB |
Output is correct |