# 【BZOJ4627】[BeiJing2016]回转寿司

## Input

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

5 5 9
1 2 3 4 5

## Sample Output

6

#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

| 欢迎来原网站坐坐！ >原文链接<

posted @ 2017-10-15 10:38  CQzhangyu  阅读(194)  评论(0编辑  收藏  举报