Petya and Array (权值线段树+逆序对)
Petya and Array
http://codeforces.com/problemset/problem/1042/D
2 seconds
256 megabytes
standard input
standard output
Petya has an array aa consisting of nn integers. He has learned partial sums recently, and now he can calculate the sum of elements on any segment of the array really fast. The segment is a non-empty sequence of elements standing one next to another in the array.
Now he wonders what is the number of segments in his array with the sum less than tt. Help Petya to calculate this number.
More formally, you are required to calculate the number of pairs l,rl,r (l≤rl≤r) such that al+al+1+⋯+ar−1+ar<tal+al+1+⋯+ar−1+ar<t.
The first line contains two integers nn and tt (1≤n≤200000,|t|≤2⋅10141≤n≤200000,|t|≤2⋅1014).
The second line contains a sequence of integers a1,a2,…,ana1,a2,…,an (|ai|≤109|ai|≤109) — the description of Petya's array. Note that there might be negative, zero and positive elements.
Print the number of segments in Petya's array with the sum of elements less than tt.
5 4
5 -1 3 4 -1
5
3 0
-1 2 -3
4
4 -1
-2 1 -2 3
3
In the first example the following segments have sum less than 44:
- [2,2][2,2], sum of elements is −1−1
- [2,3][2,3], sum of elements is 22
- [3,3][3,3], sum of elements is 33
- [4,5][4,5], sum of elements is 33
- [5,5][5,5], sum of elements is −1
参考博客 http://mamicode.com/info-detail-2452129.html
找区间和小于t的个数,区间和的问题,一般用前缀和来做
可以看成sum[i]>t+sum[k]的个数,i<k<=n。这样就变成了一个逆序对的问题
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<queue>
#include<cstdio>
#include<vector>
#define maxn 500005
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
typedef long long ll;
using namespace std; vector<ll>v;
ll n;
ll a[maxn];
ll sum[maxn]; int tree[maxn<<]; int getid(ll x){
return lower_bound(v.begin(),v.end(),x)-v.begin()+;
} void pushup(int rt){
tree[rt]=tree[rt<<]+tree[rt<<|];
} void build(int l,int r,int rt){
if(l==r){
tree[rt]=;
return;
}
int mid=(l+r)/;
build(lson);
build(rson);
pushup(rt);
} void add(int L,int k,int l,int r,int rt){
if(l==r){
tree[rt]+=k;
return;
}
int mid=(l+r)/;
if(L<=mid) add(L,k,lson);
else add(L,k,rson);
pushup(rt);
} ll query(int L,int R,int l,int r,int rt){
if(L<=l&&R>=r){
return tree[rt];
}
int mid=(l+r)/;
ll ans=;
if(L<=mid) ans+=query(L,R,lson);
if(R>mid) ans+=query(L,R,rson);
return ans;
} int main(){ std::ios::sync_with_stdio(false);
ll t;
cin>>n>>t;
for(int i=;i<=n;i++){
cin>>a[i];
}
v.push_back(t-);
for(int i=;i<=n;i++){
sum[i]=a[i]+sum[i-];
v.push_back(sum[i]);
v.push_back(sum[i]+t-);
}
if(n==){
if(a[]<t){
cout<<<<endl;
}
else{
cout<<<<endl;
}
}
else{
sort(v.begin(),v.end());
v.erase(unique(v.begin(),v.end()),v.end());
int Size=v.size();
build(,Size,);
add(getid(sum[n]),,,Size,);
ll ans=;
for(int i=n-;i>=;i--){
ans+=query(,getid(sum[i]+t-),,Size,);
add(getid(sum[i]),,,Size,);
}
ans+=query(,getid(t-),,Size,); cout<<ans<<endl;
} }
Petya and Array (权值线段树+逆序对)的更多相关文章
- hdu 6703 array(权值线段树)
Problem Description You are given an array a1,a2,...,an(∀i∈[1,n],1≤ai≤n). Initially, each element of ...
- 2019年CCPC网络赛 HDU 6703 array【权值线段树】
题目大意:给出一个n个元素的数组A,A中所有元素都是不重复的[1,n].有两种操作:1.将pos位置的元素+1e72.查询不属于[1,r]中的最小的>=k的值.强制在线. 题解因为数组中的值唯一 ...
- 洛谷P1908 逆序对 [权值线段树]
题目传送门 逆序对 题目描述 猫猫TOM和小老鼠JERRY最近又较量上了,但是毕竟都是成年人,他们已经不喜欢再玩那种你追我赶的游戏,现在他们喜欢玩统计.最近,TOM老猫查阅到一个人类称之为“逆序对”的 ...
- codevs1688 求逆序对(权值线段树)
1688 求逆序对 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description 给定一个序列a1,a2,…, ...
- [BZOJ 3295] [luogu 3157] [CQOI2011]动态逆序对(树状数组套权值线段树)
[BZOJ 3295] [luogu 3157] [CQOI2011] 动态逆序对 (树状数组套权值线段树) 题面 给出一个长度为n的排列,每次操作删除一个数,求每次操作前排列逆序对的个数 分析 每次 ...
- ccpc网赛 hdu6703 array(权值线段树
http://acm.hdu.edu.cn/showproblem.php?pid=6703 大意:给一个n个元素的数组,其中所有元素都是不重复的[1,n]. 两种操作: 将pos位置元素+1e7 查 ...
- D. Restore Permutation(权值线段树)
D. Restore Permutation time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- 线段树(单标记+离散化+扫描线+双标记)+zkw线段树+权值线段树+主席树及一些例题
“队列进出图上的方向 线段树区间修改求出总量 可持久留下的迹象 我们 俯身欣赏” ----<膜你抄> 线段树很早就会写了,但一直没有总结,所以偶尔重写又会懵逼,所以还是要总结一下. ...
- BZOJ2141排队——树状数组套权值线段树(带修改的主席树)
题目描述 排排坐,吃果果,生果甜嗦嗦,大家笑呵呵.你一个,我一个,大的分给你,小的留给我,吃完果果唱支歌,大家 乐和和.红星幼儿园的小朋友们排起了长长地队伍,准备吃果果.不过因为小朋友们的身高有所区别 ...
随机推荐
- python3 from Tkinter import * 和import Tkinter as tk两个区别import Tkinter
原文https://stackoverflow.com/questions/15974787/difference-between-import-tkinter-as-tk-and-from-tkin ...
- ajax异步、同步问题,KindEditor ajax提交内容,ajax提交form表单 解决按两次的问题
版权声明:本文为博主原创文章,未经博主允许不得转载. 如果ajax不采用异步,整个js代码在服务器返回结果前都将阻塞,alert方法除外 lookUp('lookUp','',100,300,3); ...
- td高度不随内容变化display:block;display:block;display:block;display:block;display:block;
在TD里加个DIV就可以解决!CSS对应改成#aaa td div{ height:236px; overflow:hidden; 在TD里加个DIV就可以解决!CSS对应改成#aaa td div{ ...
- Ubuntu-14.04.1 desktop安装时及安装后遇到的小问题
ubuntu安装时,进入桌面就黑屏:ctrl+alt+F1进入终端1,输入:startx,此时桌面会重新出现,快速修改桌面的分辨率即可.没有及时修改可以重复上面操作. su root认证失败:sudo ...
- OpenGL 多线程共享纹理
1:opengl 多线程共享纹理纹理: //解码时候使用opengl进行绘制,需要构建队列和两个线程,分别用于解码数据并且填充纹理和渲染. 主线程常见两个共享上下文: main() { ⋯⋯⋯⋯ gH ...
- PHP使用FPDF pdf添加水印中文乱码问题 pdf合并版本问题
---恢复内容开始--- require_once('../fpdf/fpdf.php');require_once('../fpdi/fpdi.php'); 使用此插件 pdf 合并 并添加水印 期 ...
- Neuromation新研究:利用卷积神经网络进行儿童骨龄评估
近日,Neuromation 团队在 Medium 上撰文介绍其最新研究成果:利用卷积神经网络(CNN)评估儿童骨龄,这一自动骨龄评估系统可以得到与放射科专家相似或更好的结果.该团队评估了手骨不同区域 ...
- IDEA在编辑时提示could not autowire
IDEA在编辑时提示could not autowire 原创 2016年05月14日 10:53:38 28338 在开发中我再applicationContext-dao.xml中加入了mappe ...
- CSS3基础
内容: 1.圆角 border-radius 2.阴影 text-shadow.box-shadow 3.渐变 linear.radial 4.rgba rgb+alpha opacity 5.tra ...
- nginx, supervisor
Nginx(单进程): 反向代理, 负载均衡.图解 将配置文件 nginx.conf 的 user xx 配置好 xx用户 检查语法 $ sudo service nginx configtest 重 ...