#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize ("Ofast")
#define all(x) x.begin() , x.end()
#define sze(x) (ll)(x.size())
#define mp(x , y) make_pair(x , y)
#define wall cerr<<"--------------------------------------"<<endl
typedef long long int ll;
typedef pair<ll , ll> pll;
typedef pair<int , int> pii;
typedef long double db;
typedef pair<pll , ll> plll;
typedef pair<int , pii> piii;
typedef pair<pll , pll> pllll;
const ll maxn = 5e5 + 16 , md = 1e9 + 7 , inf = 2e16;
ll gcd(ll a , ll b){
if(a < b) swap(a , b);
if(b == 0) return a;
return gcd(b , a % b);
}
inline ll tav(ll n , ll k){
ll res = 1;
while(k > 0){
if(k & 1){
res *= n; res %= md;
}
n *= n; n %= md;
k >>= 1;
}
return res;
}
ll l[maxn] , r[maxn] , a[maxn] , fl[maxn] , fr[maxn] , cnt[maxn];
vector<ll> v[maxn];
void dv(ll lx , ll rx){
if(rx - lx == 1){
l[lx] = lx; r[lx] = rx;
return;
}
ll m = (rx + lx) >> 1;
dv(lx , m); dv(m , rx);
ll x , y;
y = m;
for(ll i = m ; i > lx ; ){
i--;
for(auto j : v[i]) cnt[j]++;
while(y < rx){
if(!cnt[a[y]]) break;
for(auto j : v[y]) cnt[j]++;
y++;
}
fr[i] = y; fl[i] = i;
}
for(ll i = lx ; i < m - 1 ; ){
for(auto j : v[i]) cnt[j]--;
i++;
while(y > fr[i]){
y--;
for(auto j : v[y]) cnt[j]--;
}
if(cnt[a[i]]){
fl[i] = fl[i - 1]; fr[i] = fr[i - 1];
}
}
while(y > m - 1){
y--;
for(auto j : v[y]) cnt[j]--;
}
// bool f = false;
// for(ll i = 0 ; i < 10 ; i++){
// f |= cnt[i];
// }
// if(f){
// cout<<lx<<' '<<rx<<'\n';
// }
for(ll i = lx ; i < m ; i++){
if(r[i] == m){
l[i] = fl[i]; r[i] = fr[i];
}
}
x = m - 1;
for(ll i = m - 1 ; i < rx - 1 ; ){
i++;
for(auto j : v[i]) cnt[j]++;
while(x >= lx){
if(!cnt[a[x + 1]]) break;
for(auto j : v[x]) cnt[j]++;
x--;
}
fl[i] = x + 1; fr[i] = i + 1;
}
for(ll i = rx - 1 ; i > m ; ){
for(auto j : v[i]) cnt[j]--;
i--;
while(x < fl[i] - 1){
x++;
for(auto j : v[x]) cnt[j]--;
}
if(cnt[a[i + 1]]){
fl[i] = fl[i + 1]; fr[i] = fr[i + 1];
}
}
while(x < m){
x++;
for(auto j : v[x]) cnt[j]--;
}
for(ll i = m ; i < rx ; i++){
if(l[i] == m){
l[i] = fl[i]; r[i] = fr[i];
}
}
// f = false;
// for(ll i = 0 ; i < 10 ; i++){
// f |= cnt[i];
// }
// if(f){
// cout<<lx<<' '<<rx<<'\n';
// }
return;
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
memset(cnt , 0 , sizeof(cnt));
ll n , q;
cin>>n;
for(ll i = 1 ; i < n ; i++){
cin>>a[i];
}
for(ll i = 0 ; i < n ; i++){
ll k;
cin>>k;
for(ll j = 0 ; j < k ; j++){
ll h;
cin>>h;
v[i].push_back(h);
}
}
dv(0 , n);
// wall;
// for(ll i = 0 ; i < n ; i++){
// cout<<l[i]<<' '<<r[i]<<'\n';
// }
cin>>q;
for(ll e = 0 ; e < q ; e++){
ll v , u;
cin>>v>>u; v--; u--;
if(r[v] > u && l[v] <= u){
cout<<"YES\n";
} else {
cout<<"NO\n";
}
}
return 0;
}
/*
5
2 3 1 3
1 3
1 2
1 1
1 3
1 2
4
1 3
3 1
4 3
2 5
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
16228 KB |
Output is correct |
2 |
Correct |
11 ms |
16196 KB |
Output is correct |
3 |
Correct |
12 ms |
16364 KB |
Output is correct |
4 |
Correct |
11 ms |
16204 KB |
Output is correct |
5 |
Correct |
11 ms |
16180 KB |
Output is correct |
6 |
Correct |
11 ms |
16204 KB |
Output is correct |
7 |
Correct |
10 ms |
16204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
16228 KB |
Output is correct |
2 |
Correct |
11 ms |
16196 KB |
Output is correct |
3 |
Correct |
12 ms |
16364 KB |
Output is correct |
4 |
Correct |
11 ms |
16204 KB |
Output is correct |
5 |
Correct |
11 ms |
16180 KB |
Output is correct |
6 |
Correct |
11 ms |
16204 KB |
Output is correct |
7 |
Correct |
10 ms |
16204 KB |
Output is correct |
8 |
Correct |
94 ms |
22052 KB |
Output is correct |
9 |
Correct |
89 ms |
21956 KB |
Output is correct |
10 |
Correct |
97 ms |
22368 KB |
Output is correct |
11 |
Correct |
104 ms |
22800 KB |
Output is correct |
12 |
Correct |
85 ms |
21604 KB |
Output is correct |
13 |
Correct |
102 ms |
22144 KB |
Output is correct |
14 |
Correct |
90 ms |
22256 KB |
Output is correct |
15 |
Correct |
90 ms |
22244 KB |
Output is correct |
16 |
Correct |
90 ms |
21956 KB |
Output is correct |
17 |
Correct |
89 ms |
22284 KB |
Output is correct |
18 |
Correct |
96 ms |
22324 KB |
Output is correct |
19 |
Correct |
87 ms |
22212 KB |
Output is correct |
20 |
Correct |
95 ms |
22184 KB |
Output is correct |
21 |
Correct |
88 ms |
22004 KB |
Output is correct |
22 |
Correct |
95 ms |
22080 KB |
Output is correct |
23 |
Correct |
92 ms |
22096 KB |
Output is correct |
24 |
Correct |
93 ms |
21964 KB |
Output is correct |
25 |
Correct |
91 ms |
21956 KB |
Output is correct |
26 |
Correct |
92 ms |
21900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
282 ms |
34772 KB |
Output is correct |
2 |
Correct |
252 ms |
34628 KB |
Output is correct |
3 |
Correct |
246 ms |
34120 KB |
Output is correct |
4 |
Correct |
301 ms |
34724 KB |
Output is correct |
5 |
Correct |
272 ms |
34668 KB |
Output is correct |
6 |
Correct |
212 ms |
31432 KB |
Output is correct |
7 |
Correct |
196 ms |
31116 KB |
Output is correct |
8 |
Correct |
216 ms |
31168 KB |
Output is correct |
9 |
Correct |
195 ms |
31112 KB |
Output is correct |
10 |
Correct |
193 ms |
31084 KB |
Output is correct |
11 |
Correct |
214 ms |
30968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
16228 KB |
Output is correct |
2 |
Correct |
11 ms |
16196 KB |
Output is correct |
3 |
Correct |
12 ms |
16364 KB |
Output is correct |
4 |
Correct |
11 ms |
16204 KB |
Output is correct |
5 |
Correct |
11 ms |
16180 KB |
Output is correct |
6 |
Correct |
11 ms |
16204 KB |
Output is correct |
7 |
Correct |
10 ms |
16204 KB |
Output is correct |
8 |
Correct |
94 ms |
22052 KB |
Output is correct |
9 |
Correct |
89 ms |
21956 KB |
Output is correct |
10 |
Correct |
97 ms |
22368 KB |
Output is correct |
11 |
Correct |
104 ms |
22800 KB |
Output is correct |
12 |
Correct |
85 ms |
21604 KB |
Output is correct |
13 |
Correct |
102 ms |
22144 KB |
Output is correct |
14 |
Correct |
90 ms |
22256 KB |
Output is correct |
15 |
Correct |
90 ms |
22244 KB |
Output is correct |
16 |
Correct |
90 ms |
21956 KB |
Output is correct |
17 |
Correct |
89 ms |
22284 KB |
Output is correct |
18 |
Correct |
96 ms |
22324 KB |
Output is correct |
19 |
Correct |
87 ms |
22212 KB |
Output is correct |
20 |
Correct |
95 ms |
22184 KB |
Output is correct |
21 |
Correct |
88 ms |
22004 KB |
Output is correct |
22 |
Correct |
95 ms |
22080 KB |
Output is correct |
23 |
Correct |
92 ms |
22096 KB |
Output is correct |
24 |
Correct |
93 ms |
21964 KB |
Output is correct |
25 |
Correct |
91 ms |
21956 KB |
Output is correct |
26 |
Correct |
92 ms |
21900 KB |
Output is correct |
27 |
Correct |
282 ms |
34772 KB |
Output is correct |
28 |
Correct |
252 ms |
34628 KB |
Output is correct |
29 |
Correct |
246 ms |
34120 KB |
Output is correct |
30 |
Correct |
301 ms |
34724 KB |
Output is correct |
31 |
Correct |
272 ms |
34668 KB |
Output is correct |
32 |
Correct |
212 ms |
31432 KB |
Output is correct |
33 |
Correct |
196 ms |
31116 KB |
Output is correct |
34 |
Correct |
216 ms |
31168 KB |
Output is correct |
35 |
Correct |
195 ms |
31112 KB |
Output is correct |
36 |
Correct |
193 ms |
31084 KB |
Output is correct |
37 |
Correct |
214 ms |
30968 KB |
Output is correct |
38 |
Correct |
389 ms |
57444 KB |
Output is correct |
39 |
Correct |
394 ms |
64012 KB |
Output is correct |
40 |
Correct |
360 ms |
49880 KB |
Output is correct |
41 |
Correct |
507 ms |
62520 KB |
Output is correct |
42 |
Correct |
196 ms |
32128 KB |
Output is correct |
43 |
Correct |
171 ms |
32216 KB |
Output is correct |
44 |
Correct |
265 ms |
41912 KB |
Output is correct |
45 |
Correct |
289 ms |
41952 KB |
Output is correct |
46 |
Correct |
269 ms |
42128 KB |
Output is correct |
47 |
Correct |
231 ms |
32192 KB |
Output is correct |
48 |
Correct |
188 ms |
32124 KB |
Output is correct |
49 |
Correct |
315 ms |
42068 KB |
Output is correct |
50 |
Correct |
283 ms |
41900 KB |
Output is correct |
51 |
Correct |
289 ms |
42276 KB |
Output is correct |
52 |
Correct |
216 ms |
41056 KB |
Output is correct |
53 |
Correct |
280 ms |
50184 KB |
Output is correct |
54 |
Correct |
324 ms |
58532 KB |
Output is correct |
55 |
Correct |
289 ms |
50088 KB |
Output is correct |