#include <bits/stdc++.h>
using namespace std;
#define FOR(i,p,q) for(int i=(p); i<=(q); ++i)
#define ROF(i,p,q) for(int i=(p); i>=(q); --i)
#define REP(i,q) for(int i=0; i<(q); ++i)
#define pb push_back
#define as assign
#define rz resize
#define Co const
#define all(X) X.begin(), X.end()
#define rall(X) X.rbegin(), X.rend()
#define sz(X) (int)(X.size())
#define ckmax(a,b) a=max(a,b)
#define ckmin(a,b) a=min(a,b)
#define V vector
typedef long long ll;
typedef mt19937_64 mt;
#ifndef UNCLE
typedef basic_string<bool> vb;
typedef basic_string<int> vi;
typedef basic_string<ll> vl;
#else
typedef V<bool> vb;
typedef V<int> vi;
typedef V<ll> vl;
#endif
ll N,K;
int M;
struct RG{ll p,q;};
V<RG> rgs;
void Input(){
cin>>N>>M>>K;
ll p,q;
REP(_,M) cin>>p>>q, rgs.pb({p,q});
sort(all(rgs),[](Co RG &a, Co RG &b){return a.p<b.p;});
}
void Solve(){
RG rsRg=rgs[0];
FOR(i,1,M-1){
if(rgs[i-1].q+1==rgs[i].p) rgs[i].p=rgs[i-1].p;
if(rgs[i].p<=K&&K<=rgs[i].q) rsRg=rgs[i];
}
++M;--M;
if((rsRg.p!=1&&K-rsRg.p<=rsRg.q-K)||rsRg.q==N) cout<<rsRg.p-1<<"\n";
else cout<<rsRg.q+1<<"\n";
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
Input();
Solve();
}
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 | #include <bits/stdc++.h> using namespace std; #define FOR(i,p,q) for(int i=(p); i<=(q); ++i) #define ROF(i,p,q) for(int i=(p); i>=(q); --i) #define REP(i,q) for(int i=0; i<(q); ++i) #define pb push_back #define as assign #define rz resize #define Co const #define all(X) X.begin(), X.end() #define rall(X) X.rbegin(), X.rend() #define sz(X) (int)(X.size()) #define ckmax(a,b) a=max(a,b) #define ckmin(a,b) a=min(a,b) #define V vector typedef long long ll; typedef mt19937_64 mt; #ifndef UNCLE typedef basic_string<bool> vb; typedef basic_string<int> vi; typedef basic_string<ll> vl; #else typedef V<bool> vb; typedef V<int> vi; typedef V<ll> vl; #endif ll N,K; int M; struct RG{ll p,q;}; V<RG> rgs; void Input(){ cin>>N>>M>>K; ll p,q; REP(_,M) cin>>p>>q, rgs.pb({p,q}); sort(all(rgs),[](Co RG &a, Co RG &b){return a.p<b.p;}); } void Solve(){ RG rsRg=rgs[0]; FOR(i,1,M-1){ if(rgs[i-1].q+1==rgs[i].p) rgs[i].p=rgs[i-1].p; if(rgs[i].p<=K&&K<=rgs[i].q) rsRg=rgs[i]; } ++M;--M; if((rsRg.p!=1&&K-rsRg.p<=rsRg.q-K)||rsRg.q==N) cout<<rsRg.p-1<<"\n"; else cout<<rsRg.q+1<<"\n"; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); Input(); Solve(); } |
English