#include #include #include #include using namespace std; struct cmp{ bool operator()(const pair& a, const pair& b){ return a.first < b.first; } }; int main(){ int n; cin >> n; vector> dp(n); //ÊäÈë for(int i = 0; i < n; i++){ cin >> dp[i].first >> dp[i].second; } //°´ÕÕdp.firstÅÅÐò sort(dp.begin(), dp.end(), cmp()); vector> merge; for(auto [l, r] : dp){ if (merged.empty() || merged.back().second < l) merged.push_back({l,r}); else merged.back().second = max(merged.back().second, r); } int sum = 0; for(auto [l, r] : merge){ sum += r - l; } cout << sum << endl; return 0; }