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 ...
随机推荐
- Java学习笔记十:Java的数组以及操作数组
Java的数组以及操作数组 一:什么是数组: 数组可以理解为是一个巨大的“盒子”,里面可以按顺序存放多个类型相同的数据,比如可以定义 int 型的数组 scores 存储 4 名学生的成绩 数组中的元 ...
- Go语言中的HTTP
Go中的http使用 package main import ( "fmt" "net/http" "io/ioutil" "st ...
- LeetCode:21. Merge Two Sorted Lists(Easy)
1. 原题链接 https://leetcode.com/problems/merge-two-sorted-lists/description/ 2. 题目要求 给出两个已经从小到大排序的链表ls1 ...
- Github上的1000多本免费电子书重磅来袭!
Github上的1000多本免费电子书重磅来袭! 以前 StackOverFlow 也给出了一个免费电子书列表,现在在Github上可以看到时刻保持更新的列表了. 瞥一眼下面的书籍分类目录,你就能 ...
- centos7用yum搭建LAMP环境
用yum快速搭建LAMP平台 实验环境: [root@nmserver- html]# cat /etc/redhat-release CentOS release (AltArch) [root@n ...
- 基于Python的接口自动化-01
为什么要做接口测试 当前互联网产品迭代速度越来越快,由之前的2-3个月到个把月,再到班车制,甚至更短,每次发版之前都需要对所有功能进行回归测试,在人力资源有限的情况下,做自动化测试很有必要.由于UI更 ...
- Python网络编程(基础总结、 入门经典)
Linux下文件类型: bcd -lsp b(块.设备文件) c(字符设备文件) d(目录) -(普通文件) ...
- ThinkPHP5项目目录规划实践
ThinkPHP5安装后(或者下载后的压缩文件解压后)可以看到下面的目录结构: tp5├─application 应用目录 ├─extend 扩展类库目录(可定义) ├─pu ...
- 平衡二叉树(AVL Tree)
在学习算法的过程中,二叉平衡树是一定会碰到的,这篇博文尽可能简明易懂的介绍下二叉树的相关概念,然后着重讲下什么事平衡二叉树. (由于作图的时候忽略了箭头的问题,正常的树一般没有箭头,虽然不影响描述的过 ...
- 解决:Unable to execute dex: GC overhead limit exceeded
转自http://blog.sina.com.cn/s/blog_6e334dc70101hnug.html Android打包时下面的错误: Unable to execute dex: GC ov ...