Advertisement
Emiliatan

b966

Jun 16th, 2019
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. /* b966             */
  2. /* AC (0.4s, 412KB) */
  3. #include <cstdio>
  4. #include <vector>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. typedef struct _line
  10. {
  11.         int s, e;
  12. }line;
  13.  
  14. bool cmp(line a, line b)
  15. {
  16.         if(a.s == b.s) return a.e < b.e;
  17.         return a.s < b.s;
  18. }
  19.  
  20. vector<line> arr;
  21.  
  22. int main()
  23. {
  24.        for(int N; ~scanf("%d", &N) && N;)
  25.        {
  26.              arr.resize(N);
  27.              int len = 0;
  28.              for(int i = 0, s, e; i < N && scanf("%d %d", &s, &e); arr[i].s = s, arr[i].e = e, ++i);
  29.              sort(arr.begin(), arr.end(), cmp);
  30.              for(int i = 0; i < N; ++i)
  31.              {
  32.                    int s = arr[i].s, e = arr[i].e;
  33.                    while(i + 1 < N && e > arr[i + 1].s)
  34.                    {
  35.                             if(e < arr[i + 1].e)
  36.                                 e = arr[i + 1].e;
  37.                             ++i;
  38.                    }
  39.                    len += e - s;
  40.              }
  41.              printf("%d\n", len);
  42.        }
  43.        return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement