#55405: c++ vector解


61247091s@gapps.ntnu.edu.tw (wei)


#include <bits/stdc++.h>
using namespace std;

int main(){
    int n,a,b;
    cin>>n;
    vector <pair<int,int>> v;
    while(n--){
        cin>>a>>b;
        v.push_back({a,b});}
    sort(v.begin(),v.end());
    int total=0,low=v[0].first,high=v[0].second;
    for(int i=1;i<v.size();i++){
        if(v[i].first<=high){
            high=max(high,v[i].second);
        }else{
            total+=high-low;
            low=v[i].first;high=v[i].second;
        }}
        cout<<total+high-low<<endl;

    return 0;
}