bzoj4627 [BeiJing2016]回转寿司 线段树

Description


酷爱日料的小Z经常光顾学校东门外的回转寿司店。在这里,一盘盘寿司通过传送带依次呈现在小Z眼前。不同的寿
司带给小Z的味觉感受是不一样的,我们定义小Z对每盘寿司都有一个满意度,例如小Z酷爱三文鱼,他对一盘三文
鱼寿司的满意度为10;小Z觉得金枪鱼没有什么味道,他对一盘金枪鱼寿司的满意度只有5;小Z最近看了电影“美
人鱼”,被里面的八爪鱼恶心到了,所以他对一盘八爪鱼刺身的满意度是-100。特别地,小Z是个著名的吃货,他
吃回转寿司有一个习惯,我们称之为“狂吃不止”。具体地讲,当他吃掉传送带上的一盘寿司后,他会毫不犹豫地
吃掉它后面的寿司,直到他不想再吃寿司了为止。今天,小Z再次来到了这家回转寿司店,N盘寿司将依次经过他的
面前,其中,小Z对第i盘寿司的满意度为Ai。小Z可以选择从哪盘寿司开始吃,也可以选择吃到哪盘寿司为止,他
想知道共有多少种不同的选择,使得他的满意度之和不低于L,且不高于R。注意,虽然这是回转寿司,但是我们不
认为这是一个环上的问题,而是一条线上的问题。即,小Z能吃到的是输入序列的一个连续子序列;最后一盘转走
之后,第一盘并不会再出现一次。

N≤100000,|Ai|≤100000,0≤L, R≤10^9

Solution


实践证明题目越长不一定越难♂

只需要统计一下前缀和s,然后枚举每一位作为右端点然后统计此时的方案数即可

Code


#include <stdio.h>
#include <string.h>
#include <algorithm>
#define rep(i,st,ed) for (int i=st;i<=ed;++i)

typedef long long LL;
const LL INF=10000000000;
const int N=200005;

struct treeNode {int l,r,sum;} t[N*51];

int root=1,tot=1;
LL a[N];

LL read() {
    LL x=0,v=1; char ch=getchar();
    for (;ch<'0'||ch>'9';v=(ch=='-')?(-1):(v),ch=getchar());
    for (;ch<='9'&&ch>='0';x=x*10+ch-'0',ch=getchar());
    return x*v;
}

void modify(int &now,LL tl,LL tr,LL x) {
    if (!now) t[now=++tot]=(treeNode) {0,0,0};
    t[now].sum++;
    if (tl==tr) return ;
    LL mid=(tl+tr)>>1;
    if (x<=mid) modify(t[now].l,tl,mid,x);
    else modify(t[now].r,mid+1,tr,x);
}

int query(int now,LL tl,LL tr,LL l,LL r) {
    if (!now) return 0;
    if (tl==l&&tr==r) return t[now].sum;
    LL mid=(tl+tr)>>1;
    if (r<=mid) return query(t[now].l,tl,mid,l,r);
    if (l>mid) return query(t[now].r,mid+1,tr,l,r);
    int qx=query(t[now].l,tl,mid,l,mid);
    int qy=query(t[now].r,mid+1,tr,mid+1,r);
    return qx+qy;
}

int main(void) {
    int n=read(),L=read(),R=read();
    LL ans=0;
    modify(root,-INF,INF,0);
    rep(i,1,n) {
        a[i]=read(); a[i]+=a[i-1];
        ans+=query(root,-INF,INF,a[i]-R,a[i]-L);
        modify(root,-INF,INF,a[i]);
    }
    printf("%lld\n", ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/jpwang8/article/details/80487041