求助,0 pt
查看原帖
求助,0 pt
667808
HarryKane楼主2022/9/25 17:33
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5;

struct node{
    ll l, r;
} a[N+5];

bool cmp (node x, node y){
    if (x.l!=y.l) return x.l<y.l;
    else return x.r<y.r;
}

int main (){
    int n; cin >> n;
    for (int i=1; i<=n; i++) scanf ("%lld %lld", &a[i].l, &a[i].r);
    sort (a+1, a+n+1, cmp);
    unsigned long long ans=0, lst=0;
    for (int i=1; i<=n; i++){
        if (a[i].l>lst){
            ans+=a[i].r-a[i].l+1; lst=a[i].r;
        }
        else if (a[i].l<lst && a[i].r>lst) ans+=a[i].r-lst; lst=a[i].r;
    }
    cout << ans << endl;
    return 0;
}

不知道是不是题意理解的有问题。

2022/9/25 17:33
加载中...