#include <bits/stdc++.h>
// #define BALBIT 1
using namespace std;
#define ll long long
#define pii pair<int, int>
#define f first
#define s second
#define pb push_back
#define SZ(x) (int)((x).size())
#define REP1(i,n) for(int i = 1; i<=n;++i)
#define REP(i,n) for (int i = 0; i<n;++i)
#ifdef BALBIT
#define bug(...) cout<<"#"<<__LINE__<<": "<<#__VA_ARGS__<<"- ", _do(__VA_ARGS__)
template<typename T> void _do(T && x) {cout<<x<<endl;}
template<typename T, typename ...S> void _do(T && x, S&& ...y) {cout<<x<<", "; _do(y...);}
#else
#define bug(...)
#define endl '\n'
#endif // BALBIT
const int inf = 1e9;
const int N = 500005;
int tol[21][N], tor[21][N], need[N], lb[N], rb[N];
int last[N];
vector<int> keys[N];
signed main(){
ios::sync_with_stdio(0), cin.tie(0);
// cout<<"HI";
// bug("HI"); return 0;
int n; cin>>n;
// assert(n<=10);
// tol[0] = -inf;
fill(last, last+n+1, -inf);
REP1(i,n-1) cin>>need[i];
REP1(i,n) {
int k; cin>>k;
REP(j,k) {
int y; cin>>y; keys[i].pb(y);
last[y] = i;
}
if(i !=n)
tol[0][i] = last[need[i]];
else
tol[0][i] = -inf;
}
// return 0;
// tol[n] = inf;
fill(last, last+n+1, inf);
for(int i = n; i>=1; --i) {
for(int y : keys[i]) last[y] = i;
if (i!=1)
tor[0][i] = last[need[i-1]];
else
tor[0][i] = inf;
}
// return 0;
REP1(j,20) {
int df = (1<<(j-1));
REP1(i,n) {
tol[j][i] = tol[j-1][i];
tor[j][i] = tor[j-1][i];
if(i+df<=n)
tol[j][i] = min(tol[j][i], tol[j-1][i+df]);
if(i-df>=1)
tor[j][i] = max(tor[j][i], tor[j-1][i-df]);
}
}
// return 0;
// int bigr = 0;
REP1(i,n) {
// bug(i);
if(rb[i-1] >= i) {
// covered
int at = i;
for(int j = 20; j>=0; --j) {
if(tol[j][at] >= i) {
at += (1<<j);
}
}
bug(at);
if(tor[0][i]<=at) {
lb[i] = lb[i-1]; rb[i] = rb[i-1];
}else{
lb[i] = i; rb[i] = at;
}
bug(i, lb[i], rb[i]);
}else{
lb[i] = rb[i] = i;
for(;; ) {
// grow to the left
for(int j = 20; j>=0; --j) {
if(tor[j][lb[i]] <= rb[i]) {
lb[i] -= (1<<j);
}
}
int tmp = rb[i];
for(int j = 20; j>=0; --j) {
if(tol[j][rb[i]] >= lb[i]) {
rb[i] += (1<<j);
}
}
if(tmp == rb[i]) break;
}
bug(i, lb[i], rb[i]);
}
}
// return 0;
int q; cin>>q;
while(q--) {
int x, y; cin>>x>>y;
// bug(x,lb[x],rb[x],y);
if(y>=lb[x]&&y<=rb[x]) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
12908 KB |
Output is correct |
2 |
Correct |
11 ms |
13164 KB |
Output is correct |
3 |
Correct |
12 ms |
13548 KB |
Output is correct |
4 |
Correct |
11 ms |
12908 KB |
Output is correct |
5 |
Correct |
11 ms |
12908 KB |
Output is correct |
6 |
Correct |
11 ms |
12908 KB |
Output is correct |
7 |
Correct |
11 ms |
12908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
12908 KB |
Output is correct |
2 |
Correct |
11 ms |
13164 KB |
Output is correct |
3 |
Correct |
12 ms |
13548 KB |
Output is correct |
4 |
Correct |
11 ms |
12908 KB |
Output is correct |
5 |
Correct |
11 ms |
12908 KB |
Output is correct |
6 |
Correct |
11 ms |
12908 KB |
Output is correct |
7 |
Correct |
11 ms |
12908 KB |
Output is correct |
8 |
Correct |
138 ms |
18796 KB |
Output is correct |
9 |
Correct |
152 ms |
18796 KB |
Output is correct |
10 |
Correct |
142 ms |
19308 KB |
Output is correct |
11 |
Correct |
137 ms |
19948 KB |
Output is correct |
12 |
Correct |
128 ms |
18156 KB |
Output is correct |
13 |
Correct |
135 ms |
18924 KB |
Output is correct |
14 |
Correct |
151 ms |
18924 KB |
Output is correct |
15 |
Correct |
133 ms |
19052 KB |
Output is correct |
16 |
Correct |
141 ms |
18816 KB |
Output is correct |
17 |
Correct |
132 ms |
18924 KB |
Output is correct |
18 |
Correct |
136 ms |
19316 KB |
Output is correct |
19 |
Correct |
133 ms |
19044 KB |
Output is correct |
20 |
Correct |
136 ms |
18924 KB |
Output is correct |
21 |
Correct |
134 ms |
18796 KB |
Output is correct |
22 |
Correct |
133 ms |
18924 KB |
Output is correct |
23 |
Correct |
139 ms |
18796 KB |
Output is correct |
24 |
Correct |
156 ms |
18924 KB |
Output is correct |
25 |
Correct |
137 ms |
18900 KB |
Output is correct |
26 |
Correct |
139 ms |
18796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
250 ms |
41324 KB |
Output is correct |
2 |
Correct |
250 ms |
43116 KB |
Output is correct |
3 |
Correct |
246 ms |
42732 KB |
Output is correct |
4 |
Correct |
244 ms |
43116 KB |
Output is correct |
5 |
Correct |
244 ms |
43116 KB |
Output is correct |
6 |
Correct |
211 ms |
41964 KB |
Output is correct |
7 |
Correct |
206 ms |
41580 KB |
Output is correct |
8 |
Correct |
213 ms |
41708 KB |
Output is correct |
9 |
Correct |
212 ms |
41708 KB |
Output is correct |
10 |
Correct |
216 ms |
41580 KB |
Output is correct |
11 |
Correct |
249 ms |
41708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
12908 KB |
Output is correct |
2 |
Correct |
11 ms |
13164 KB |
Output is correct |
3 |
Correct |
12 ms |
13548 KB |
Output is correct |
4 |
Correct |
11 ms |
12908 KB |
Output is correct |
5 |
Correct |
11 ms |
12908 KB |
Output is correct |
6 |
Correct |
11 ms |
12908 KB |
Output is correct |
7 |
Correct |
11 ms |
12908 KB |
Output is correct |
8 |
Correct |
138 ms |
18796 KB |
Output is correct |
9 |
Correct |
152 ms |
18796 KB |
Output is correct |
10 |
Correct |
142 ms |
19308 KB |
Output is correct |
11 |
Correct |
137 ms |
19948 KB |
Output is correct |
12 |
Correct |
128 ms |
18156 KB |
Output is correct |
13 |
Correct |
135 ms |
18924 KB |
Output is correct |
14 |
Correct |
151 ms |
18924 KB |
Output is correct |
15 |
Correct |
133 ms |
19052 KB |
Output is correct |
16 |
Correct |
141 ms |
18816 KB |
Output is correct |
17 |
Correct |
132 ms |
18924 KB |
Output is correct |
18 |
Correct |
136 ms |
19316 KB |
Output is correct |
19 |
Correct |
133 ms |
19044 KB |
Output is correct |
20 |
Correct |
136 ms |
18924 KB |
Output is correct |
21 |
Correct |
134 ms |
18796 KB |
Output is correct |
22 |
Correct |
133 ms |
18924 KB |
Output is correct |
23 |
Correct |
139 ms |
18796 KB |
Output is correct |
24 |
Correct |
156 ms |
18924 KB |
Output is correct |
25 |
Correct |
137 ms |
18900 KB |
Output is correct |
26 |
Correct |
139 ms |
18796 KB |
Output is correct |
27 |
Correct |
250 ms |
41324 KB |
Output is correct |
28 |
Correct |
250 ms |
43116 KB |
Output is correct |
29 |
Correct |
246 ms |
42732 KB |
Output is correct |
30 |
Correct |
244 ms |
43116 KB |
Output is correct |
31 |
Correct |
244 ms |
43116 KB |
Output is correct |
32 |
Correct |
211 ms |
41964 KB |
Output is correct |
33 |
Correct |
206 ms |
41580 KB |
Output is correct |
34 |
Correct |
213 ms |
41708 KB |
Output is correct |
35 |
Correct |
212 ms |
41708 KB |
Output is correct |
36 |
Correct |
216 ms |
41580 KB |
Output is correct |
37 |
Correct |
249 ms |
41708 KB |
Output is correct |
38 |
Correct |
427 ms |
110188 KB |
Output is correct |
39 |
Correct |
507 ms |
130796 KB |
Output is correct |
40 |
Correct |
370 ms |
87836 KB |
Output is correct |
41 |
Correct |
472 ms |
129132 KB |
Output is correct |
42 |
Correct |
211 ms |
42732 KB |
Output is correct |
43 |
Correct |
213 ms |
42732 KB |
Output is correct |
44 |
Correct |
293 ms |
66412 KB |
Output is correct |
45 |
Correct |
300 ms |
66576 KB |
Output is correct |
46 |
Correct |
296 ms |
66796 KB |
Output is correct |
47 |
Correct |
213 ms |
42860 KB |
Output is correct |
48 |
Correct |
210 ms |
42604 KB |
Output is correct |
49 |
Correct |
289 ms |
66412 KB |
Output is correct |
50 |
Correct |
326 ms |
66540 KB |
Output is correct |
51 |
Correct |
300 ms |
66796 KB |
Output is correct |
52 |
Correct |
306 ms |
65644 KB |
Output is correct |
53 |
Correct |
376 ms |
88188 KB |
Output is correct |
54 |
Correct |
1407 ms |
110976 KB |
Output is correct |
55 |
Correct |
355 ms |
88428 KB |
Output is correct |