Codeforces Round #510 (Div. 2) D. Petya and Array(树状数组)
D. Petya and Array
题目链接:https://codeforces.com/contest/1042/problem/D
题意:
给出n个数,问一共有多少个区间,满足区间和小于t。
题解:
假设目前区间右端点为r,左端点为l,那么由前缀和可得知:sumr-suml-1<t,然后我们再边个形:sumr<t+suml-1,根据这个我们可以发现这有点类似于逆序对。
然后我们就可以用求解逆序对问题的解法来解这个问题了,这里不同的就是每次前面的加上t大于当前这个数即为一对逆序对。
我用的树状数组来做的,用树状数组用两种枚举方式,一种是从前往后,另一种是从后往前。
从前往后的话,如若当前是第i个位置,那么首先要保证前面i-1个数都update了,然后查询前面小于等于它减去t的数有多少,最后用i减去就可以了。
从后往前的话,如若当前是第i个位置,那么从i到最后一个位置都应插入进去,之后查询有多少个小于它加上r就行了。
这个题里面0也应该考虑进去,表示从1到x的这个区间。
代码如下(包含两种方式):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5+;
ll n,t;
ll a[N],sum[N],c[N];
ll lowbit(ll x){
return x&(-x);
}
void upd(ll x,ll b){
for(;x<=N-;x+=lowbit(x)) c[x]+=b;
}
ll query(ll x){
ll ans = ;
for(;x>;x-=lowbit(x)) ans+=c[x];
return ans ;
}
int main(){
scanf("%I64d%I64d",&n,&t);
ll ans=;
for(int i=;i<=n;i++){
scanf("%I64d",&a[i]);
sum[i]=sum[i-]+a[i];
a[i]=sum[i];
}
sort(sum,sum+n+);
/*for(int i=n;i>=0;i--){
int pos1 = lower_bound(sum,sum+n+1,a[i]+t)-sum;
int pos2 = lower_bound(sum,sum+n+1,a[i])-sum+1;
ans+=query(pos1);
//printf("%d\n",pos1);
upd(pos2,1);
}*/
for(int i=;i<=n;i++){
int pos1 = lower_bound(sum,sum+n+,a[i-])-sum+;
int pos2 = upper_bound(sum,sum+n+,a[i]-t)-sum;
upd(pos1,);
ans+=i-query(pos2);
}
cout<<ans;
return ;
}
Codeforces Round #510 (Div. 2) D. Petya and Array(树状数组)的更多相关文章
- Codeforces Round #510 (Div. 2) D. Petya and Array(离散化+反向树状数组)
http://codeforces.com/contest/1042/problem/D 题意 给一个数组n个元素,求有多少个连续的子序列的和<t (1<=n<=200000,abs ...
- Codeforces Round #368 (Div. 2) E. Garlands 二维树状数组 暴力
E. Garlands 题目连接: http://www.codeforces.com/contest/707/problem/E Description Like all children, Ale ...
- Codeforces Round #216 (Div. 2) E. Valera and Queries 树状数组 离线处理
题意:n个线段[Li, Ri], m次询问, 每次询问由cnt个点组成,输出包含cnt个点中任意一个点的线段的总数. 由于是无修改的,所以我们首先应该往离线上想, 不过我是没想出来. 首先反着做,先求 ...
- Codeforces Round #300 F - A Heap of Heaps (树状数组 OR 差分)
F. A Heap of Heaps time limit per test 3 seconds memory limit per test 512 megabytes input standard ...
- D. Petya and Array 树状数组
题意: 给出一个数组,元素有正有负有0,问其区间和小于 t 的子区间的个数. sum[ r ]-sum[ l-1 ]<t,其中sum是a的前缀和. 实现的方法就是从前往后对于每一个sum[ i ...
- Codeforces Round #510 (Div. 2)
Codeforces Round #510 (Div. 2) https://codeforces.com/contest/1042 A 二分 #include<iostream> usi ...
- Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)-D. Restore Permutation-构造+树状数组
Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)-D. Restore Permutation-构造+树状数组 [Pro ...
- 8VC Venture Cup 2016 - Final Round (Div. 2 Edition) D. Factory Repairs 树状数组
D. Factory Repairs 题目连接: http://www.codeforces.com/contest/635/problem/D Description A factory produ ...
- Codeforces Round #603 (Div. 2) E. Editor(线段树)
链接: https://codeforces.com/contest/1263/problem/E 题意: The development of a text editor is a hard pro ...
随机推荐
- mysql5.6主主复制及keepalived 高可用
1.实验目的 mysql服务器作为生产环境中使用最广泛的数据库软件,以其开源性,稳定性而广泛使用,但同时由于数据存储,读写频率高,极易造成数据库出错,从而给企业造成不可挽回的损失,我们除了做好数据库的 ...
- CAS解扰小结
1.每个ts数据包由:1.包头 2.包数据 包头有个字段 PID ,该字段指示包数据的类型.比如说: PID 为 0x0000 包数据的类型就是 PAT表 PID 为 0x0001 包数据的类型就是 ...
- ubuntu64位运行32位程序
sudo dpkg --add-architecture i386 sudo apt install libc6:i386 转:https://blog.csdn.net/zoomdy/article ...
- 【紫书】(UVa12096) The SetStack Computer
突然转进到第五章的low题目的原因是做到图论了(紫书),然后惊喜的发现第一题就做不出来.那么里面用到了这一题的思想,我们就先解决这题.当然,dp必须继续做下去,这是基本功.断不得. 题意分析 这条题真 ...
- hdu1506 Largest Rectangle in a Histogram
Problem Description A histogram is a polygon composed of a sequence of rectangles aligned at a commo ...
- 怎样安装PyCharm
在地址栏输入http://www.jetbrains.com/pycharm/ 打开PyCharm官网 http://idea.lanyus.com/
- MySQL☞length函数
length(字符串/列名):求出该字符串/列名中字符的个数 格式: select length(列名) from 表名 如下图:
- Jmeter中传递cookie值
场景:用户登陆后会本地会保存cookie,cookie是用来跟服务端验证此用户已经登陆过的重要信息,但是如何获取并在其他请求时将此cookie传递给服务器呢? 在线程组下面之直接添加HTTP Cook ...
- Assetbundle1
AssetBundle运行时加载:来自文件就用CreateFromFile(注意这种方法只能用于standalone程序)这是最快的加载方法也可以来自Memory,用CreateFromMemory( ...
- virtualBox 安装 CentOs 6.8 以及网络配置
安装 virtual box 基本设置: 1.创建虚拟电脑 类型:Linux 版本:Red Hat(64-bit) 这个64/32 和电脑具体配置关系. 然后就是路next or 设置常规的东西. 2 ...