#include <bits/stdc++.h>
using namespace std;
#define LCBorz ios_base::sync_with_stdio(false); cin.tie(0);
#define all(x) x.begin(), x.end()
#define endl '\n'
const int K=18;
const int INF=1e9;
const int mod=998244353;
struct sparse_table_min{
vector<int> v[K];
void init(vector<int> &a){
int n=a.size();
for(int i=0;i<K;i++){
v[i].resize(n);
}
for(int i=0;i<n;i++){
v[0][i]=a[i];
}
for(int j=1;j<K;j++){
int p=(1<<j);
for(int i=0;i+p<=n;i++){
v[j][i]=min(v[j-1][i],v[j-1][i+(p>>1)]);
}
}
}
int ask(int l,int r){
int p=__lg(r-l+1);
return min(v[p][l],v[p][r-(1<<p)+1]);
}
};
struct sparse_table_max{
vector<int> v[K];
void init(vector<int> &a){
int n=a.size();
for(int i=0;i<K;i++){
v[i].resize(n);
}
for(int i=0;i<n;i++){
v[0][i]=a[i];
}
for(int j=1;j<K;j++){
int p=(1<<j);
for(int i=0;i+p<=n;i++){
v[j][i]=max(v[j-1][i],v[j-1][i+(p>>1)]);
}
}
}
int ask(int l,int r){
int p=__lg(r-l+1);
return max(v[p][l],v[p][r-(1<<p)+1]);
}
};
void solve(){
int n;cin>>n;
vector<int> b(n+1),c(n+1),b1(n+1),c1(n+1);
for(int i=1;i<=n;i++){
cin>>b[i]>>c[i];
b1[i]=b[i];
c1[i]=c[i];
}
vector<sparse_table_min> st1(K);
vector<sparse_table_max> st2(K);
for(int i=0;i<K;i++){
st1[i].init(b);
st2[i].init(c);
for(int j=1;j<=n;j++){
int l=b[j],r=c[j];
b[j]=st1[i].ask(l,r);
c[j]=st2[i].ask(l,r);
}
}
vector<int> ans(n+1);
for(int i=1;i<=n;i++){
if(b[i]!=1||c[i]!=n){
ans[i]=-1;
continue;
}
int l=b1[i],r=c1[i];
for(int j=K-1;j>=0;j--){
int l1=st1[j].ask(l,r);
int r1=st2[j].ask(l,r);
if(r1==n&&l1==1){
continue;
}
l=l1,r=r1;
ans[i]+=(1<<j);
}
if(l==1&&r==n){
ans[i]--;
}
ans[i]+=2;
}
int q;cin>>q;
for(int i=0;i<q;i++){
int p;cin>>p;
cout<<ans[p]<<endl;
}
}
int32_t main() {
LCBorz;
int t=1;//cin>>t;
while(t--){
solve();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
646 ms |
502872 KB |
Output is correct |
5 |
Correct |
349 ms |
509516 KB |
Output is correct |
6 |
Correct |
353 ms |
514940 KB |
Output is correct |
7 |
Correct |
309 ms |
501168 KB |
Output is correct |
8 |
Correct |
237 ms |
404308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Incorrect |
0 ms |
424 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 |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Incorrect |
0 ms |
424 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 |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Incorrect |
0 ms |
424 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
646 ms |
502872 KB |
Output is correct |
5 |
Correct |
349 ms |
509516 KB |
Output is correct |
6 |
Correct |
353 ms |
514940 KB |
Output is correct |
7 |
Correct |
309 ms |
501168 KB |
Output is correct |
8 |
Correct |
237 ms |
404308 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Incorrect |
0 ms |
424 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |