#include<bits/stdc++.h>
using namespace std;
const int maxn=200000+10;
int fl[maxn],fr[maxn],dpl[maxn],kaf=(1<<19),dpr[maxn],dp[maxn],inf=1e8,n;
pair<int,int>all[maxn];
vector<int>adj[maxn];
struct segment{
pair<int,int>seg[(1<<20)];
segment(){
for(int i=0;i<(1<<20);i++){
seg[i]=make_pair(-inf,-inf);
}
}
void clear(){
for(int i=0;i<(1<<20);i++){
seg[i]=make_pair(-inf,-inf);
}
}
void ins(int i,pair<int,int>w){
i+=kaf;
seg[i]=w;
i>>=1;
while(i>0){
seg[i]=max(seg[(i<<1)],seg[(i<<1)^1]);
i>>=1;
}
}
pair<int,int> pors(int i,int l,int r,int tl,int tr){
if(l>r||l>tr||r<tl||tl>tr){
return make_pair(-inf,-inf);
}
if(l>=tl&&r<=tr){
return seg[i];
}
int m=(l+r)>>1;
return max(pors((i<<1),l,m,tl,tr),pors((i<<1)^1,m+1,r,tl,tr));
}
}seg;
void vorod(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>all[i].first>>all[i].second;
}
}
void calfr(int ind){
fr[ind]=seg.pors(1,0,kaf-1,all[ind].first,all[ind].second).second;
if(all[fr[ind]].second==all[ind].second){
fr[ind]=-1;
}else{
adj[fr[ind]].push_back(ind);
}
}
void calr(){
seg.clear();
for(int i=1;i<=n;i++){
seg.ins(i,make_pair(all[i].second,i));
}
for(int i=1;i<=n;i++){
dpr[i]=inf;
calfr(i);
}
vector<pair<int,int>>v;
for(int i=1;i<=n;i++){
v.push_back(make_pair(all[i].second,i));
}
sort(v.rbegin(),v.rend());
for(int i=0;i<n;i++){
int u=v[i].second;
if(v[i].first==n){
dpr[u]=0;
continue;
}
if(fr[u]==-1){
continue;
}
dpr[u]=dpr[fr[u]]+1;
}
}
void calfl(int ind){
fl[ind]=-seg.pors(1,0,kaf-1,all[ind].first,all[ind].second).second;
if(all[fl[ind]].first==all[ind].first){
fl[ind]=-1;
}else{
adj[fl[ind]].push_back(ind);
}
}
void call(){
seg.clear();
for(int i=1;i<=n;i++){
seg.ins(i,make_pair(-all[i].first,-i));
}
for(int i=1;i<=n;i++){
calfl(i);
dpl[i]=inf;
}
vector<pair<int,int>>v;
for(int i=1;i<=n;i++){
v.push_back(make_pair(all[i].first,i));
}
sort(v.begin(),v.end());
for(int i=0;i<n;i++){
int u=v[i].second;
if(v[i].first==1){
dpl[u]=0;
continue;
}
if(fl[u]==-1){
continue;
}
dpl[u]=dpl[fl[u]]+1;
}
}
void calres(){
set<pair<int,int>>st;
vector<int>vis(n+2);
for(int i=1;i<=n;i++){
dp[i]=dpr[i]+dpl[i]+1;
st.insert(make_pair(dp[i],i));
}
while((int)st.size()>0){
auto x=*st.begin();
st.erase(x);
if(vis[x.second]==1){
continue;
}
vis[x.second]=1;
dp[x.second]=x.first;
for(auto y:adj[x.second]){
st.insert(make_pair(x.first+1,y));
}
}
vector<pair<int,int>>v;
for(int i=1;i<=n;i++){
v.push_back(make_pair(all[i].second-all[i].first+1,i));
}
sort(v.rbegin(),v.rend());
for(int asd=0;asd<10;asd++){
seg.clear();
for(int i=0;i<n;i++){
int u=v[i].second;
seg.ins(u,make_pair(-(dp[u]-1),u));
int z=-seg.pors(1,0,kaf-1,all[u].first,all[u].second).first+1;
if(asd>2&&z!=dp[u]){
assert(0);
}
dp[u]=min(dp[u],z);
seg.ins(u,make_pair(-dp[u],u));
// for(int j=all[u].first;j<=all[u].second;j++){
// dp[u]=min(dp[u],dp[j]+(j!=u));
// }
}
}
}
void pre(){
call();
calr();
calres();
}
void khor(){
int q;
cin>>q;
for(int i=1;i<=q;i++){
int x;
cin>>x;
if(dp[x]>=inf/2){
cout<<-1<<"\n";
}else{
cout<<dp[x]<<"\n";
}
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// freopen("inp.txt","r",stdin);
vorod();
pre();
khor();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
16988 KB |
Output is correct |
2 |
Correct |
12 ms |
17240 KB |
Output is correct |
3 |
Correct |
11 ms |
16988 KB |
Output is correct |
4 |
Correct |
1463 ms |
32292 KB |
Output is correct |
5 |
Correct |
1096 ms |
32804 KB |
Output is correct |
6 |
Correct |
965 ms |
35252 KB |
Output is correct |
7 |
Correct |
1077 ms |
28696 KB |
Output is correct |
8 |
Correct |
908 ms |
26304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
16988 KB |
Output is correct |
2 |
Correct |
12 ms |
17036 KB |
Output is correct |
3 |
Correct |
11 ms |
16984 KB |
Output is correct |
4 |
Correct |
12 ms |
16988 KB |
Output is correct |
5 |
Correct |
12 ms |
16988 KB |
Output is correct |
6 |
Correct |
11 ms |
16988 KB |
Output is correct |
7 |
Correct |
13 ms |
17040 KB |
Output is correct |
8 |
Correct |
16 ms |
16988 KB |
Output is correct |
9 |
Correct |
12 ms |
17040 KB |
Output is correct |
10 |
Correct |
12 ms |
16988 KB |
Output is correct |
11 |
Correct |
14 ms |
17064 KB |
Output is correct |
12 |
Correct |
13 ms |
16988 KB |
Output is correct |
13 |
Correct |
13 ms |
16984 KB |
Output is correct |
14 |
Correct |
13 ms |
16988 KB |
Output is correct |
15 |
Correct |
13 ms |
17040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
16988 KB |
Output is correct |
2 |
Correct |
12 ms |
17036 KB |
Output is correct |
3 |
Correct |
11 ms |
16984 KB |
Output is correct |
4 |
Correct |
12 ms |
16988 KB |
Output is correct |
5 |
Correct |
12 ms |
16988 KB |
Output is correct |
6 |
Correct |
11 ms |
16988 KB |
Output is correct |
7 |
Correct |
13 ms |
17040 KB |
Output is correct |
8 |
Correct |
16 ms |
16988 KB |
Output is correct |
9 |
Correct |
12 ms |
17040 KB |
Output is correct |
10 |
Correct |
12 ms |
16988 KB |
Output is correct |
11 |
Correct |
14 ms |
17064 KB |
Output is correct |
12 |
Correct |
13 ms |
16988 KB |
Output is correct |
13 |
Correct |
13 ms |
16984 KB |
Output is correct |
14 |
Correct |
13 ms |
16988 KB |
Output is correct |
15 |
Correct |
13 ms |
17040 KB |
Output is correct |
16 |
Correct |
25 ms |
17244 KB |
Output is correct |
17 |
Correct |
25 ms |
17028 KB |
Output is correct |
18 |
Correct |
31 ms |
16988 KB |
Output is correct |
19 |
Correct |
24 ms |
16984 KB |
Output is correct |
20 |
Correct |
26 ms |
17500 KB |
Output is correct |
21 |
Correct |
22 ms |
17244 KB |
Output is correct |
22 |
Correct |
24 ms |
16988 KB |
Output is correct |
23 |
Correct |
22 ms |
17244 KB |
Output is correct |
24 |
Correct |
24 ms |
16988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
16988 KB |
Output is correct |
2 |
Correct |
12 ms |
17036 KB |
Output is correct |
3 |
Correct |
11 ms |
16984 KB |
Output is correct |
4 |
Correct |
12 ms |
16988 KB |
Output is correct |
5 |
Correct |
12 ms |
16988 KB |
Output is correct |
6 |
Correct |
11 ms |
16988 KB |
Output is correct |
7 |
Correct |
13 ms |
17040 KB |
Output is correct |
8 |
Correct |
16 ms |
16988 KB |
Output is correct |
9 |
Correct |
12 ms |
17040 KB |
Output is correct |
10 |
Correct |
12 ms |
16988 KB |
Output is correct |
11 |
Correct |
14 ms |
17064 KB |
Output is correct |
12 |
Correct |
13 ms |
16988 KB |
Output is correct |
13 |
Correct |
13 ms |
16984 KB |
Output is correct |
14 |
Correct |
13 ms |
16988 KB |
Output is correct |
15 |
Correct |
13 ms |
17040 KB |
Output is correct |
16 |
Correct |
25 ms |
17244 KB |
Output is correct |
17 |
Correct |
25 ms |
17028 KB |
Output is correct |
18 |
Correct |
31 ms |
16988 KB |
Output is correct |
19 |
Correct |
24 ms |
16984 KB |
Output is correct |
20 |
Correct |
26 ms |
17500 KB |
Output is correct |
21 |
Correct |
22 ms |
17244 KB |
Output is correct |
22 |
Correct |
24 ms |
16988 KB |
Output is correct |
23 |
Correct |
22 ms |
17244 KB |
Output is correct |
24 |
Correct |
24 ms |
16988 KB |
Output is correct |
25 |
Correct |
11 ms |
16988 KB |
Output is correct |
26 |
Correct |
12 ms |
16988 KB |
Output is correct |
27 |
Correct |
26 ms |
17244 KB |
Output is correct |
28 |
Correct |
27 ms |
17244 KB |
Output is correct |
29 |
Correct |
23 ms |
17240 KB |
Output is correct |
30 |
Correct |
22 ms |
17268 KB |
Output is correct |
31 |
Correct |
23 ms |
17260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
16988 KB |
Output is correct |
2 |
Correct |
12 ms |
17240 KB |
Output is correct |
3 |
Correct |
11 ms |
16988 KB |
Output is correct |
4 |
Correct |
1463 ms |
32292 KB |
Output is correct |
5 |
Correct |
1096 ms |
32804 KB |
Output is correct |
6 |
Correct |
965 ms |
35252 KB |
Output is correct |
7 |
Correct |
1077 ms |
28696 KB |
Output is correct |
8 |
Correct |
908 ms |
26304 KB |
Output is correct |
9 |
Correct |
11 ms |
16988 KB |
Output is correct |
10 |
Correct |
12 ms |
17036 KB |
Output is correct |
11 |
Correct |
11 ms |
16984 KB |
Output is correct |
12 |
Correct |
12 ms |
16988 KB |
Output is correct |
13 |
Correct |
12 ms |
16988 KB |
Output is correct |
14 |
Correct |
11 ms |
16988 KB |
Output is correct |
15 |
Correct |
13 ms |
17040 KB |
Output is correct |
16 |
Correct |
16 ms |
16988 KB |
Output is correct |
17 |
Correct |
12 ms |
17040 KB |
Output is correct |
18 |
Correct |
12 ms |
16988 KB |
Output is correct |
19 |
Correct |
14 ms |
17064 KB |
Output is correct |
20 |
Correct |
13 ms |
16988 KB |
Output is correct |
21 |
Correct |
13 ms |
16984 KB |
Output is correct |
22 |
Correct |
13 ms |
16988 KB |
Output is correct |
23 |
Correct |
13 ms |
17040 KB |
Output is correct |
24 |
Correct |
25 ms |
17244 KB |
Output is correct |
25 |
Correct |
25 ms |
17028 KB |
Output is correct |
26 |
Correct |
31 ms |
16988 KB |
Output is correct |
27 |
Correct |
24 ms |
16984 KB |
Output is correct |
28 |
Correct |
26 ms |
17500 KB |
Output is correct |
29 |
Correct |
22 ms |
17244 KB |
Output is correct |
30 |
Correct |
24 ms |
16988 KB |
Output is correct |
31 |
Correct |
22 ms |
17244 KB |
Output is correct |
32 |
Correct |
24 ms |
16988 KB |
Output is correct |
33 |
Correct |
11 ms |
16988 KB |
Output is correct |
34 |
Correct |
12 ms |
16988 KB |
Output is correct |
35 |
Correct |
26 ms |
17244 KB |
Output is correct |
36 |
Correct |
27 ms |
17244 KB |
Output is correct |
37 |
Correct |
23 ms |
17240 KB |
Output is correct |
38 |
Correct |
22 ms |
17268 KB |
Output is correct |
39 |
Correct |
23 ms |
17260 KB |
Output is correct |
40 |
Incorrect |
1501 ms |
34812 KB |
Output isn't correct |
41 |
Halted |
0 ms |
0 KB |
- |