Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long int ll;
- ll ans,n;
- int main()
- {
- cin >> n;
- pair <ll,ll> prev,cur,totalInterv;
- prev = make_pair(0,0);
- cur = prev = totalInterv;
- for(int i = 0; i < n; i++)
- {
- cin >> cur.first >> cur.second;
- if(cur == prev && i != 0)
- continue;
- ll change = min(cur.first,cur.second) - max(prev.first,prev.second);
- 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
- change--;
- else change++; //Inclusive counting
- if(change < 0) change = 0;
- ans += change;
- prev = cur;
- totalInterv.second = cur.second;
- }
- cout << ans << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement