TLE玄关求条
查看原帖
TLE玄关求条
1286422
Muggles楼主2025/6/21 15:38

TLE#2#10QAQ

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
bool f[N*2];
long long a[N],b[N],c[N*2],d[N*2];
long long dt,ct,ans,x,y;
int n;
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i]>>b[i];
        d[++dt]=a[i];
        d[++dt]=b[i];
    }
    sort(d+1,d+1+dt);
    c[++ct]=d[1];
    for(int i=1;i<=dt;i++){
        if(d[i]!=d[i-1]){
            c[++ct]=d[i];
        }
    }
    for(int i=1;i<=n;i++){
        x=lower_bound(c+1,c+ct+1,a[i])-c;
        y=lower_bound(c+1,c+ct+1,b[i])-c;
        for(int j=x;j<y;j++){
            f[j]=1;
        }
    }
    for(int i=1;i<ct;i++){
        if(f[i]){
            ans+=c[i+1]-c[i];
            if(f[i+1]!=1){
                ans++;
            }
        }
    }
    cout<<ans;
    return 0;
}
2025/6/21 15:38
加载中...