#include <bits/stdc++.h>
#define int long long
#define FOR(i,a,b) for(int i = a; i < b;++i)
#define pb push_back
#define f first
#define s second
#define pi pair<int,int>
using namespace std;
int32_t main(){
int n,m,s;
cin>>n >>m >>s;
vector<pi> prze,nowy;
FOR(i,0,m){
int l,r;
cin>>l >>r;
prze.pb({l,r});
}
sort(prze.begin(),prze.end());
int poc = 0;
FOR(i,0,prze.size()){
poc = i;
while(poc + 1 < prze.size() && prze[poc].s + 1 == prze[poc + 1].f){
++poc;
}
nowy.pb({prze[i].f,prze[poc].s});
i = poc;
}
int wyn = 1e16;
FOR(i,0,nowy.size()){
if(nowy[i].f <= s && s <= nowy[i].s){
if(nowy[i].f > 1){
wyn = nowy[i].f - 1;
}
if(nowy[i].s + 1 <= n){
if(abs(wyn - s) > abs(s - (nowy[i].s + 1))){
wyn = nowy[i].s + 1;
}
}
}
}
cout<<wyn;
}
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 | #include <bits/stdc++.h> #define int long long #define FOR(i,a,b) for(int i = a; i < b;++i) #define pb push_back #define f first #define s second #define pi pair<int,int> using namespace std; int32_t main(){ int n,m,s; cin>>n >>m >>s; vector<pi> prze,nowy; FOR(i,0,m){ int l,r; cin>>l >>r; prze.pb({l,r}); } sort(prze.begin(),prze.end()); int poc = 0; FOR(i,0,prze.size()){ poc = i; while(poc + 1 < prze.size() && prze[poc].s + 1 == prze[poc + 1].f){ ++poc; } nowy.pb({prze[i].f,prze[poc].s}); i = poc; } int wyn = 1e16; FOR(i,0,nowy.size()){ if(nowy[i].f <= s && s <= nowy[i].s){ if(nowy[i].f > 1){ wyn = nowy[i].f - 1; } if(nowy[i].s + 1 <= n){ if(abs(wyn - s) > abs(s - (nowy[i].s + 1))){ wyn = nowy[i].s + 1; } } } } cout<<wyn; } |
English