//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[500000][2];
vector<int> inxs[500000];
int seg[2000001][2] , seginf[2000001][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 |
9 ms |
14940 KB |
Output is correct |
2 |
Correct |
9 ms |
14940 KB |
Output is correct |
3 |
Correct |
9 ms |
15100 KB |
Output is correct |
4 |
Correct |
6 ms |
14940 KB |
Output is correct |
5 |
Correct |
6 ms |
14832 KB |
Output is correct |
6 |
Correct |
6 ms |
14940 KB |
Output is correct |
7 |
Correct |
7 ms |
15192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
14940 KB |
Output is correct |
2 |
Correct |
9 ms |
14940 KB |
Output is correct |
3 |
Correct |
9 ms |
15100 KB |
Output is correct |
4 |
Correct |
6 ms |
14940 KB |
Output is correct |
5 |
Correct |
6 ms |
14832 KB |
Output is correct |
6 |
Correct |
6 ms |
14940 KB |
Output is correct |
7 |
Correct |
7 ms |
15192 KB |
Output is correct |
8 |
Correct |
178 ms |
20564 KB |
Output is correct |
9 |
Correct |
168 ms |
20672 KB |
Output is correct |
10 |
Correct |
211 ms |
20988 KB |
Output is correct |
11 |
Correct |
188 ms |
21460 KB |
Output is correct |
12 |
Correct |
153 ms |
20232 KB |
Output is correct |
13 |
Correct |
147 ms |
20904 KB |
Output is correct |
14 |
Correct |
136 ms |
20816 KB |
Output is correct |
15 |
Correct |
135 ms |
20888 KB |
Output is correct |
16 |
Correct |
115 ms |
20596 KB |
Output is correct |
17 |
Correct |
139 ms |
20816 KB |
Output is correct |
18 |
Correct |
140 ms |
20816 KB |
Output is correct |
19 |
Correct |
138 ms |
20816 KB |
Output is correct |
20 |
Correct |
127 ms |
20892 KB |
Output is correct |
21 |
Correct |
142 ms |
20608 KB |
Output is correct |
22 |
Correct |
135 ms |
20852 KB |
Output is correct |
23 |
Correct |
125 ms |
20560 KB |
Output is correct |
24 |
Correct |
148 ms |
20768 KB |
Output is correct |
25 |
Correct |
123 ms |
20612 KB |
Output is correct |
26 |
Correct |
124 ms |
20564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
353 ms |
32308 KB |
Output is correct |
2 |
Correct |
342 ms |
31824 KB |
Output is correct |
3 |
Correct |
351 ms |
31780 KB |
Output is correct |
4 |
Correct |
349 ms |
32336 KB |
Output is correct |
5 |
Correct |
353 ms |
32620 KB |
Output is correct |
6 |
Correct |
231 ms |
30492 KB |
Output is correct |
7 |
Correct |
254 ms |
30400 KB |
Output is correct |
8 |
Correct |
206 ms |
30288 KB |
Output is correct |
9 |
Correct |
210 ms |
30292 KB |
Output is correct |
10 |
Correct |
203 ms |
30188 KB |
Output is correct |
11 |
Correct |
203 ms |
30208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
14940 KB |
Output is correct |
2 |
Correct |
9 ms |
14940 KB |
Output is correct |
3 |
Correct |
9 ms |
15100 KB |
Output is correct |
4 |
Correct |
6 ms |
14940 KB |
Output is correct |
5 |
Correct |
6 ms |
14832 KB |
Output is correct |
6 |
Correct |
6 ms |
14940 KB |
Output is correct |
7 |
Correct |
7 ms |
15192 KB |
Output is correct |
8 |
Correct |
178 ms |
20564 KB |
Output is correct |
9 |
Correct |
168 ms |
20672 KB |
Output is correct |
10 |
Correct |
211 ms |
20988 KB |
Output is correct |
11 |
Correct |
188 ms |
21460 KB |
Output is correct |
12 |
Correct |
153 ms |
20232 KB |
Output is correct |
13 |
Correct |
147 ms |
20904 KB |
Output is correct |
14 |
Correct |
136 ms |
20816 KB |
Output is correct |
15 |
Correct |
135 ms |
20888 KB |
Output is correct |
16 |
Correct |
115 ms |
20596 KB |
Output is correct |
17 |
Correct |
139 ms |
20816 KB |
Output is correct |
18 |
Correct |
140 ms |
20816 KB |
Output is correct |
19 |
Correct |
138 ms |
20816 KB |
Output is correct |
20 |
Correct |
127 ms |
20892 KB |
Output is correct |
21 |
Correct |
142 ms |
20608 KB |
Output is correct |
22 |
Correct |
135 ms |
20852 KB |
Output is correct |
23 |
Correct |
125 ms |
20560 KB |
Output is correct |
24 |
Correct |
148 ms |
20768 KB |
Output is correct |
25 |
Correct |
123 ms |
20612 KB |
Output is correct |
26 |
Correct |
124 ms |
20564 KB |
Output is correct |
27 |
Correct |
353 ms |
32308 KB |
Output is correct |
28 |
Correct |
342 ms |
31824 KB |
Output is correct |
29 |
Correct |
351 ms |
31780 KB |
Output is correct |
30 |
Correct |
349 ms |
32336 KB |
Output is correct |
31 |
Correct |
353 ms |
32620 KB |
Output is correct |
32 |
Correct |
231 ms |
30492 KB |
Output is correct |
33 |
Correct |
254 ms |
30400 KB |
Output is correct |
34 |
Correct |
206 ms |
30288 KB |
Output is correct |
35 |
Correct |
210 ms |
30292 KB |
Output is correct |
36 |
Correct |
203 ms |
30188 KB |
Output is correct |
37 |
Correct |
203 ms |
30208 KB |
Output is correct |
38 |
Correct |
2432 ms |
50548 KB |
Output is correct |
39 |
Runtime error |
126 ms |
44240 KB |
Execution killed with signal 11 |
40 |
Halted |
0 ms |
0 KB |
- |