#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);
}
}
*/
}
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
48376 KB |
Output is correct |
2 |
Correct |
144 ms |
48252 KB |
Output is correct |
3 |
Correct |
258 ms |
48504 KB |
Output is correct |
4 |
Correct |
48 ms |
48120 KB |
Output is correct |
5 |
Correct |
47 ms |
47992 KB |
Output is correct |
6 |
Correct |
46 ms |
48252 KB |
Output is correct |
7 |
Correct |
49 ms |
49144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
48376 KB |
Output is correct |
2 |
Correct |
144 ms |
48252 KB |
Output is correct |
3 |
Correct |
258 ms |
48504 KB |
Output is correct |
4 |
Correct |
48 ms |
48120 KB |
Output is correct |
5 |
Correct |
47 ms |
47992 KB |
Output is correct |
6 |
Correct |
46 ms |
48252 KB |
Output is correct |
7 |
Correct |
49 ms |
49144 KB |
Output is correct |
8 |
Correct |
315 ms |
74348 KB |
Output is correct |
9 |
Correct |
287 ms |
74204 KB |
Output is correct |
10 |
Correct |
426 ms |
75000 KB |
Output is correct |
11 |
Correct |
507 ms |
73720 KB |
Output is correct |
12 |
Correct |
322 ms |
73852 KB |
Output is correct |
13 |
Correct |
226 ms |
75256 KB |
Output is correct |
14 |
Correct |
294 ms |
75384 KB |
Output is correct |
15 |
Correct |
253 ms |
75000 KB |
Output is correct |
16 |
Correct |
292 ms |
75128 KB |
Output is correct |
17 |
Correct |
226 ms |
75384 KB |
Output is correct |
18 |
Correct |
247 ms |
75000 KB |
Output is correct |
19 |
Correct |
228 ms |
75236 KB |
Output is correct |
20 |
Correct |
244 ms |
73424 KB |
Output is correct |
21 |
Correct |
238 ms |
75256 KB |
Output is correct |
22 |
Correct |
227 ms |
75256 KB |
Output is correct |
23 |
Correct |
270 ms |
74500 KB |
Output is correct |
24 |
Correct |
274 ms |
74664 KB |
Output is correct |
25 |
Correct |
290 ms |
74548 KB |
Output is correct |
26 |
Correct |
277 ms |
74232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3091 ms |
96800 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
48376 KB |
Output is correct |
2 |
Correct |
144 ms |
48252 KB |
Output is correct |
3 |
Correct |
258 ms |
48504 KB |
Output is correct |
4 |
Correct |
48 ms |
48120 KB |
Output is correct |
5 |
Correct |
47 ms |
47992 KB |
Output is correct |
6 |
Correct |
46 ms |
48252 KB |
Output is correct |
7 |
Correct |
49 ms |
49144 KB |
Output is correct |
8 |
Correct |
315 ms |
74348 KB |
Output is correct |
9 |
Correct |
287 ms |
74204 KB |
Output is correct |
10 |
Correct |
426 ms |
75000 KB |
Output is correct |
11 |
Correct |
507 ms |
73720 KB |
Output is correct |
12 |
Correct |
322 ms |
73852 KB |
Output is correct |
13 |
Correct |
226 ms |
75256 KB |
Output is correct |
14 |
Correct |
294 ms |
75384 KB |
Output is correct |
15 |
Correct |
253 ms |
75000 KB |
Output is correct |
16 |
Correct |
292 ms |
75128 KB |
Output is correct |
17 |
Correct |
226 ms |
75384 KB |
Output is correct |
18 |
Correct |
247 ms |
75000 KB |
Output is correct |
19 |
Correct |
228 ms |
75236 KB |
Output is correct |
20 |
Correct |
244 ms |
73424 KB |
Output is correct |
21 |
Correct |
238 ms |
75256 KB |
Output is correct |
22 |
Correct |
227 ms |
75256 KB |
Output is correct |
23 |
Correct |
270 ms |
74500 KB |
Output is correct |
24 |
Correct |
274 ms |
74664 KB |
Output is correct |
25 |
Correct |
290 ms |
74548 KB |
Output is correct |
26 |
Correct |
277 ms |
74232 KB |
Output is correct |
27 |
Execution timed out |
3091 ms |
96800 KB |
Time limit exceeded |
28 |
Halted |
0 ms |
0 KB |
- |