#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
//freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
ll n,m,s;
cin>>n>>m>>s;
vector<pair<ll, ll>> pairs(m);
for (ll i=0; i<m; i++) {
cin>>pairs[i].first>>pairs[i].second;
}
sort(pairs.begin(), pairs.end());
vector<pair<ll, ll>> new_pairs;
new_pairs.push_back(pairs[0]);
for (ll i=1; i<m; i++){
if (new_pairs.back().second+1==pairs[i].first) {
new_pairs.back().second=pairs[i].second;
}
else{
new_pairs.push_back(pairs[i]);
}
}
// for (auto e:new_pairs){
// cout<<e.first<<"-"<<e.second<<endl;
// }
ll p1=-12, p2=-12;
for (auto p:new_pairs) {
if (p.first<=s and s<=p.second) {
if (p.first-1==0) {
p1=-12;
}
else{
p1=p.first-1;
}
if (p.second+1==n+1) {
p2 = -12;
}
else{
p2=p.second+1;
}
break;
}
}
//cout<<p1<<"-"<<p2<<endl;
if(p1==-12){
cout<<p2<<endl;
}
else if(p2==-12){
cout<<p1<<endl;
}
else{
if(abs(s-p1)<=abs(s-p2)){
cout<<p1<<endl;
} else {
cout<<p2<<endl;
}
}
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | #include<bits/stdc++.h> using namespace std; typedef long long ll; int main() { //freopen("input.in", "r", stdin); //freopen("output.out", "w", stdout); ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); ll n,m,s; cin>>n>>m>>s; vector<pair<ll, ll>> pairs(m); for (ll i=0; i<m; i++) { cin>>pairs[i].first>>pairs[i].second; } sort(pairs.begin(), pairs.end()); vector<pair<ll, ll>> new_pairs; new_pairs.push_back(pairs[0]); for (ll i=1; i<m; i++){ if (new_pairs.back().second+1==pairs[i].first) { new_pairs.back().second=pairs[i].second; } else{ new_pairs.push_back(pairs[i]); } } // for (auto e:new_pairs){ // cout<<e.first<<"-"<<e.second<<endl; // } ll p1=-12, p2=-12; for (auto p:new_pairs) { if (p.first<=s and s<=p.second) { if (p.first-1==0) { p1=-12; } else{ p1=p.first-1; } if (p.second+1==n+1) { p2 = -12; } else{ p2=p.second+1; } break; } } //cout<<p1<<"-"<<p2<<endl; if(p1==-12){ cout<<p2<<endl; } else if(p2==-12){ cout<<p1<<endl; } else{ if(abs(s-p1)<=abs(s-p2)){ cout<<p1<<endl; } else { cout<<p2<<endl; } } return 0; } |
English