【BZOJ4627】[BeiJing2016]回转寿司 SBT

时间:2023-03-10 03:23:08
【BZOJ4627】[BeiJing2016]回转寿司 SBT

【BZOJ4627】[BeiJing2016]回转寿司

Description

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

Input

第一行包含三个整数N,L和R,分别表示寿司盘数,满意度的下限和上限。
第二行包含N个整数Ai,表示小Z对寿司的满意度。
N≤100000,|Ai|≤100000,0≤L, R≤10^9

Output

仅一行,包含一个整数,表示共有多少种选择可以使得小Z的满意度之和不低于L且不高于R。

Sample Input

5 5 9
1 2 3 4 5

Sample Output

6

题解:首先区间和可以表示成前缀相减,那么我们对于当前的前缀和s[i],只需要统计j<i,L<=s[i]-s[j]<=R的个数即可。可以用SBT实现。

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
const int maxn=100010;
typedef long long ll;
int n,tot,rt;
ll ans;
ll v[maxn],L,R;
struct node
{
int siz,ch[2];
ll val;
}s[maxn];
inline void pushup(int x)
{
s[x].siz=s[s[x].ch[0]].siz+s[s[x].ch[1]].siz+1;
}
inline void rotate(int &x,int d)
{
int y=s[x].ch[d];
s[x].ch[d]=s[y].ch[d^1],s[y].ch[d^1]=x;
pushup(x),pushup(y);
x=y;
}
void maintain(int &x,int d)
{
if(s[s[s[x].ch[d]].ch[d]].siz>s[s[x].ch[d^1]].siz) rotate(x,d);
else if(s[s[s[x].ch[d]].ch[d^1]].siz>s[s[x].ch[d^1]].siz) rotate(s[x].ch[d],d^1),rotate(x,d);
else return ;
maintain(s[x].ch[d],d),maintain(s[x].ch[d^1],d^1);
maintain(x,d),maintain(x,d^1);
}
void insert(int &x,ll y)
{
if(!x)
{
x=++tot;
s[x].siz=1,s[x].ch[0]=s[x].ch[1]=0,s[x].val=y;
return ;
}
int d=(y>s[x].val);
insert(s[x].ch[d],y),s[x].siz++;
maintain(x,d);
}
int qless(int x,ll y)
{
if(!x) return 0;
if(s[x].val>=y) return qless(s[x].ch[0],y);
else return s[s[x].ch[0]].siz+1+qless(s[x].ch[1],y);
}
int qmore(int x,ll y)
{
if(!x) return 0;
if(s[x].val<=y) return qmore(s[x].ch[1],y);
else return s[s[x].ch[1]].siz+1+qmore(s[x].ch[0],y);
}
inline char nc()
{
static char buf[100000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int rd()
{
int ret=0,f=1; char gc=nc();
while(gc<'0'||gc>'9') {if(gc=='-') f=-f; gc=nc();}
while(gc>='0'&&gc<='9') ret=ret*10+gc-'0',gc=nc();
return ret*f;
}
int main()
{
n=rd(),L=rd(),R=rd();
int i;
for(i=1;i<=n;i++)
{
insert(rt,v[i-1]);
v[i]=rd()+v[i-1];
ans+=i-qmore(rt,v[i]-L)-qless(rt,v[i]-R);
}
printf("%lld",ans);
return 0;
}//5 5 9 1 2 3 4 5