Fenwick tree, in reverse order of --CodeForces - 1042D

Title meaning

Given the number of pile and a number T

Identify and meet the strict interval is less than the number of intervals T

Topic analysis

Interval and is seeking, and then consider the prefix [a, b] = sum [b] -sum [a-1] <T

If you find a prefix and a test with two cycles, it is certainly time out

And the formula can be turned into a sum [j] -T <sum [i], j> i, a request like this problem is the reverse of the

Topic Code

#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=2e5+7;
LL a[maxn],sum[maxn],temp[maxn],c[maxn],t;
int n;
int lowbit(int x){
    return x&(-x);
}
void add(int x){
    while(x<=n+1){
        c[x]++;
        x+=lowbit(x);
    }
}
int ask(int x){
    int ans=0;
    while(x){
        ans+=c[x];
        x-=lowbit(x);
    }
    return ans;
}
int main(){
    scanf("%d%lld",&n,&t);
    for(int i=1;i<=n;i++){
        scanf("%lld",&a[i]);
        sum[i]=sum[i-1]+a[i];
        temp[i]=sum[i];
    }
    sort(temp,temp+1+n);
    LL ans=0;
    for(int i=1;i<=n;i++){
        int pos=lower_bound(temp,temp+1+n,sum[i-1])-temp+1;
        add(pos);
        pos=upper_bound(temp,temp+1+n,sum[i]-t)-temp;
        ans+=i-ask(pos);
    }
    printf("%lld\n",ans);
    return 0;
}

And because the array to open a small card for a while. . .

 

Guess you like

Origin www.cnblogs.com/helman/p/11222747.html