Codeforces Round #510 (Div. 2) D. Petya and Array(离散化+反向树状数组)
http://codeforces.com/contest/1042/problem/D
题意
给一个数组n个元素,求有多少个连续的子序列的和<t
(1<=n<=200000,abs(a[i])<=1e9)
思路
- 将公式转化以下,sum[r]-sum[l-1]<t 变成 sum[r]<sum[l-1]+t
- 可以考虑遍历每个r,先更新sum[r-1]+t,统计有多少满足条件的sum[l-1],反向树状数组维护即可
实现细节
- 对于每个r是更新他的sum[r-1]
- 因为要统计>sum[r]的数有多少,但是反向树状数组是包含自己当前这个点的,所以要加一
- 反向树状数组的右边界最好是数组大小
#include<bits/stdc++.h>
#define M 400005
#define ll long long
using namespace std;
int tr[M];
int n,tot,i;
ll tp,p[M],x[M],ans,t;
int lowbit(int x){return x&(-x);}
void add(int x){
while(x>0){
tr[x]++;x-=lowbit(x);
}
}
int qy(int x){
x++; //
int ans=0;
while(x<M){ //
ans+=tr[x];x+=lowbit(x);
}
return ans;
}
int fd(ll x){
return lower_bound(p,p+tot,x)-p+1;
}
int main(){
cin>>n>>t;
tot=0;
for(i=1;i<=n;i++){
scanf("%lld",&x[i]);
x[i]+=x[i-1];
p[tot++]=x[i];
p[tot++]=x[i]+t;
}
sort(p,p+tot);
tot=unique(p,p+tot)-p;
ans=0;
for(i=1;i<=n;i++){
add(fd(x[i-1]+t)); //
ans+=qy(fd(x[i]));
//cout<<ans<<endl;
}
cout<<ans;
}
Codeforces Round #510 (Div. 2) D. Petya and Array(离散化+反向树状数组)的更多相关文章
- Codeforces Round #510 (Div. 2) D. Petya and Array(树状数组)
D. Petya and Array 题目链接:https://codeforces.com/contest/1042/problem/D 题意: 给出n个数,问一共有多少个区间,满足区间和小于t. ...
- Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum (离线树状数组+前缀xor)
题目链接:http://codeforces.com/contest/703/problem/D 给你n个数,m次查询,每次查询问你l到r之间出现偶数次的数字xor和是多少. 我们可以先预处理前缀和X ...
- Codeforces Round #365 (Div. 2)-D Mishka and Interesting sum(树状数组)
题目链接:http://codeforces.com/contest/703/problem/D 思路:看了神犇的代码写的... 偶数个相同的数异或结果为0,所以区间ans[l , r]=区间[l , ...
- Codeforces Round #198 (Div. 1) D. Iahub and Xors 二维树状数组*
D. Iahub and Xors Iahub does not like background stories, so he'll tell you exactly what this prob ...
- Codeforces Round 261 Div.2 D Pashmak and Parmida's problem --树状数组
题意:给出数组A,定义f(l,r,x)为A[]的下标l到r之间,等于x的元素数.i和j符合f(1,i,a[i])>f(j,n,a[j]),求有多少对这样的(i,j). 解法:分别从左到右,由右到 ...
- Codeforces Round #590 (Div. 3)【D题:26棵树状数组维护字符出现次数】
A题 题意:给你 n 个数 , 你需要改变这些数使得这 n 个数的值相等 , 并且要求改变后所有数的和需大于等于原来的所有数字的和 , 然后输出满足题意且改变后最小的数值. AC代码: #includ ...
- VK Cup 2016 - Round 1 (Div. 2 Edition) B. Bear and Displayed Friends 树状数组
B. Bear and Displayed Friends 题目连接: http://www.codeforces.com/contest/658/problem/B Description Lima ...
- Codeforces Round #510 (Div. 2)
Codeforces Round #510 (Div. 2) https://codeforces.com/contest/1042 A 二分 #include<iostream> usi ...
- CF #365 (Div. 2) D - Mishka and Interesting sum 离线树状数组
题目链接:CF #365 (Div. 2) D - Mishka and Interesting sum 题意:给出n个数和m个询问,(1 ≤ n, m ≤ 1 000 000) ,问在每个区间里所有 ...
随机推荐
- 对stm32f373XX的startup.s的文件的分析
;******************** (C) COPYRIGHT 2012 STMicroelectronics ********************;* File Name : start ...
- 写了一个兼容IE9的图片放大器(基于vue)
photoloupe 图片放大器 第一次写vue插件,本人比较喜欢用简单易懂的写法,不喜勿喷. 本插件支持IE9及以上版本,已经过验证. 本插件可根据需要设置放大倍数,最小支持1倍,支持小数 下载地址 ...
- Wannafly挑战赛13 C:zzf的好矩阵(思维)
题目描述 一个8 * 8的棋盘,第一个格子放1个麦穗,第二个格子放2个麦穗,第三个格子放4个麦穗……那么最后,共要放几个麦穗呢? zzf表示这个问题实在太简单,于是重新规定了游戏的规则. 初始的棋盘为 ...
- c#: TabControl隐藏选项卡(WizardPages)
如Delphi之TPageControl控件,其TTabSheet有TabVisible属性,在制作类似Wizard页面切换时,甚为有用. 而c#对应之TabControl控件,其页面TabPage无 ...
- 896. Monotonic Array单调数组
[抄题]: An array is monotonic if it is either monotone increasing or monotone decreasing. An array A i ...
- http协议的学习
TPC/IP协议是传输层协议,主要解决数据如何在网络中传输,而HTTP是应用层协议,主要解决如何包装数据.
- 43-将javaweb项目部署到Linux服务器
这是第二次弄了,感觉由于上次积累了点资源,这次要少走很多弯路了,再次记录下来吧. 第一次的记录:将本地的javaweb项目部署到Linux服务器的一般操作 1. 在Linux上建立数据库,我是将本地的 ...
- Debug和汇编编译器masm对指令的不同处理
我们在Debug和源程序中写入同样形式的指令 : "mov al,[0]","mov bl,[1]","mov cl,[2]"," ...
- 2019.3.15 关于IE
1. .clearfix {zoom:1} zoom:1 是ie浏览器专有属性 它可以设置或检索对象缩放比例 处理ie的hasLayout属性 清除浮动 清除margin的重叠
- Aspose.Words二 基础
1.目录样式 doc.Styles[Aspose.Words.StyleIdentifier.Toc1].Font.Size = 14; doc.Styles[Aspose.Words.StyleId ...