#include <bits/stdc++.h>
using namespace std;
#define TRACE(x) cerr << #x << " :: " << x << endl
#define _ << " " <<
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(),(x).end()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i)
typedef long long ll;
typedef pair<ll,ll> pll;
const int mxN = 5e5+5;
int N, C[mxN], Q;
set<int> keys[mxN];
int k2[mxN];
int goleft[mxN], goright[mxN];
void mergeto(set<int>& a, set<int>& b) {
if (SZ(a) > SZ(b)) swap(a,b);
for (auto& x : a) b.insert(x);
}
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> N;
FOR(i,1,N-1){
cin >> C[i];
}
C[0] = C[N] = 1e9+5; // impossible
FOR(i,1,N){
int B; cin >> B;
FOR(j,1,B){
int A; cin >> A;
keys[i].insert(A);
k2[i] |= (1<<A);
}
}
if (N <= 5000) {
FOR(i,1,N){
set<int> k;
for (auto& x : keys[i]) k.insert(x);
goleft[i] = goright[i] = i;
while (true) {
if (k.count(C[goleft[i]-1])) {
--goleft[i];
for (auto& x : keys[goleft[i]]) k.insert(x);
} else if (k.count(C[goright[i]])) {
++goright[i];
for (auto& x : keys[goright[i]]) k.insert(x);
} else break;
}
}
} else {
FOR(i,1,N){
goleft[i] = goright[i] = i;
while (true) {
if (goleft[i] > 1 && (k2[i] & (1<<C[goleft[i]-1]))) {
--goleft[i];
k2[i] |= k2[goleft[i]];
goleft[i] = goleft[goleft[i]];
goright[i] = max(goright[i], goright[i]);
} else if (goright[i] < N && (k2[i] & (1<<C[goright[i]]))) {
++goright[i];
k2[i] |= k2[goright[i]];
} else break;
}
}
}
cin >> Q;
FOR(i,1,Q){
int X, Y; cin >> X >> Y;
cout << (goleft[X] <= Y && Y <= goright[X] ? "YES" : "NO") << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
24080 KB |
Output is correct |
2 |
Correct |
174 ms |
24048 KB |
Output is correct |
3 |
Correct |
433 ms |
24128 KB |
Output is correct |
4 |
Correct |
38 ms |
24032 KB |
Output is correct |
5 |
Correct |
452 ms |
24032 KB |
Output is correct |
6 |
Correct |
178 ms |
24044 KB |
Output is correct |
7 |
Correct |
136 ms |
24036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
24080 KB |
Output is correct |
2 |
Correct |
174 ms |
24048 KB |
Output is correct |
3 |
Correct |
433 ms |
24128 KB |
Output is correct |
4 |
Correct |
38 ms |
24032 KB |
Output is correct |
5 |
Correct |
452 ms |
24032 KB |
Output is correct |
6 |
Correct |
178 ms |
24044 KB |
Output is correct |
7 |
Correct |
136 ms |
24036 KB |
Output is correct |
8 |
Correct |
208 ms |
25716 KB |
Output is correct |
9 |
Correct |
195 ms |
25480 KB |
Output is correct |
10 |
Correct |
311 ms |
25692 KB |
Output is correct |
11 |
Correct |
561 ms |
25908 KB |
Output is correct |
12 |
Correct |
203 ms |
25768 KB |
Output is correct |
13 |
Correct |
179 ms |
25768 KB |
Output is correct |
14 |
Correct |
177 ms |
25708 KB |
Output is correct |
15 |
Correct |
423 ms |
25820 KB |
Output is correct |
16 |
Correct |
618 ms |
26016 KB |
Output is correct |
17 |
Correct |
177 ms |
25716 KB |
Output is correct |
18 |
Correct |
176 ms |
25776 KB |
Output is correct |
19 |
Correct |
233 ms |
25716 KB |
Output is correct |
20 |
Correct |
548 ms |
25928 KB |
Output is correct |
21 |
Correct |
585 ms |
26028 KB |
Output is correct |
22 |
Correct |
493 ms |
25784 KB |
Output is correct |
23 |
Correct |
320 ms |
25592 KB |
Output is correct |
24 |
Correct |
297 ms |
25588 KB |
Output is correct |
25 |
Correct |
267 ms |
25548 KB |
Output is correct |
26 |
Correct |
215 ms |
25556 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1689 ms |
47688 KB |
Output is correct |
2 |
Execution timed out |
3047 ms |
45200 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
24080 KB |
Output is correct |
2 |
Correct |
174 ms |
24048 KB |
Output is correct |
3 |
Correct |
433 ms |
24128 KB |
Output is correct |
4 |
Correct |
38 ms |
24032 KB |
Output is correct |
5 |
Correct |
452 ms |
24032 KB |
Output is correct |
6 |
Correct |
178 ms |
24044 KB |
Output is correct |
7 |
Correct |
136 ms |
24036 KB |
Output is correct |
8 |
Correct |
208 ms |
25716 KB |
Output is correct |
9 |
Correct |
195 ms |
25480 KB |
Output is correct |
10 |
Correct |
311 ms |
25692 KB |
Output is correct |
11 |
Correct |
561 ms |
25908 KB |
Output is correct |
12 |
Correct |
203 ms |
25768 KB |
Output is correct |
13 |
Correct |
179 ms |
25768 KB |
Output is correct |
14 |
Correct |
177 ms |
25708 KB |
Output is correct |
15 |
Correct |
423 ms |
25820 KB |
Output is correct |
16 |
Correct |
618 ms |
26016 KB |
Output is correct |
17 |
Correct |
177 ms |
25716 KB |
Output is correct |
18 |
Correct |
176 ms |
25776 KB |
Output is correct |
19 |
Correct |
233 ms |
25716 KB |
Output is correct |
20 |
Correct |
548 ms |
25928 KB |
Output is correct |
21 |
Correct |
585 ms |
26028 KB |
Output is correct |
22 |
Correct |
493 ms |
25784 KB |
Output is correct |
23 |
Correct |
320 ms |
25592 KB |
Output is correct |
24 |
Correct |
297 ms |
25588 KB |
Output is correct |
25 |
Correct |
267 ms |
25548 KB |
Output is correct |
26 |
Correct |
215 ms |
25556 KB |
Output is correct |
27 |
Correct |
1689 ms |
47688 KB |
Output is correct |
28 |
Execution timed out |
3047 ms |
45200 KB |
Time limit exceeded |
29 |
Halted |
0 ms |
0 KB |
- |