Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int INF=1e9;
- using pii=pair<int,int>;
- int main(){
- int n,p;
- scanf("%d%d",&n,&p);
- vector <pii> g;
- for(int i=0;i<n;i++){
- int a,b,k;
- scanf("%d%d",&a,&b);
- if(a>b) {
- k=a;
- a=b;
- b=k;
- }
- g.push_back({a,1});//add
- g.push_back({b,2});//remove
- }
- sort(g.begin(),g.end());
- int cnt=0,s=-1,e=-1;
- for(auto i:g){
- if(i.second==1){
- cnt++;
- if(cnt==n and s==-1) s=i.first;
- }
- else{
- cnt--;
- if(cnt==n-1 and s!=-1) e=i.first;
- }
- }
- if(s==-1) printf("-1");
- else if(s<=p and p<=e) printf("0");
- else if(p<s) printf("%d",s-p);
- else printf("%d",p-e);
- return 0;
- }
Add Comment
Please, Sign In to add comment