Advertisement
OIQ

Untitled

OIQ
Feb 4th, 2020
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <utility>
  5. #include <string>
  6. #include <math.h>
  7. #include <map>
  8. #include <set>
  9. #include <queue>
  10.  
  11. using namespace std;
  12. typedef long long ll;
  13.  
  14. int main() {
  15.     ios_base::sync_with_stdio(false);
  16.     cin.tie(0);
  17.     int n;
  18.     cin >> n;
  19.     int ind = 0;
  20.     vector <pair<int, bool>> a(n * 2);
  21.     for (int i = 0; i < n; i++) {
  22.         int x, y;
  23.         cin >> x >> y;
  24.         a[ind++] = { x, true };
  25.         a[ind++] = { y, false };
  26.     }
  27.  
  28.     sort(a.begin(), a.end());
  29.  
  30.     int ans = 0;
  31.  
  32.     int count = 1;
  33.     for (int i = 1; i < a.size(); i++) {
  34.         if (count >= 1)
  35.             ans += a[i].first - a[i - 1].first;
  36.         if (a[i].second == true)
  37.             count++;
  38.         else
  39.             count--;
  40.  
  41.     }
  42.  
  43.     cout << ans;
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement