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
#include <bits/stdc++.h>
using namespace std;
long long n, m, s, p, k, mn=1000000000000;
int main()
{
  ios_base::sync_with_stdio(0); 
  cin.tie(0);
  cout.tie(0);
  cin>>n>>m>>s;
  int t[n];
  for(int i=0; i<n; ++i)
  {
      t[i]=i+1;
  }
  t[s-1]=0;
  for(int i=0; i<m; ++i)
  {
      cin>>p>>k;
      for(int j=p-1; j<k; ++j)
      {
          t[j]=0;
      }
  }
  for(int i=s; i>0; --i)
  {
      if(t[i]!=0 && s-t[i]<abs(s-mn))
      {
          mn=t[i];
      }
  }
  for(int i=s; i<n; ++i)
  {
      if(t[i]!=0 && t[i]-s<abs(s-mn))
      {
          mn=t[i];
      }
  }
  cout<<mn;
  return 0;
}