#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
//#include<bits/extc++.h>
//__gnu_pbds
const int N = 200005;
struct segtree{
vector<int> minl;
vector<int> maxr;
int n;
void init(int S){
minl.resize(4*S,1e9);
maxr.resize(4*S,-1e9);
n = S;
}
void set(int ind,int l,int r,pair<int,int> v,int t){
if(r-l==1){
minl[t] = min(minl[t],v.first);
maxr[t] = max(maxr[t],v.second);
return;
}
int mid = (l+r)/2;
if(ind<mid) set(ind,l,mid,v,2*t);
if(ind>=mid) set(ind,mid,r,v,2*t+1);
minl[t] = min(minl[2*t],minl[2*t+1]);
maxr[t] = max(maxr[2*t],maxr[2*t+1]);
}
pair<int,int> result(int l,int r,int L,int R,int t){
if(l==L && r==R) return {minl[t],maxr[t]};
int M = (L+R)/2;
if(r<=M) return result(l,r,L,M,2*t);
if(l>=M) return result(l,r,M,R,2*t+1);
pair<int,int> ff = result(l,M,L,M,2*t);
pair<int,int> ss = result(M,r,M,R,2*t+1);
return {min(ff.first,ss.first),max(ff.second,ss.second)};
}
};
int main(){
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
int n;cin>>n;
segtree seg[20];
for(int i=0;i<20;i++) seg[i].init(n);
for(int i=1;i<=n;i++){
int l,r;cin>>l>>r;
seg[0].set(i,1,n+1,make_pair(l,r),1);
}
for(int j=1;j<20;j++){
for(int i=1;i<=n;i++){
pair<int,int> ran = seg[j-1].result(i,i+1,1,n+1,1);
seg[j].set(i,1,n+1,seg[j-1].result(ran.first,ran.second+1,1,n+1,1),1);
}
}
int q;cin>>q;
while(q--){
int x;cin>>x;
pair<int,int> ran = seg[19].result(x,x+1,1,n+1,1);
if(ran.first!=1 || ran.second!=n){
cout<<-1<<"\n";
continue;
}
pair<int,int> re = {x,x};
int ans = 0;
for(int j=19;j>=0;j--){
pair<int,int> ra = seg[j].result(re.first,re.second+1,1,n+1,1);
if(ra.first!=1 || ra.second!=n){
re = ra;
ans+=(1<<j);
}
}
cout<<ans+1<<"\n" ;
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
766 ms |
123216 KB |
Output is correct |
5 |
Correct |
991 ms |
124752 KB |
Output is correct |
6 |
Correct |
1111 ms |
126032 KB |
Output is correct |
7 |
Correct |
732 ms |
122600 KB |
Output is correct |
8 |
Correct |
544 ms |
98640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
766 ms |
123216 KB |
Output is correct |
5 |
Correct |
991 ms |
124752 KB |
Output is correct |
6 |
Correct |
1111 ms |
126032 KB |
Output is correct |
7 |
Correct |
732 ms |
122600 KB |
Output is correct |
8 |
Correct |
544 ms |
98640 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |