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 ...
随机推荐
- liunx下搭建python开发环境
=============================================================================注意: 在linux下安装新的版本的pytho ...
- 模块的使用与orm简介
目录 1 django中app的概念: 2 模板路径配置: 3 静态文件配置: 4 完整版登录功能 5 get请求和post请求 6 新手三件套总结 7 pycharm连接mysql 8 orm介绍 ...
- 当安装mongodb客户端出现了Failed to load list of databases
在装mongodb最新版(4.1.5开发版)服务后,我用robo3t打开它的时候遇到了这个问题. 最直接的解决办法就是换一个mongodb版本,https://github.com/Studio3T/ ...
- 【Leetcode】413. Arithmetic Slices
Description A sequence of number is called arithmetic if it consists of at least three elements and ...
- Tensorflow之安装GPU版错误集合
在根据教程http://blog.csdn.net/sb19931201/article/details/53648615安装好全部的时候,却无情的给我抛了几个错: 1.AttributeEr ...
- python, pycharm, virtualenv 的使用
创建虚拟环境,一次安装多个库 pip freeze > requirements.txt (库的名字都在里面) 产生requirements.txt文件 在另一个环境下使用 pip instal ...
- AWS安装CDH5.3-CentOS6.4
1.下载CM启动文件 [root@ip-172-31-23-107 ec2-user]# wget http://archive.cloudera.com/cm5/installer/latest/c ...
- 导入execl到数据库mysql
GwykhrenyuankuList <body jwcid="$content$"> <span jwcid="@components/AppBord ...
- ActiveRecord-连接多张表之单表继承
ActiveRecord-连接多张表之单表继承 1. 基本概念 Rails提供了两种机制,可以将复杂的面向对象模型映射为关系模型,即所谓的单表继承(single-table inheritance)和 ...
- Julia 学习笔记(一):数组
个人向,只会记录一些需要注意的点. 前言 学习 Julia 已经有一段时间了,但是进步缓慢.这一方面是最近代码写得少,一方面是 Julia 学习资料少.中文资料更少,但也有我没做笔记的缘故导致学习效率 ...