#include<bits/stdc++.h>
using namespace std;
const int maxn=200000+10;
int n,q,vasps[maxn],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>st;
void upddij(pair<int,int>x){
for(int i=1;i<=n;i++){
if(vis[i]==1){
continue;
}
if(all[i].first<=x.second&&all[i].second>=x.second){
all[i].second=max(all[i].second,dp[x.second].second);
st.insert(make_pair(x.first+1,i));
vis[i]=1;
}
}
}
void dij(){
for(int i=1;i<=n;i++){
if(all[i].first==1){
st.insert(make_pair(1,i));
vis[i]=1;
}
}
while((int)st.size()>0){
auto x=*st.begin();
st.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++){
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;
int x;
cin>>x;
if(x!=1||q!=1){
assert(0);
}
if(vasps[x]==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 |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Execution timed out |
2011 ms |
20568 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Runtime error |
9 ms |
17244 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Runtime error |
9 ms |
17244 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8536 KB |
Output is correct |
2 |
Runtime error |
9 ms |
17244 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
8540 KB |
Output is correct |
2 |
Correct |
1 ms |
8540 KB |
Output is correct |
3 |
Correct |
2 ms |
8540 KB |
Output is correct |
4 |
Execution timed out |
2011 ms |
20568 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |