More than 2019 cattle off summer school camp (second field) J.Subarray

The meaning of problems: you n represents a n-1 consecutive segments now ask you a total sequence length in the range of 0 ~ 1e9-1 how many sub-period is greater than 0

Idea: Suppose we count the current prefix and obviously we can look at Fenwick tree maintenance prefixes and we nlogn calculate the answer but for data processing 1e7 of this time-out so we will definitely consider is a prefix and each change is 1 polyline

We can directly simulate an array lazy to find the answer

#include <bits/stdc++.h>
using namespace std;
const int N = 1e7+7;
const int inf = 0x3f3f3f3f;
typedef long long ll;
const ll mod = 1e7+9;
int l[N],r[N];
int f[N],g[N],sum[3*N];
int b[3*N],c[3*N];
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int n; cin>>n;
    for(int i=1;i<=n;i++){
        cin>>l[i]>>r[i];
    }
    f[1]=r[1]-l[1]+1;
    for(int i=2;i<=n;i++)
        f[i]=max(r[i]-l[i]+1,f[i-1]-(l[i]-r[i-1]-1)+r[i]-l[i]+1);
    g[n]=r[n]-l[n]+1;
    for(int i=n-1;i>=1;i--)
        g[i]=max(r[i]-l[i]+1,g[i+1]-(l[i+1]-r[i]-1)+r[i]-l[i]+1);
    int i=1; int now=1e7;
    ll ans=0;
    while(i<=n){
        int j=i+1;
        while(j<=n&&g[j]+f[j-1]>=(l[j]-r[j-1]-1)){
            j++;
        }
        j--;
        int le=max(0,l[i]-g[i]); int ri=min(int(1e9)-1,r[j]+f[j]);
        int t=i; int mx,mi; mx=-1; mi=inf;
        for(int k=le;k<=ri;k++){
            if(k>=l[t]&&k<=r[t])
                sum[k-le+1]=sum[k-le]+1;
            else
                sum[k-le+1]=sum[k-le]-1;
            if(k==r[t]) t++;    
            mx=max(mx,sum[k-le+1]+now);
            mi=min(mi,sum[k-le+1]+now);
            b[sum[k-le+1]+now]++;
        }
        for(int k=mx-1;k>=mi;k--)
            b[k]+=b[k+1];
        ans+=b[now+1];
        for(int k=le;k<=ri;k++){
            t=sum[k-le+1]+now;
            b[t+1]-=c[t+1];
            c[t]+=c[t+1]+1;
            c[t+1]=0;
            ans+=b[t+1];
        }
        for(int k=mi;k<=mx;k++)
            b[k]=0,c[k]=0;
        i=j+1;
    }
    cout<<ans<<endl;
}

 

Guess you like

Origin www.cnblogs.com/wmj6/p/11288038.html