bzoj4627: [BeiJing2016]回转寿司

好题!

这题教会我:写cdq可以不拘一格,只要保证本质是释放左半部分影响于右半部分就可以了。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
int n;LL L,R;
LL a[110000],s[110000],tt[110000],ans;
void cdq(int l,int r)
{
    if(l==r)return ;
    int mid=(l+r)/2;
    cdq(l,mid);cdq(mid+1,r);
    
    LL head=l,tail=l-1;
    for(LL i=mid+1;i<=r;i++)
    {
        while(tail+1<=mid&&L<=s[i]-s[tail+1])tail++;
        while(head<=mid&&s[i]-s[head]>R)head++;
        ans+=max(tail-head+1,0LL);
    }
    sort(s+l,s+r+1);
}
int main()
{
    scanf("%d%lld%lld",&n,&L,&R);
    s[1]=0;
    for(int i=1;i<=n;i++)
        scanf("%lld",&a[i]), s[i+1]=s[i]+a[i];
    n++;ans=0;
    cdq(1,n);
    printf("%lld\n",ans);
    return 0;
}

 

pain and happy in the cruel world.
posted @ 2018-03-22 10:46  AKCqhzdy  阅读(115)  评论(0编辑  收藏  举报