Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.94 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long int ll;
  5. ll ans,n;
  6. int main()
  7. {
  8.     cin >> n;
  9.     pair <ll,ll> prev,cur,totalInterv;
  10.     prev = make_pair(0,0);
  11.     cur  = prev = totalInterv;
  12.  
  13.     for(int i = 0; i < n; i++)
  14.     {
  15.         cin >> cur.first >> cur.second;
  16.         if(cur == prev && i != 0)
  17.             continue;
  18.         ll change = min(cur.first,cur.second) - max(prev.first,prev.second);
  19.  
  20.         if( (max(prev.first,prev.second) == 0 && min(cur.first,cur.second) > 0 && i > 0) || ((cur.second == totalInterv.second && i > 0) || (cur.first == totalInterv.second && i > 0))) //If there is an interval with 0 or same ending, we need to exclude intitial value
  21.                 change--;
  22.         else change++; //Inclusive counting
  23.  
  24.         if(change < 0) change = 0;
  25.         ans += change;
  26.         prev = cur;
  27.         totalInterv.second = cur.second;
  28.     }
  29.  
  30.     cout << ans << endl;
  31.     return 0;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement