#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int maxn = 500005;
int N, Q;
int C[maxn];
set<int> keys[maxn];
int furthest[maxn]; //furthest right index just by going right
struct Query {
int st, en, id;
};
vector<Query> queries[maxn]; //queries that start at queries[i]
void expand(int &li, int &ri, set<int> &se) {
while (true) {
if (li > 1 && se.count(C[li-1])) {
--li;
for (int a: keys[li]) se.insert(a);
}
else if (ri < N && se.count(C[ri])) {
++ri;
for (int a: keys[ri]) se.insert(a);
}
else break;
}
}
int li[maxn], ri[maxn];
set<int>* currkeys[maxn];
vector<int> pos[maxn]; //list of posititions for keys
int main()
{
ios_base::sync_with_stdio(false); cin.tie(NULL);
cin >> N;
for (int i = 1; i <= N - 1; i++) {
cin >> C[i];
}
for (int i = 1; i <= N; i++) {
int k; cin >> k;
for (int j = 0; j < k; j++) {
int key; cin >> key;
keys[i].insert(key);
pos[key].push_back(i);
}
}
//precalc
furthest[N] = N;
for (int i = N-1; i >= 1; i--) {
furthest[i] = keys[i].count(C[i]) ? furthest[i+1] : i;
}
cin >> Q;
for (int i = 0; i < Q; i++) {
int x, y; cin >> x >> y;
queries[x].push_back({x,y,i});
}
vector<string> ans(Q);
for (int i = 1; i <= N; i++) {
li[i] = ri[i] = i;
if (keys[i].count(C[i-1])) {
if (ri[i-1] >= i) {
//same
li[i] = li[i-1];
ri[i] = ri[i-1];
currkeys[i] = currkeys[i-1];
//copy the pointer over
}
else {
li[i] = li[i-1];
currkeys[i] = currkeys[i-1];
for (int a: keys[i]) currkeys[i]->insert(a);
expand(li[i],ri[i],*currkeys[i]);
}
}
else {
/*
currkeys[i] = new set<int>;
for (int a: keys[i]) currkeys[i]->insert(a);
expand(li[i],ri[i],*currkeys[i]);
*/
ri[i] = furthest[i];
auto it = lower_bound(pos[C[i-1]].begin(),pos[C[i-1]].end(),i);
if (it != pos[C[i-1]].end() && *it <= ri[i]) {
li[i] = min(li[i],li[i-1]);
ri[i] = max(ri[i],ri[i-1]);
currkeys[i] = currkeys[i-1];
for (int j = i; j <= ri[i]; j++) {
for (int a: keys[j]) currkeys[i]->insert(a);
}
expand(li[i],ri[i],*currkeys[i]);
}
else {
currkeys[i] = new set<int>;
for (int j = i; j <= ri[i]; j++) {
for (int a: keys[j]) currkeys[i]->insert(a);
}
expand(li[i],ri[i],*currkeys[i]);
}
}
for (Query q: queries[i]) {
ans[q.id] = (li[i] <= q.en && q.en <= ri[i]) ? "YES" : "NO";
}
}
//output
for (int i = 0; i < Q; i++) {
cout << ans[i] << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
48248 KB |
Output is correct |
2 |
Correct |
127 ms |
48248 KB |
Output is correct |
3 |
Correct |
208 ms |
48248 KB |
Output is correct |
4 |
Correct |
46 ms |
48120 KB |
Output is correct |
5 |
Correct |
48 ms |
47864 KB |
Output is correct |
6 |
Correct |
47 ms |
47988 KB |
Output is correct |
7 |
Correct |
49 ms |
48376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
48248 KB |
Output is correct |
2 |
Correct |
127 ms |
48248 KB |
Output is correct |
3 |
Correct |
208 ms |
48248 KB |
Output is correct |
4 |
Correct |
46 ms |
48120 KB |
Output is correct |
5 |
Correct |
48 ms |
47864 KB |
Output is correct |
6 |
Correct |
47 ms |
47988 KB |
Output is correct |
7 |
Correct |
49 ms |
48376 KB |
Output is correct |
8 |
Correct |
273 ms |
74076 KB |
Output is correct |
9 |
Correct |
289 ms |
74272 KB |
Output is correct |
10 |
Correct |
386 ms |
74900 KB |
Output is correct |
11 |
Correct |
487 ms |
73688 KB |
Output is correct |
12 |
Correct |
320 ms |
73752 KB |
Output is correct |
13 |
Correct |
234 ms |
75220 KB |
Output is correct |
14 |
Correct |
233 ms |
75128 KB |
Output is correct |
15 |
Correct |
300 ms |
74872 KB |
Output is correct |
16 |
Correct |
254 ms |
74872 KB |
Output is correct |
17 |
Correct |
241 ms |
75128 KB |
Output is correct |
18 |
Correct |
244 ms |
74820 KB |
Output is correct |
19 |
Correct |
239 ms |
75012 KB |
Output is correct |
20 |
Correct |
264 ms |
73164 KB |
Output is correct |
21 |
Correct |
239 ms |
74872 KB |
Output is correct |
22 |
Correct |
246 ms |
75136 KB |
Output is correct |
23 |
Correct |
257 ms |
74232 KB |
Output is correct |
24 |
Correct |
329 ms |
74480 KB |
Output is correct |
25 |
Correct |
266 ms |
74232 KB |
Output is correct |
26 |
Correct |
269 ms |
74088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3026 ms |
97036 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
48248 KB |
Output is correct |
2 |
Correct |
127 ms |
48248 KB |
Output is correct |
3 |
Correct |
208 ms |
48248 KB |
Output is correct |
4 |
Correct |
46 ms |
48120 KB |
Output is correct |
5 |
Correct |
48 ms |
47864 KB |
Output is correct |
6 |
Correct |
47 ms |
47988 KB |
Output is correct |
7 |
Correct |
49 ms |
48376 KB |
Output is correct |
8 |
Correct |
273 ms |
74076 KB |
Output is correct |
9 |
Correct |
289 ms |
74272 KB |
Output is correct |
10 |
Correct |
386 ms |
74900 KB |
Output is correct |
11 |
Correct |
487 ms |
73688 KB |
Output is correct |
12 |
Correct |
320 ms |
73752 KB |
Output is correct |
13 |
Correct |
234 ms |
75220 KB |
Output is correct |
14 |
Correct |
233 ms |
75128 KB |
Output is correct |
15 |
Correct |
300 ms |
74872 KB |
Output is correct |
16 |
Correct |
254 ms |
74872 KB |
Output is correct |
17 |
Correct |
241 ms |
75128 KB |
Output is correct |
18 |
Correct |
244 ms |
74820 KB |
Output is correct |
19 |
Correct |
239 ms |
75012 KB |
Output is correct |
20 |
Correct |
264 ms |
73164 KB |
Output is correct |
21 |
Correct |
239 ms |
74872 KB |
Output is correct |
22 |
Correct |
246 ms |
75136 KB |
Output is correct |
23 |
Correct |
257 ms |
74232 KB |
Output is correct |
24 |
Correct |
329 ms |
74480 KB |
Output is correct |
25 |
Correct |
266 ms |
74232 KB |
Output is correct |
26 |
Correct |
269 ms |
74088 KB |
Output is correct |
27 |
Execution timed out |
3026 ms |
97036 KB |
Time limit exceeded |
28 |
Halted |
0 ms |
0 KB |
- |