#include<bits/stdc++.h>
using namespace std;
typedef long long in;
void solve()
{
in a,b,s;
cin>>a>>b>>s;
vector<pair<in,in>>v;
for(int z=0;z<b;z++)
{
in k,l;
cin>>k>>l;
v.push_back({k,l});
}
sort(v.begin(),v.end());
in zap;
for(int z=0;z<b;z++)
{
if(v[z].first<=s&&v[z].second>=s)
{
zap=z;
}
}
in k1;
in k2;
for(int z=zap;z<b;z++)
{
if(z==b-1||v[z].second!=v[z+1].first-1)
{
k1=v[z].second+1;
break;
}
}
for(int z=zap;z>=0;z--)
{
if(z==0||v[z].first!=v[z-1].second+1)
{
k2=v[z].first-1;
break;
}
}
if(k1==a+1)
{
cout<<k2;
return;
}
if(k2==0)
{
cout<<k1;
return;
}
if(s-k2<=k1-s)cout<<k2;else cout<<k1;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
int h=1;
while(h--)solve();
return 0;
}
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | #include<bits/stdc++.h> using namespace std; typedef long long in; void solve() { in a,b,s; cin>>a>>b>>s; vector<pair<in,in>>v; for(int z=0;z<b;z++) { in k,l; cin>>k>>l; v.push_back({k,l}); } sort(v.begin(),v.end()); in zap; for(int z=0;z<b;z++) { if(v[z].first<=s&&v[z].second>=s) { zap=z; } } in k1; in k2; for(int z=zap;z<b;z++) { if(z==b-1||v[z].second!=v[z+1].first-1) { k1=v[z].second+1; break; } } for(int z=zap;z>=0;z--) { if(z==0||v[z].first!=v[z-1].second+1) { k2=v[z].first-1; break; } } if(k1==a+1) { cout<<k2; return; } if(k2==0) { cout<<k1; return; } if(s-k2<=k1-s)cout<<k2;else cout<<k1; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int h=1; while(h--)solve(); return 0; } |
English