#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
int main()
{
long long l=0,n=0,s=0,loc=0;
cin>>l>>n>>s;
pair <long long, long long> t[n]{};
for(int i=0;i<n;i++){
cin>>t[i].first>>t[i].second;
}
sort(t,t+n);
for(int i=0;i<n;i++){
if(t[i].first<=s&&t[i].second>=s) loc=i;
}
long long down=s-t[loc].first,up=t[loc].second-s;
/*for(int i=0;i<n;i++){
cout<<t[i].first<<' '<<t[i].second<<endl;
}*/
//cout<<up<<' '<<down<<' ';
for(int i=loc;i<n-1;i++){
if(t[i].second+1==t[i+1].first) up+=t[i+1].second-t[i].second;
else i=n;
}
for(int i=loc;i>0;i--){
if(t[i].first-1==t[i-1].second) down+=t[i].first-t[i-1].first;
else i=0;
}
up++;
down++;
if(up>=down) cout<<s-down;
else cout<<s+up;
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | #include <iostream> #include <vector> #include <stack> #include <algorithm> using namespace std; int main() { long long l=0,n=0,s=0,loc=0; cin>>l>>n>>s; pair <long long, long long> t[n]{}; for(int i=0;i<n;i++){ cin>>t[i].first>>t[i].second; } sort(t,t+n); for(int i=0;i<n;i++){ if(t[i].first<=s&&t[i].second>=s) loc=i; } long long down=s-t[loc].first,up=t[loc].second-s; /*for(int i=0;i<n;i++){ cout<<t[i].first<<' '<<t[i].second<<endl; }*/ //cout<<up<<' '<<down<<' '; for(int i=loc;i<n-1;i++){ if(t[i].second+1==t[i+1].first) up+=t[i+1].second-t[i].second; else i=n; } for(int i=loc;i>0;i--){ if(t[i].first-1==t[i-1].second) down+=t[i].first-t[i-1].first; else i=0; } up++; down++; if(up>=down) cout<<s-down; else cout<<s+up; return 0; } |
English