// High above the clouds there is a rainbow...
#include<bits/stdc++.h>
#define F first
#define S second
#define PB push_back
#define sz(s) int((s).size())
#define bit(n,k) (((n)>>(k))&1)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int maxn=5e5+10,inf=1e9;
int c[maxn];
vector<int> v[maxn];
int lst[maxn], aft[maxn],bef[maxn], L[maxn], R[maxn], n;
struct Segmentf{
int mx[4*maxn], mn[4*maxn];
void build(int *arr,int *arr2,int l=0,int r=n+2,int id=1){
if(r-l==1){
mx[id]= arr[l];
mn[id]= arr2[l];
return;
}
int mid=(l+r)>>1;
build(arr,arr2,l,mid,2*id);
build(arr,arr2,mid,r,2*id+1);
mx[id]= max( mx[2*id], mx[2*id+1]);
mn[id]= min( mn[2*id], mn[2*id+1]);
}
int askgr(int f,int s,int x,int l=0,int r=n+2,int id=1){
if(f>=s || l>=r || s<=l || r<=f) return -1;
if(mx[id]<x) return -1;
if(f<=l && r<=s){
if(r-l==1) return l;
int mid=(l+r)>>1;
if(mx[2*id]<x) return askgr(f,s,x,mid,r,2*id+1);
return askgr(f,s,x,l,mid,2*id);
}
int mid=(l+r)>>1;
int num= askgr(f,s,x,l,mid,2*id);
if(num!=-1) return num;
return askgr(f,s,x,mid,r,2*id+1);
}
int askls(int f,int s,int x,int l=0,int r=n+2,int id=1){
if(f>=s || l>=r || s<=l || r<=f) return -1;
if(mn[id]>x) return -1;
if(f<=l && r<=s){
if(r-l==1) return l;
int mid=(l+r)>>1;
if(mn[2*id+1]>x) return askls(f,s,x,l,mid,2*id);
return askls(f,s,x,mid,r,2*id+1);
}
int mid=(l+r)>>1;
int num= askls(f,s,x,mid,r,2*id+1);
if(num!=-1) return num;
return askls(f,s,x,l,mid,2*id);
}
};
Segmentf sf;
struct Segmentv{
int lz[4*maxn];
void build(int l=1,int r=n+1,int id=1){
lz[id]=-inf;
if(r-l==1) return;
int mid=(l+r)>>1;
build(l,mid,2*id);
build(mid,r,2*id+1);
}
void add(int f,int s,int x,int l=1,int r=n+1,int id=1){
if(f>=s || l>=r || s<=l || r<=f) return;
if(f<=l && r<=s) { lz[id]=max(lz[id],x); return; }
int mid=(l+r)>>1;
add(f,s,x,l,mid,2*id);
add(f,s,x,mid,r,2*id+1);
}
int ask(int pos, int l=1,int r=n+1,int id=1){
if(r-l==1) return lz[id];
int mid=(l+r)>>1, num;
if(pos<mid) num= ask(pos,l,mid,2*id);
else num= ask(pos,mid,r,2*id+1);
return max( num, lz[id]);
}
};
Segmentv svl, svr;
int main(){
ios_base::sync_with_stdio(false);cin.tie(0);
cin>>n;
for(int i=1;i<=n-1;i++){
cin>>c[i];
}
for(int i=1;i<=n;i++){
int s; cin>>s;
while(s--){
int x; cin>>x;
v[i].PB(x);
}
}
bef[n+1]=0, aft[0]=n+1;
for(int i=0;i<=n;i++){
lst[i]=n+1;
}
for(int i=n;i>=1;i--){
aft[i]=lst[c[i]];
for(int x:v[i])
lst[x]=i;
}
for(int i=0;i<=n;i++){
lst[i]=0;
}
for(int i=1;i<=n;i++){
bef[i]=lst[ c[i-1] ];
for(int x:v[i])
lst[x]=i;
}
for(int i=1;i<=n;i++){
L[i]=1, R[i]=n;
}
/*
for(int i=1;i<=n;i++){
int id=-1;
for(int j=i-1;j>=bef[i];j--){
if(aft[j]>=i) id=j;
}
if(id!=-1){
for(int j=id+1;j<i;j++)
R[j]= min( R[j], i-1);
}
id=-1;
for(int j=i+1;j<=aft[i];j++){
if(bef[j]<=i) id=j;
}
if(id!=-1){
for(int j=i+1;j<id;j++)
L[j]= max( L[j], i+1);
}
}
*/
sf.build(aft,bef);
svl.build(), svr.build();
for(int i=1;i<=n;i++){
int id= sf.askgr(bef[i],i,i);
if(id!=-1) svr.add(id+1,i, -(i-1) );
id= sf.askls(i+1,aft[i]+1,i);
if(id!=-1) svl.add(i+1,id, i+1 );
}
for(int i=1;i<=n;i++){
L[i]= svl.ask(i);
R[i]=-svr.ask(i);
L[i]=max(L[i],1);
R[i]=min(R[i],n);
}
/* for(int i=1;i<=n;i++){
cout<<"HELLO "<<L[i]<<" "<<R[i]<<endl;
}*/
int q; cin>>q;
while(q--){
int a,b; cin>>a>>b;
cout<<( L[a]<=b && b<=R[a] ? "YES\n" : "NO\n" );
}
return 0;
}
// Deathly mistakes:
// * Read the problem curfully.
// * Check maxn.
// * Overflows.
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
12544 KB |
Output is correct |
2 |
Correct |
15 ms |
12544 KB |
Output is correct |
3 |
Correct |
19 ms |
12672 KB |
Output is correct |
4 |
Correct |
13 ms |
12288 KB |
Output is correct |
5 |
Correct |
14 ms |
12364 KB |
Output is correct |
6 |
Correct |
14 ms |
12416 KB |
Output is correct |
7 |
Correct |
14 ms |
12288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
12544 KB |
Output is correct |
2 |
Correct |
15 ms |
12544 KB |
Output is correct |
3 |
Correct |
19 ms |
12672 KB |
Output is correct |
4 |
Correct |
13 ms |
12288 KB |
Output is correct |
5 |
Correct |
14 ms |
12364 KB |
Output is correct |
6 |
Correct |
14 ms |
12416 KB |
Output is correct |
7 |
Correct |
14 ms |
12288 KB |
Output is correct |
8 |
Correct |
104 ms |
13828 KB |
Output is correct |
9 |
Correct |
103 ms |
13820 KB |
Output is correct |
10 |
Correct |
107 ms |
14072 KB |
Output is correct |
11 |
Correct |
106 ms |
14456 KB |
Output is correct |
12 |
Correct |
98 ms |
13968 KB |
Output is correct |
13 |
Correct |
133 ms |
14172 KB |
Output is correct |
14 |
Correct |
105 ms |
14048 KB |
Output is correct |
15 |
Correct |
102 ms |
14072 KB |
Output is correct |
16 |
Correct |
101 ms |
14312 KB |
Output is correct |
17 |
Correct |
104 ms |
14000 KB |
Output is correct |
18 |
Correct |
109 ms |
14072 KB |
Output is correct |
19 |
Correct |
102 ms |
14076 KB |
Output is correct |
20 |
Correct |
104 ms |
14200 KB |
Output is correct |
21 |
Correct |
102 ms |
14328 KB |
Output is correct |
22 |
Correct |
100 ms |
14048 KB |
Output is correct |
23 |
Correct |
103 ms |
13944 KB |
Output is correct |
24 |
Correct |
101 ms |
13944 KB |
Output is correct |
25 |
Correct |
103 ms |
13908 KB |
Output is correct |
26 |
Correct |
102 ms |
13816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
248 ms |
24164 KB |
Output is correct |
2 |
Correct |
231 ms |
24056 KB |
Output is correct |
3 |
Correct |
225 ms |
24056 KB |
Output is correct |
4 |
Correct |
239 ms |
24024 KB |
Output is correct |
5 |
Correct |
245 ms |
24136 KB |
Output is correct |
6 |
Correct |
192 ms |
23688 KB |
Output is correct |
7 |
Correct |
193 ms |
23672 KB |
Output is correct |
8 |
Correct |
191 ms |
23672 KB |
Output is correct |
9 |
Correct |
188 ms |
23872 KB |
Output is correct |
10 |
Correct |
183 ms |
23672 KB |
Output is correct |
11 |
Correct |
182 ms |
23800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
12544 KB |
Output is correct |
2 |
Correct |
15 ms |
12544 KB |
Output is correct |
3 |
Correct |
19 ms |
12672 KB |
Output is correct |
4 |
Correct |
13 ms |
12288 KB |
Output is correct |
5 |
Correct |
14 ms |
12364 KB |
Output is correct |
6 |
Correct |
14 ms |
12416 KB |
Output is correct |
7 |
Correct |
14 ms |
12288 KB |
Output is correct |
8 |
Correct |
104 ms |
13828 KB |
Output is correct |
9 |
Correct |
103 ms |
13820 KB |
Output is correct |
10 |
Correct |
107 ms |
14072 KB |
Output is correct |
11 |
Correct |
106 ms |
14456 KB |
Output is correct |
12 |
Correct |
98 ms |
13968 KB |
Output is correct |
13 |
Correct |
133 ms |
14172 KB |
Output is correct |
14 |
Correct |
105 ms |
14048 KB |
Output is correct |
15 |
Correct |
102 ms |
14072 KB |
Output is correct |
16 |
Correct |
101 ms |
14312 KB |
Output is correct |
17 |
Correct |
104 ms |
14000 KB |
Output is correct |
18 |
Correct |
109 ms |
14072 KB |
Output is correct |
19 |
Correct |
102 ms |
14076 KB |
Output is correct |
20 |
Correct |
104 ms |
14200 KB |
Output is correct |
21 |
Correct |
102 ms |
14328 KB |
Output is correct |
22 |
Correct |
100 ms |
14048 KB |
Output is correct |
23 |
Correct |
103 ms |
13944 KB |
Output is correct |
24 |
Correct |
101 ms |
13944 KB |
Output is correct |
25 |
Correct |
103 ms |
13908 KB |
Output is correct |
26 |
Correct |
102 ms |
13816 KB |
Output is correct |
27 |
Correct |
248 ms |
24164 KB |
Output is correct |
28 |
Correct |
231 ms |
24056 KB |
Output is correct |
29 |
Correct |
225 ms |
24056 KB |
Output is correct |
30 |
Correct |
239 ms |
24024 KB |
Output is correct |
31 |
Correct |
245 ms |
24136 KB |
Output is correct |
32 |
Correct |
192 ms |
23688 KB |
Output is correct |
33 |
Correct |
193 ms |
23672 KB |
Output is correct |
34 |
Correct |
191 ms |
23672 KB |
Output is correct |
35 |
Correct |
188 ms |
23872 KB |
Output is correct |
36 |
Correct |
183 ms |
23672 KB |
Output is correct |
37 |
Correct |
182 ms |
23800 KB |
Output is correct |
38 |
Correct |
774 ms |
51960 KB |
Output is correct |
39 |
Correct |
875 ms |
57456 KB |
Output is correct |
40 |
Correct |
564 ms |
46588 KB |
Output is correct |
41 |
Correct |
521 ms |
57976 KB |
Output is correct |
42 |
Correct |
212 ms |
23448 KB |
Output is correct |
43 |
Correct |
197 ms |
23692 KB |
Output is correct |
44 |
Correct |
290 ms |
33016 KB |
Output is correct |
45 |
Correct |
306 ms |
33068 KB |
Output is correct |
46 |
Correct |
290 ms |
33016 KB |
Output is correct |
47 |
Correct |
184 ms |
23480 KB |
Output is correct |
48 |
Correct |
185 ms |
23544 KB |
Output is correct |
49 |
Correct |
279 ms |
33016 KB |
Output is correct |
50 |
Correct |
288 ms |
33056 KB |
Output is correct |
51 |
Correct |
299 ms |
32988 KB |
Output is correct |
52 |
Correct |
459 ms |
32820 KB |
Output is correct |
53 |
Correct |
471 ms |
46812 KB |
Output is correct |
54 |
Correct |
575 ms |
51964 KB |
Output is correct |
55 |
Correct |
484 ms |
46580 KB |
Output is correct |