#include <bits/stdc++.h>
using namespace std ;
const int MAX = 5e5 + 10 ;
int arr[MAX] ;
int n , q ;
vector<int>v[MAX] ;
int prv[MAX] , nxt[MAX] ;
int L[MAX] , R[MAX] ;
int last[MAX] ;
int table[MAX][20] , lg[MAX] ;
void build()
{
lg[1] = 0 ;
for(int i = 2 ; i <= n ; ++i)
lg[i] = lg[i/2] + 1 ;
for(int i = 1 ; i <= n ; ++i)
table[i][0] = nxt[i] ;
for(int j = 1 ; (1 << j) <= n ; ++j)
{
for(int i = 1 ; i + (1 << j) - 1 <= n ; ++i)
table[i][j] = max(table[i][j-1] , table[i + (1 << (j-1))][j-1]) ;
}
}
int RmaxQ(int l , int r)
{
int k = lg[r-l+1] ;
return max(table[l][k] , table[r - (1 << k) + 1][k]) ;
}
void preprocess()
{
for(int i = 1 ; i <= n ; ++i)
{
prv[i] = last[arr[i-1]] ;
for(auto &x : v[i])
last[x] = i ;
}
for(int i = 0 ; i <= n ; ++i)
last[i] = n+1 ;
for(int i = n ; i >= 1 ; --i)
{
nxt[i] = last[arr[i]] ;
for(auto &x : v[i])
last[x] = i ;
}
build() ;
}
vector<int>Erase[MAX] ;
int main()
{
ios_base::sync_with_stdio(0) ;
cin.tie(0) ;
cin>>n ;
for(int i = 1 ; i <= n-1 ; ++i)
cin>>arr[i] ;
for(int i = 1 ; i <= n ; ++i)
{
int sz ;
cin>>sz ;
v[i].resize(sz) ;
for(auto &x : v[i])
cin>>x ;
}
preprocess() ;
set<int>s ;
s.insert(n) ;
for(int i = n ; i >= 1 ; --i)
{
for(auto &x : Erase[i])
s.erase(x) ;
R[i] = *s.begin() ;
if(prv[i] == 0)
{
s.insert(i-1) ;
continue ;
}
int l = prv[i] , r = i-2 ;
int idx = -1 ;
while(l <= r)
{
int mid = (l + r) >> 1 ;
if(RmaxQ(prv[i] , mid) >= i)
idx = mid , r = mid-1 ;
else
l = mid+1 ;
}
if(idx != -1)
s.insert(i-1) , Erase[idx].push_back(i-1) ;
}
for(int i = 1 ; i <= n ; ++i)
{
L[i] = i ;
int l = 1 , r = i-1 ;
while(l <= r)
{
int mid = (l + r) >> 1 ;
if(RmaxQ(mid , i-1) <= R[i])
L[i] = mid , r = mid-1 ;
else
l = mid+1 ;
}
}
cin>>q ;
while(q--)
{
int x , y ;
cin>>x>>y ;
if(y >= L[x] && y <= R[x])
cout<<"YES\n" ;
else
cout<<"NO\n" ;
}
return 0 ;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
24148 KB |
Output is correct |
2 |
Correct |
12 ms |
24340 KB |
Output is correct |
3 |
Correct |
13 ms |
24612 KB |
Output is correct |
4 |
Correct |
11 ms |
24148 KB |
Output is correct |
5 |
Correct |
11 ms |
24152 KB |
Output is correct |
6 |
Correct |
13 ms |
24168 KB |
Output is correct |
7 |
Correct |
12 ms |
24148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
24148 KB |
Output is correct |
2 |
Correct |
12 ms |
24340 KB |
Output is correct |
3 |
Correct |
13 ms |
24612 KB |
Output is correct |
4 |
Correct |
11 ms |
24148 KB |
Output is correct |
5 |
Correct |
11 ms |
24152 KB |
Output is correct |
6 |
Correct |
13 ms |
24168 KB |
Output is correct |
7 |
Correct |
12 ms |
24148 KB |
Output is correct |
8 |
Correct |
80 ms |
25768 KB |
Output is correct |
9 |
Correct |
75 ms |
25576 KB |
Output is correct |
10 |
Correct |
93 ms |
25984 KB |
Output is correct |
11 |
Correct |
76 ms |
26336 KB |
Output is correct |
12 |
Correct |
75 ms |
25716 KB |
Output is correct |
13 |
Correct |
86 ms |
25832 KB |
Output is correct |
14 |
Correct |
81 ms |
25860 KB |
Output is correct |
15 |
Correct |
72 ms |
25932 KB |
Output is correct |
16 |
Correct |
76 ms |
26052 KB |
Output is correct |
17 |
Correct |
72 ms |
25764 KB |
Output is correct |
18 |
Correct |
71 ms |
25876 KB |
Output is correct |
19 |
Correct |
85 ms |
25864 KB |
Output is correct |
20 |
Correct |
71 ms |
26044 KB |
Output is correct |
21 |
Correct |
84 ms |
26112 KB |
Output is correct |
22 |
Correct |
79 ms |
25888 KB |
Output is correct |
23 |
Correct |
76 ms |
25848 KB |
Output is correct |
24 |
Correct |
77 ms |
25664 KB |
Output is correct |
25 |
Correct |
85 ms |
25676 KB |
Output is correct |
26 |
Correct |
74 ms |
25676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
140 ms |
40568 KB |
Output is correct |
2 |
Correct |
168 ms |
40452 KB |
Output is correct |
3 |
Correct |
145 ms |
40268 KB |
Output is correct |
4 |
Correct |
155 ms |
47860 KB |
Output is correct |
5 |
Correct |
152 ms |
47728 KB |
Output is correct |
6 |
Correct |
122 ms |
46056 KB |
Output is correct |
7 |
Correct |
122 ms |
45656 KB |
Output is correct |
8 |
Correct |
136 ms |
45548 KB |
Output is correct |
9 |
Correct |
123 ms |
45604 KB |
Output is correct |
10 |
Correct |
123 ms |
45532 KB |
Output is correct |
11 |
Correct |
130 ms |
45568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
24148 KB |
Output is correct |
2 |
Correct |
12 ms |
24340 KB |
Output is correct |
3 |
Correct |
13 ms |
24612 KB |
Output is correct |
4 |
Correct |
11 ms |
24148 KB |
Output is correct |
5 |
Correct |
11 ms |
24152 KB |
Output is correct |
6 |
Correct |
13 ms |
24168 KB |
Output is correct |
7 |
Correct |
12 ms |
24148 KB |
Output is correct |
8 |
Correct |
80 ms |
25768 KB |
Output is correct |
9 |
Correct |
75 ms |
25576 KB |
Output is correct |
10 |
Correct |
93 ms |
25984 KB |
Output is correct |
11 |
Correct |
76 ms |
26336 KB |
Output is correct |
12 |
Correct |
75 ms |
25716 KB |
Output is correct |
13 |
Correct |
86 ms |
25832 KB |
Output is correct |
14 |
Correct |
81 ms |
25860 KB |
Output is correct |
15 |
Correct |
72 ms |
25932 KB |
Output is correct |
16 |
Correct |
76 ms |
26052 KB |
Output is correct |
17 |
Correct |
72 ms |
25764 KB |
Output is correct |
18 |
Correct |
71 ms |
25876 KB |
Output is correct |
19 |
Correct |
85 ms |
25864 KB |
Output is correct |
20 |
Correct |
71 ms |
26044 KB |
Output is correct |
21 |
Correct |
84 ms |
26112 KB |
Output is correct |
22 |
Correct |
79 ms |
25888 KB |
Output is correct |
23 |
Correct |
76 ms |
25848 KB |
Output is correct |
24 |
Correct |
77 ms |
25664 KB |
Output is correct |
25 |
Correct |
85 ms |
25676 KB |
Output is correct |
26 |
Correct |
74 ms |
25676 KB |
Output is correct |
27 |
Correct |
140 ms |
40568 KB |
Output is correct |
28 |
Correct |
168 ms |
40452 KB |
Output is correct |
29 |
Correct |
145 ms |
40268 KB |
Output is correct |
30 |
Correct |
155 ms |
47860 KB |
Output is correct |
31 |
Correct |
152 ms |
47728 KB |
Output is correct |
32 |
Correct |
122 ms |
46056 KB |
Output is correct |
33 |
Correct |
122 ms |
45656 KB |
Output is correct |
34 |
Correct |
136 ms |
45548 KB |
Output is correct |
35 |
Correct |
123 ms |
45604 KB |
Output is correct |
36 |
Correct |
123 ms |
45532 KB |
Output is correct |
37 |
Correct |
130 ms |
45568 KB |
Output is correct |
38 |
Correct |
471 ms |
98096 KB |
Output is correct |
39 |
Correct |
513 ms |
112748 KB |
Output is correct |
40 |
Correct |
368 ms |
81492 KB |
Output is correct |
41 |
Correct |
384 ms |
107100 KB |
Output is correct |
42 |
Correct |
131 ms |
47264 KB |
Output is correct |
43 |
Correct |
124 ms |
46880 KB |
Output is correct |
44 |
Correct |
186 ms |
63268 KB |
Output is correct |
45 |
Correct |
187 ms |
63388 KB |
Output is correct |
46 |
Correct |
182 ms |
63460 KB |
Output is correct |
47 |
Correct |
119 ms |
46764 KB |
Output is correct |
48 |
Correct |
129 ms |
46488 KB |
Output is correct |
49 |
Correct |
183 ms |
63028 KB |
Output is correct |
50 |
Correct |
178 ms |
63112 KB |
Output is correct |
51 |
Correct |
179 ms |
63456 KB |
Output is correct |
52 |
Correct |
207 ms |
66556 KB |
Output is correct |
53 |
Correct |
267 ms |
84056 KB |
Output is correct |
54 |
Correct |
328 ms |
102372 KB |
Output is correct |
55 |
Correct |
263 ms |
84688 KB |
Output is correct |