4627: [BeiJing2016]回转寿司
Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 624 Solved: 273
[Submit][Status][Discuss]
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
1 2 3 4 5
Sample Output
6
这个题。随便做啊。。。
权值线段树 权值树状数组
之后本来想粘代码
之后看到有个人写的cdq
觉得好久没写了 就写了一发
然后memcpy的时候sizeof(int)一直挂。。。
利用cdq常数小的巨大优势卡到了rank1
#include<cmath>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<string>
#include<bitset>
#include<queue>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch<='9'&&ch>='0'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}
void print(int x)
{if(x<0)putchar('-'),x=-x;if(x>=10)print(x/10);putchar(x%10+'0');}
const int N=100100;
ll ans,sum[N],tmp[N];
int a[N],L,R;
void cdq(int l,int r)
{
if(l==r)return ;
int mid=(l+r)>>1;
cdq(l,mid);cdq(mid+1,r);
int x=l,y=l;
for(int i=mid+1;i<=r;++i)
{
while(y<=mid&&sum[i]-sum[y]>=L)y++;
while(x<=mid&&sum[i]-sum[x]>R)x++;
ans+=max(0,y-x);
}
int top=l;
x=l,y=mid+1;
while(x<=mid&&y<=r)
{
while(sum[y]<=sum[x]&&y<=r)tmp[top++]=sum[y++];
tmp[top++]=sum[x++];
}
while(x<=mid)tmp[top++]=sum[x++];
while(y<=r)tmp[top++]=sum[y++];
memcpy(sum+l,tmp+l,sizeof(ll)*(r-l+1));
}
int main()
{
int n=read();L=read();R=read();
register int i;
for(i=1;i<=n;++i)a[i]=read(),sum[i]=sum[i-1]+a[i];
cdq(0,n);
cout<<ans<<endl;
return 0;
}
/*
5 5 9
1 2 3 4 5
6
*/