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
#include<bits/stdc++.h>
using namespace std;

int main(){
  ios_base::sync_with_stdio(0);
  cin.tie(0); cout.tie(0);
  long long n,s,m;
  cin>>n>>m>>s;
  vector<pair<long long,long long>>tab(m);
  for(int i=0;i<m;i++)cin>>tab[i].first>>tab[i].second;
  sort(tab.begin(),tab.end());
  
  vector<pair<long long,long long>>pom;
  if(tab[0].first>1)pom.push_back({1,tab[0].first-1});
  for(int i=1;i<m;i++)
    if(tab[i-1].second<tab[i].first-1)pom.push_back({tab[i-1].second+1,tab[i].first-1});
  if(tab[m-1].second<n)pom.push_back({tab[m-1].second+1,n});
  
  long long wyn=LLONG_MAX,naj=0;
  for(auto x:pom){
    if(x.first<=s&&s<=x.second){
      cout<<s; return 0;
    }
    if(x.second<=s){
      naj=x.second;
      wyn=s-x.second;
    }
    else if(x.first-s<wyn){
      naj=x.first;
      wyn=x.first-s;
    }
  }
  cout<<naj;
  return 0;
}