Bzoj4627 [BeiJing2016]回转寿司
Submit: 329 Solved: 149
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
1 2 3 4 5
Sample Output
HINT
Source
线段树
求满足L<=sum[i]-sum[j-1]<=R (j<=i)的i,j有多少对
变形得到sum[i]-R<=sum[j-1]<=sum[i]-L
那么把sum[i]挨个扔进线段树里,每次查询有多少个sum[j-1]满足条件即可
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cstring> 5 #define LL long long 6 using namespace std; 7 const LL INF=1e13; 8 const int mxn=100010; 9 int read(){ 10 int x=0,f=1;char ch=getchar(); 11 while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();} 12 while(ch>='0' && ch<='9'){x=x*10-'0'+ch;ch=getchar();} 13 return x*f; 14 } 15 struct node{ 16 int l,r; 17 int v; 18 }t[mxn*50];int cnt=0; 19 int n,L,R,rot=0; 20 LL sum[mxn],ans=0; 21 void update(LL p,LL l,LL r,int &rt){ 22 if(!rt){rt=++cnt;} 23 if(l==r){++t[rt].v;return;} 24 LL mid=(l+r)>>1; 25 if(p<=mid)update(p,l,mid,t[rt].l); 26 else update(p,mid+1,r,t[rt].r); 27 t[rt].v=t[t[rt].l].v+t[t[rt].r].v; 28 return; 29 } 30 LL query(LL x,LL y,LL l,LL r,int rt){ 31 if(x<=l && r<=y){return t[rt].v;} 32 LL mid=(l+r)>>1; 33 LL res=0; 34 if(x<=mid && t[rt].l)res+=query(x,y,l,mid,t[rt].l); 35 if(y>mid && t[rt].r)res+=query(x,y,mid+1,r,t[rt].r); 36 return res; 37 } 38 int main(){ 39 int i,j,w; 40 n=read();L=read();R=read(); 41 // LL lm=INF,rm=-INF;然而没考虑到sum[0],会WA 42 LL lm=0,rm=0; 43 for(i=1;i<=n;i++){ 44 w=read(); 45 sum[i]=sum[i-1]+w; 46 lm=min(lm,sum[i]);rm=max(rm,sum[i]); 47 } 48 update(0,lm,rm,rot);//sum[0]==0 49 for(i=1;i<=n;i++){ 50 ans+=query(sum[i]-R,sum[i]-L,lm,rm,rot); 51 update(sum[i],lm,rm,rot); 52 } 53 printf("%lld\n",ans); 54 return 0; 55 }
转载于:https://www.cnblogs.com/SilverNebula/p/6361072.html
Bzoj4627 [BeiJing2016]回转寿司相关推荐
- bzoj4627[BeiJing2016]回转寿司
bzoj4627[BeiJing2016]回转寿司 题意: 求在一个序列中和在区间[l,r]中的连续子序列的个数.序列大小≤100000,序列元素可以为负数. 题解: 题目要求这个:l<=sum ...
- BZOJ4627: [BeiJing2016]回转寿司
BZOJ4627: [BeiJing2016]回转寿司 Description 酷爱日料的小Z经常光顾学校东门外的回转寿司店. 在这里,一盘盘寿司通过传送带依次呈现在小Z眼前. 不同的寿司带给小Z的味 ...
- [BZOJ4627][BeiJing2016]回转寿司 cdq分治
怎么短怎么写对吧,cdq分治代替数据结构 把区间连续的看成前缀和相减,排序单调队列维护即可 注意sum[0] /******************************************** ...
- 【BZOJ4627】[BeiJing2016]回转寿司 SBT
[BZOJ4627][BeiJing2016]回转寿司 Description 酷爱日料的小Z经常光顾学校东门外的回转寿司店.在这里,一盘盘寿司通过传送带依次呈现在小Z眼前.不同的寿司带给小Z的味觉感 ...
- bzoj 4627: [BeiJing2016]回转寿司 -- 权值线段树
4627: [BeiJing2016]回转寿司 Time Limit: 10 Sec Memory Limit: 256 MB Description 酷爱日料的小Z经常光顾学校东门外的回转寿司店. ...
- BZOJ 4627: [BeiJing2016]回转寿司 cdq
4627: [BeiJing2016]回转寿司 Time Limit: 10 Sec Memory Limit: 256 MB Submit: 624 Solved: 273 [Submit][S ...
- BZOJ 4627: [BeiJing2016]回转寿司
线段树裸题 #include<cstdio> using namespace std; int cnt,root,sz[10000005],ls[10000005],rs[10000005 ...
- bzoj 4627:[BeiJing2016]回转寿司
算法:裸树状数组/裸线段树 难度:(NOIP-) 设s[i]为前缀和,差分,把序列和转化为前缀相减,即选出满足L≤s[x]−s[y]≤R的x>y的数个数.那么我们枚举x,即可得到y的范围,二分找 ...
- BZOJ 4627回转寿司(值域线段树)
4627: [BeiJing2016]回转寿司 Time Limit: 10 Sec Memory Limit: 256 MB Submit: 507 Solved: 217 [Submit][S ...
最新文章
- 高并发存储番外篇:Redis套路,一网打尽
- NAPI技术--在Linux 网络驱动上的应用和完善(二)
- [笔记]NumPy基础操作
- 前端日拱一卒D9——ES6笔记之基础篇
- float取整数部分_一步一步学Python3(小学生也适用) 第六篇: 变量及整数(int)类型...
- 七牛云存储Python SDK使用教程 - 上传策略详解
- Django从理论到实战(part20)--DLT常用过滤器
- oracle服务器找不到怎么解决,简析Oracle数据库常见问题及解决方案
- 联发科预计天玑系列5G智能手机处理器今年出货超4500万颗
- 2009年度工作安排计划
- 广告位管理系统--跨域广告加载问题
- 创业邦:谈谈 To B业务的机会
- 图片上传经过jwt_SpringSecurity整合Jwt过程图解
- Win 10 专业版重新激活详细操作步骤
- 10个常用的数据分析商业模型之波特五种竞争力模型(一)
- Delphi 生成二维码【PaintBox】并保存为本地文件添加至【Image】控件中
- 洞见趋势系列(二)特征工程
- PHP审计-RIPS
- android 多屏幕显示activity,副屏,无线投屏
- android10手机众筹,世界上最小的Android 10手机海外众筹,竟是来自中国?