#include<bits/stdc++.h>
using namespace std;
const int maxn=200000+10;
int n,q,vasps[maxn],kaf=(1<<18),vassuf[maxn],tedps[maxn],tedsuf[maxn],fasps[maxn],fassuf[maxn],vis[maxn];
pair<int,int>all[maxn];
vector<int>fps,fsuf;
pair<int,int>dp[maxn];
struct cmp {
bool operator() (pair<int,int> a,pair<int,int> b) const {
if(a.first!=b.first){
return a.first<b.first;
}
if(all[a.second].second!=all[b.second].second){
return all[a.second].second>all[b.second].second;
}
return a<b;
}
};
set<pair<int,int>,cmp>stdij;
struct segment{
set<int>seg[(1<<19)];
void upd(int i,int l,int r,int tl,int tr,int w){
if(l>r||l>tr||r<tl||tl>tr){
return ;
}
if(l>=tl&&r<=tr){
if(w<0){
w=-w;
seg[i].erase(w);
return ;
}
seg[i].insert(w);
return ;
}
int m=(l+r)>>1;
return upd((i<<1),l,m,tl,tr,w),upd((i<<1)^1,m+1,r,tl,tr,w);
}
int pors(int i){
i+=kaf;
while(i>0){
if((int)seg[i].size()>0){
return *seg[i].begin();
}
i>>=1;
}
return -1;
}
}seg;
void upddij(pair<int,int>x){
int wtf=seg.pors(x.second);
while(wtf!=-1){
seg.upd(1,0,kaf-1,all[wtf].first,all[wtf].second,-wtf);
all[wtf].second=max(all[wtf].second,dp[x.second].second);
stdij.insert(make_pair(x.first+1,wtf));
vis[wtf]=1;
wtf=seg.pors(x.second);
}
}
void dij(){
for(int i=1;i<=n;i++){
if(all[i].first==1){
stdij.insert(make_pair(1,i));
vis[i]=1;
seg.upd(1,0,kaf-1,all[i].first,all[i].second,-i);
}
}
while((int)stdij.size()>0){
auto x=*stdij.begin();
stdij.erase(x);
dp[x.second]=make_pair(x.first,all[x.second].second);
upddij(x);
}
}
void vorod(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>all[i].first>>all[i].second;
}
}
void pre(){
set<pair<int,int>>st;
int now=0;
while(true){
int nx=now;
if((int)st.size()>0){
nx=max(nx,(*st.rbegin()).first);
}
if(nx==now){
vasps[now]=1;
st.insert(make_pair(all[now+1].second,now+1));
}else{
fps.push_back((*st.rbegin()).second);
st.clear();
for(int i=now+1;i<=nx;i++){
st.insert(make_pair(all[i].second,i));
}
now=nx;
}
if(now==n){
break;
}
}
st.clear();
now=n+1;
while(true){
int nx=now;
if((int)st.size()>0){
nx=min(nx,(*st.begin()).first);
}
if(nx==now){
vassuf[now]=1;
st.insert(make_pair(all[now-1].first,now-1));
}else{
fsuf.push_back((*st.rbegin()).second);
st.clear();
for(int i=now-1;i>=nx;i--){
st.insert(make_pair(all[i].first,i));
}
now=nx;
}
if(now==1){
break;
}
}
for(int i=1;i<=n;i++){
seg.upd(1,0,kaf-1,all[i].first,all[i].second,i);
vassuf[i]|=vassuf[i-1];
}
for(int i=n;i>=1;i--){
vasps[i]|=vasps[i+1];
}
for(auto x:fps){
tedps[x]++;
}
for(auto x:fsuf){
tedsuf[x]++;
}
for(int i=1;i<=n;i++){
tedsuf[i]+=tedsuf[i-1];
}
for(int i=n;i>=1;i--){
tedps[i]+=tedps[i+1];
}
dij();
}
void khor(){
int q;
cin>>q;
for(int i=0;i<q;i++){
int x;
cin>>x;
if(vis[x]==1&&vasps[all[x].second]==0){
cout<<dp[x].first+tedps[dp[x].second+1]+(dp[x].second!=n)<<"\n";
}else{
cout<<-1<<"\n";
}
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// freopen("inp.txt","r",stdin);
vorod();
pre();
khor();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
33368 KB |
Output is correct |
2 |
Correct |
6 ms |
33372 KB |
Output is correct |
3 |
Correct |
7 ms |
33372 KB |
Output is correct |
4 |
Correct |
1881 ms |
158976 KB |
Output is correct |
5 |
Correct |
245 ms |
71504 KB |
Output is correct |
6 |
Correct |
120 ms |
54052 KB |
Output is correct |
7 |
Correct |
1612 ms |
299288 KB |
Output is correct |
8 |
Correct |
849 ms |
188144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
33624 KB |
Output is correct |
2 |
Correct |
8 ms |
33372 KB |
Output is correct |
3 |
Correct |
7 ms |
33372 KB |
Output is correct |
4 |
Correct |
7 ms |
33464 KB |
Output is correct |
5 |
Correct |
7 ms |
33372 KB |
Output is correct |
6 |
Correct |
10 ms |
33408 KB |
Output is correct |
7 |
Correct |
7 ms |
33372 KB |
Output is correct |
8 |
Correct |
7 ms |
33372 KB |
Output is correct |
9 |
Correct |
7 ms |
33372 KB |
Output is correct |
10 |
Correct |
7 ms |
33372 KB |
Output is correct |
11 |
Correct |
10 ms |
33776 KB |
Output is correct |
12 |
Correct |
8 ms |
33368 KB |
Output is correct |
13 |
Correct |
7 ms |
33372 KB |
Output is correct |
14 |
Correct |
7 ms |
33372 KB |
Output is correct |
15 |
Correct |
7 ms |
33404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
33624 KB |
Output is correct |
2 |
Correct |
8 ms |
33372 KB |
Output is correct |
3 |
Correct |
7 ms |
33372 KB |
Output is correct |
4 |
Correct |
7 ms |
33464 KB |
Output is correct |
5 |
Correct |
7 ms |
33372 KB |
Output is correct |
6 |
Correct |
10 ms |
33408 KB |
Output is correct |
7 |
Correct |
7 ms |
33372 KB |
Output is correct |
8 |
Correct |
7 ms |
33372 KB |
Output is correct |
9 |
Correct |
7 ms |
33372 KB |
Output is correct |
10 |
Correct |
7 ms |
33372 KB |
Output is correct |
11 |
Correct |
10 ms |
33776 KB |
Output is correct |
12 |
Correct |
8 ms |
33368 KB |
Output is correct |
13 |
Correct |
7 ms |
33372 KB |
Output is correct |
14 |
Correct |
7 ms |
33372 KB |
Output is correct |
15 |
Correct |
7 ms |
33404 KB |
Output is correct |
16 |
Correct |
13 ms |
34136 KB |
Output is correct |
17 |
Correct |
10 ms |
33884 KB |
Output is correct |
18 |
Correct |
12 ms |
34748 KB |
Output is correct |
19 |
Correct |
12 ms |
34652 KB |
Output is correct |
20 |
Correct |
8 ms |
33628 KB |
Output is correct |
21 |
Correct |
8 ms |
33628 KB |
Output is correct |
22 |
Correct |
12 ms |
34904 KB |
Output is correct |
23 |
Correct |
10 ms |
34136 KB |
Output is correct |
24 |
Correct |
11 ms |
34140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
33624 KB |
Output is correct |
2 |
Correct |
8 ms |
33372 KB |
Output is correct |
3 |
Correct |
7 ms |
33372 KB |
Output is correct |
4 |
Correct |
7 ms |
33464 KB |
Output is correct |
5 |
Correct |
7 ms |
33372 KB |
Output is correct |
6 |
Correct |
10 ms |
33408 KB |
Output is correct |
7 |
Correct |
7 ms |
33372 KB |
Output is correct |
8 |
Correct |
7 ms |
33372 KB |
Output is correct |
9 |
Correct |
7 ms |
33372 KB |
Output is correct |
10 |
Correct |
7 ms |
33372 KB |
Output is correct |
11 |
Correct |
10 ms |
33776 KB |
Output is correct |
12 |
Correct |
8 ms |
33368 KB |
Output is correct |
13 |
Correct |
7 ms |
33372 KB |
Output is correct |
14 |
Correct |
7 ms |
33372 KB |
Output is correct |
15 |
Correct |
7 ms |
33404 KB |
Output is correct |
16 |
Correct |
13 ms |
34136 KB |
Output is correct |
17 |
Correct |
10 ms |
33884 KB |
Output is correct |
18 |
Correct |
12 ms |
34748 KB |
Output is correct |
19 |
Correct |
12 ms |
34652 KB |
Output is correct |
20 |
Correct |
8 ms |
33628 KB |
Output is correct |
21 |
Correct |
8 ms |
33628 KB |
Output is correct |
22 |
Correct |
12 ms |
34904 KB |
Output is correct |
23 |
Correct |
10 ms |
34136 KB |
Output is correct |
24 |
Correct |
11 ms |
34140 KB |
Output is correct |
25 |
Correct |
6 ms |
33372 KB |
Output is correct |
26 |
Correct |
6 ms |
33372 KB |
Output is correct |
27 |
Incorrect |
12 ms |
34396 KB |
Output isn't correct |
28 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
33368 KB |
Output is correct |
2 |
Correct |
6 ms |
33372 KB |
Output is correct |
3 |
Correct |
7 ms |
33372 KB |
Output is correct |
4 |
Correct |
1881 ms |
158976 KB |
Output is correct |
5 |
Correct |
245 ms |
71504 KB |
Output is correct |
6 |
Correct |
120 ms |
54052 KB |
Output is correct |
7 |
Correct |
1612 ms |
299288 KB |
Output is correct |
8 |
Correct |
849 ms |
188144 KB |
Output is correct |
9 |
Correct |
7 ms |
33624 KB |
Output is correct |
10 |
Correct |
8 ms |
33372 KB |
Output is correct |
11 |
Correct |
7 ms |
33372 KB |
Output is correct |
12 |
Correct |
7 ms |
33464 KB |
Output is correct |
13 |
Correct |
7 ms |
33372 KB |
Output is correct |
14 |
Correct |
10 ms |
33408 KB |
Output is correct |
15 |
Correct |
7 ms |
33372 KB |
Output is correct |
16 |
Correct |
7 ms |
33372 KB |
Output is correct |
17 |
Correct |
7 ms |
33372 KB |
Output is correct |
18 |
Correct |
7 ms |
33372 KB |
Output is correct |
19 |
Correct |
10 ms |
33776 KB |
Output is correct |
20 |
Correct |
8 ms |
33368 KB |
Output is correct |
21 |
Correct |
7 ms |
33372 KB |
Output is correct |
22 |
Correct |
7 ms |
33372 KB |
Output is correct |
23 |
Correct |
7 ms |
33404 KB |
Output is correct |
24 |
Correct |
13 ms |
34136 KB |
Output is correct |
25 |
Correct |
10 ms |
33884 KB |
Output is correct |
26 |
Correct |
12 ms |
34748 KB |
Output is correct |
27 |
Correct |
12 ms |
34652 KB |
Output is correct |
28 |
Correct |
8 ms |
33628 KB |
Output is correct |
29 |
Correct |
8 ms |
33628 KB |
Output is correct |
30 |
Correct |
12 ms |
34904 KB |
Output is correct |
31 |
Correct |
10 ms |
34136 KB |
Output is correct |
32 |
Correct |
11 ms |
34140 KB |
Output is correct |
33 |
Correct |
6 ms |
33372 KB |
Output is correct |
34 |
Correct |
6 ms |
33372 KB |
Output is correct |
35 |
Incorrect |
12 ms |
34396 KB |
Output isn't correct |
36 |
Halted |
0 ms |
0 KB |
- |