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

using namespace std;

int ilosc[8008][4];

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

   int m;
   long long n,s,l,r;
   cin>>n>>m>>s;
   vector<pair<long long,long long>>W;
   for(int i=0;i<m;i++)
   {
       cin>>l>>r;
       W.push_back({l,r});
   }
   sort(W.begin(),W.end());
   long long p,k;
   p=W[0].first-1;
   k=W[m-1].second+1;
   for(int i=1;i<m;i++)
   {
       if(s>=W[i].first&&s<=W[i].second)
       {
            if(W[i].first-1!=W[i-1].second)p=W[i].first-1;
       }
       if(s>W[i].second)
       {
           if(W[i].first-1!=W[i-1].second)p=W[i].first-1;
       }
       if(s<W[i].first)
       {
           if(W[i].first-1!=W[i-1].second)k=W[i].first-1;
       }
   }
long long wyn=s-p;
if(p==0)wyn=k+s;


   if(k-s<wyn&&k!=n+1)cout<<k;
   else cout<<p;



    return 0;
}