//Be Name KHODA
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
#define pp pop_back
#define lb lower_bound
#define ub upper_bound
#define cl clear
#define bg begin
#define arr(x) array<int , x>
#define endl '\n'
int n , q , in[1000000][2];
vector<int> inxs[1000000];
int seg[3000001][2] , seginf[3000001][2];
void init(){
for(int i = 0 ; i < n ; i++){
if(in[i][0] != -1){
auto itr = lb(inxs[in[i][0]].bg() , inxs[in[i][0]].end() , i);
if(itr == inxs[in[i][0]].bg()) in[i][0] = -1;
else in[i][0] = *(--itr);
}
if(in[i][1] != -1){
auto itr = ub(inxs[in[i][1]].bg() , inxs[in[i][1]].end() , i);
if(itr == inxs[in[i][1]].end()) in[i][1] = n;
else in[i][1] = *(itr);
}
}
}
void buildSeg(int l = 0 , int r = n - 1 , int i = 1){
seginf[i][0] = l , seginf[i][1] = r;
if(l == r){
seg[i][0] = in[l][0];
seg[i][1] = in[l][1];
}else{
int c = (r + l - 1) >> 1;
buildSeg(l , c , i << 1) , buildSeg(c + 1 , r , (i << 1) | 1);
seg[i][0] = min(seg[i << 1][0] , seg[(i << 1) | 1][0]);
seg[i][1] = max(seg[i << 1][1] , seg[(i << 1) | 1][1]);
}
}
int getSeg(int l , int r , int i , int md){
if(seginf[i][0] >= l and seginf[i][1] <= r) return seg[i][md];
if(seginf[i][1] >= l and seginf[i][0] <= r){
if(md == 0) return min(getSeg(l , r , i << 1 , md) , getSeg(l , r , (i << 1) | 1 , md));
return max(getSeg(l , r , i << 1 , md) , getSeg(l , r , (i << 1) | 1 , md));
}
if(md == 0) return n;
return -1;
}
int bsmx(int l , int r){
int i = l , j = r;
l--;
while(r - l - 1 > 1){
int c = (r + l + 1) >> 1;
if(getSeg(i , c , 1 , 1) >= j) r = c;
else l = c - 1;
}
if(r - l - 1 == 1 and getSeg(i , r - 1 , 1 , 1) >= j) return r - 1;
return r;
}
int bsmn(int l , int r){
int i = l , j = r;
r++;
while(r - l - 1 > 1){
int c = (r + l + 1) >> 1;
if(getSeg(c , j , 1 , 0) <= i) l = c - 1;
else r = c;
}
if(r - l - 1 == 1 and getSeg(l + 1 , j , 1 , 0) <= i) return l + 1;
return l;
}
void f(){
buildSeg();
for(int i = 0 ; i < n ; i++){
if(in[i][0] != -1){
in[i][0] = bsmx(in[i][0] , i);
}
if(in[i][1] != n){
in[i][1] = bsmn(i , in[i][1]);
}
}
buildSeg();
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
in[0][0] = -1 , in[n - 1][1] = n;
for(int i = 1 ; i < n ; i++){
int d;
cin >> d;
in[i - 1][1] = d - 1 , in[i][0] = d - 1;
}
for(int i = 0 ; i < n ; i++){
int b;
cin >> b;
for(int j = 0 ; j < b ; j++){
int d;
cin >> d;
inxs[d - 1].pb(i);
}
}
init() , f();
cin >> q;
for(int i = 0 ; i < q ; i++){
int v , u;
cin >> v >> u;
v-- , u--;
if(v < u){
int d = getSeg(v + 1 , u , 1 , 0);
if(d >= v) cout << "YES\n";
else cout << "NO\n";
}else{
int d = getSeg(u , v - 1 , 1 , 1);
if(d <= v) cout << "YES\n";
else cout << "NO\n";
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
29272 KB |
Output is correct |
2 |
Correct |
11 ms |
29276 KB |
Output is correct |
3 |
Correct |
13 ms |
29276 KB |
Output is correct |
4 |
Correct |
10 ms |
29272 KB |
Output is correct |
5 |
Correct |
9 ms |
29276 KB |
Output is correct |
6 |
Correct |
10 ms |
29276 KB |
Output is correct |
7 |
Correct |
11 ms |
29276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
29272 KB |
Output is correct |
2 |
Correct |
11 ms |
29276 KB |
Output is correct |
3 |
Correct |
13 ms |
29276 KB |
Output is correct |
4 |
Correct |
10 ms |
29272 KB |
Output is correct |
5 |
Correct |
9 ms |
29276 KB |
Output is correct |
6 |
Correct |
10 ms |
29276 KB |
Output is correct |
7 |
Correct |
11 ms |
29276 KB |
Output is correct |
8 |
Correct |
169 ms |
30696 KB |
Output is correct |
9 |
Correct |
166 ms |
30520 KB |
Output is correct |
10 |
Correct |
185 ms |
30804 KB |
Output is correct |
11 |
Correct |
186 ms |
30896 KB |
Output is correct |
12 |
Correct |
176 ms |
30940 KB |
Output is correct |
13 |
Correct |
140 ms |
30800 KB |
Output is correct |
14 |
Correct |
172 ms |
30928 KB |
Output is correct |
15 |
Correct |
135 ms |
30844 KB |
Output is correct |
16 |
Correct |
117 ms |
31256 KB |
Output is correct |
17 |
Correct |
136 ms |
30800 KB |
Output is correct |
18 |
Correct |
140 ms |
30988 KB |
Output is correct |
19 |
Correct |
140 ms |
30756 KB |
Output is correct |
20 |
Correct |
125 ms |
31164 KB |
Output is correct |
21 |
Correct |
117 ms |
31056 KB |
Output is correct |
22 |
Correct |
139 ms |
31000 KB |
Output is correct |
23 |
Correct |
125 ms |
30800 KB |
Output is correct |
24 |
Correct |
128 ms |
30804 KB |
Output is correct |
25 |
Correct |
127 ms |
30848 KB |
Output is correct |
26 |
Correct |
126 ms |
30776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
346 ms |
37504 KB |
Output is correct |
2 |
Correct |
331 ms |
36964 KB |
Output is correct |
3 |
Correct |
332 ms |
37076 KB |
Output is correct |
4 |
Correct |
337 ms |
37216 KB |
Output is correct |
5 |
Correct |
363 ms |
37232 KB |
Output is correct |
6 |
Correct |
237 ms |
36436 KB |
Output is correct |
7 |
Correct |
217 ms |
36436 KB |
Output is correct |
8 |
Correct |
207 ms |
36432 KB |
Output is correct |
9 |
Correct |
206 ms |
36472 KB |
Output is correct |
10 |
Correct |
228 ms |
36668 KB |
Output is correct |
11 |
Correct |
207 ms |
36524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
29272 KB |
Output is correct |
2 |
Correct |
11 ms |
29276 KB |
Output is correct |
3 |
Correct |
13 ms |
29276 KB |
Output is correct |
4 |
Correct |
10 ms |
29272 KB |
Output is correct |
5 |
Correct |
9 ms |
29276 KB |
Output is correct |
6 |
Correct |
10 ms |
29276 KB |
Output is correct |
7 |
Correct |
11 ms |
29276 KB |
Output is correct |
8 |
Correct |
169 ms |
30696 KB |
Output is correct |
9 |
Correct |
166 ms |
30520 KB |
Output is correct |
10 |
Correct |
185 ms |
30804 KB |
Output is correct |
11 |
Correct |
186 ms |
30896 KB |
Output is correct |
12 |
Correct |
176 ms |
30940 KB |
Output is correct |
13 |
Correct |
140 ms |
30800 KB |
Output is correct |
14 |
Correct |
172 ms |
30928 KB |
Output is correct |
15 |
Correct |
135 ms |
30844 KB |
Output is correct |
16 |
Correct |
117 ms |
31256 KB |
Output is correct |
17 |
Correct |
136 ms |
30800 KB |
Output is correct |
18 |
Correct |
140 ms |
30988 KB |
Output is correct |
19 |
Correct |
140 ms |
30756 KB |
Output is correct |
20 |
Correct |
125 ms |
31164 KB |
Output is correct |
21 |
Correct |
117 ms |
31056 KB |
Output is correct |
22 |
Correct |
139 ms |
31000 KB |
Output is correct |
23 |
Correct |
125 ms |
30800 KB |
Output is correct |
24 |
Correct |
128 ms |
30804 KB |
Output is correct |
25 |
Correct |
127 ms |
30848 KB |
Output is correct |
26 |
Correct |
126 ms |
30776 KB |
Output is correct |
27 |
Correct |
346 ms |
37504 KB |
Output is correct |
28 |
Correct |
331 ms |
36964 KB |
Output is correct |
29 |
Correct |
332 ms |
37076 KB |
Output is correct |
30 |
Correct |
337 ms |
37216 KB |
Output is correct |
31 |
Correct |
363 ms |
37232 KB |
Output is correct |
32 |
Correct |
237 ms |
36436 KB |
Output is correct |
33 |
Correct |
217 ms |
36436 KB |
Output is correct |
34 |
Correct |
207 ms |
36432 KB |
Output is correct |
35 |
Correct |
206 ms |
36472 KB |
Output is correct |
36 |
Correct |
228 ms |
36668 KB |
Output is correct |
37 |
Correct |
207 ms |
36524 KB |
Output is correct |
38 |
Correct |
2395 ms |
52408 KB |
Output is correct |
39 |
Correct |
2201 ms |
60856 KB |
Output is correct |
40 |
Correct |
1407 ms |
63088 KB |
Output is correct |
41 |
Correct |
660 ms |
63424 KB |
Output is correct |
42 |
Correct |
283 ms |
44020 KB |
Output is correct |
43 |
Correct |
294 ms |
44004 KB |
Output is correct |
44 |
Correct |
387 ms |
54916 KB |
Output is correct |
45 |
Correct |
364 ms |
55088 KB |
Output is correct |
46 |
Correct |
372 ms |
55508 KB |
Output is correct |
47 |
Correct |
269 ms |
44208 KB |
Output is correct |
48 |
Correct |
253 ms |
43692 KB |
Output is correct |
49 |
Correct |
358 ms |
54208 KB |
Output is correct |
50 |
Correct |
345 ms |
54900 KB |
Output is correct |
51 |
Correct |
364 ms |
56048 KB |
Output is correct |
52 |
Correct |
800 ms |
56176 KB |
Output is correct |
53 |
Correct |
1082 ms |
68988 KB |
Output is correct |
54 |
Correct |
1500 ms |
73988 KB |
Output is correct |
55 |
Correct |
1099 ms |
69260 KB |
Output is correct |