SHARE
TWEET

Untitled

a guest Jun 19th, 2019 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<iostream>
  2. #include<algorithm>
  3. #include<cmath>
  4. #include<vector>
  5. #include<stack>
  6. #include<queue>
  7. #include<set>
  8. #include<map>
  9. #define x first
  10. #define y second
  11. #define all(x) x.begin(), x.end()
  12. #define pb push_back
  13. #define mp make_pair
  14. using namespace std;
  15. // #define int long long
  16. signed main(){
  17.   int n;
  18.   cin >> n;
  19.   int l,r;
  20.   vector<pair<int,int> >v;
  21.   while(n--){
  22.     cin >> l >> r;
  23.     v.pb(mp(l,1));
  24.     v.pb(mp(r,-1));
  25.   }
  26.   sort(all(v));
  27.   int bal = 0,ans = 0,prev = v[0].x;
  28.   for(int i = 0;i < v.size();i++){
  29.     if(bal){
  30.       ans+=(v[i].x - prev);
  31.     }
  32.     prev = v[i].x;
  33.     bal+=v[i].y;
  34.   }
  35.   cout << ans;
  36.   return 0;
  37. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top