Petya and Array

http://codeforces.com/problemset/problem/1042/D

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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.

Output

Print the number of segments in Petya's array with the sum of elements less than tt.

Examples
input
5 4
5 -1 3 4 -1
output
5
input
3 0
-1 2 -3
output
4
input
4 -1
-2 1 -2 3
output
3
Note

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 (权值线段树+逆序对)的更多相关文章

  1. hdu 6703 array(权值线段树)

    Problem Description You are given an array a1,a2,...,an(∀i∈[1,n],1≤ai≤n). Initially, each element of ...

  2. 2019年CCPC网络赛 HDU 6703 array【权值线段树】

    题目大意:给出一个n个元素的数组A,A中所有元素都是不重复的[1,n].有两种操作:1.将pos位置的元素+1e72.查询不属于[1,r]中的最小的>=k的值.强制在线. 题解因为数组中的值唯一 ...

  3. 洛谷P1908 逆序对 [权值线段树]

    题目传送门 逆序对 题目描述 猫猫TOM和小老鼠JERRY最近又较量上了,但是毕竟都是成年人,他们已经不喜欢再玩那种你追我赶的游戏,现在他们喜欢玩统计.最近,TOM老猫查阅到一个人类称之为“逆序对”的 ...

  4. codevs1688 求逆序对(权值线段树)

    1688 求逆序对  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Description 给定一个序列a1,a2,…, ...

  5. [BZOJ 3295] [luogu 3157] [CQOI2011]动态逆序对(树状数组套权值线段树)

    [BZOJ 3295] [luogu 3157] [CQOI2011] 动态逆序对 (树状数组套权值线段树) 题面 给出一个长度为n的排列,每次操作删除一个数,求每次操作前排列逆序对的个数 分析 每次 ...

  6. ccpc网赛 hdu6703 array(权值线段树

    http://acm.hdu.edu.cn/showproblem.php?pid=6703 大意:给一个n个元素的数组,其中所有元素都是不重复的[1,n]. 两种操作: 将pos位置元素+1e7 查 ...

  7. D. Restore Permutation(权值线段树)

    D. Restore Permutation time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  8. 线段树(单标记+离散化+扫描线+双标记)+zkw线段树+权值线段树+主席树及一些例题

    “队列进出图上的方向 线段树区间修改求出总量 可持久留下的迹象 我们 俯身欣赏” ----<膜你抄>     线段树很早就会写了,但一直没有总结,所以偶尔重写又会懵逼,所以还是要总结一下. ...

  9. BZOJ2141排队——树状数组套权值线段树(带修改的主席树)

    题目描述 排排坐,吃果果,生果甜嗦嗦,大家笑呵呵.你一个,我一个,大的分给你,小的留给我,吃完果果唱支歌,大家 乐和和.红星幼儿园的小朋友们排起了长长地队伍,准备吃果果.不过因为小朋友们的身高有所区别 ...

随机推荐

  1. 解决Ubuntu下使用命令行subl 打开Sublime text3无法输入中文的问题

    cd /opt/sublime_text/ sudo vim sub-fcitx.c 新建文件sub-fcitx.c,建议放在Sublime Text的所在目录下,将下面的代码复制进去 ,参考: ht ...

  2. c++并发编程实战 笔记

    http://blog.csdn.net/u010087886/article/category/5884745

  3. HBase基础之Hbase shell常用操作

    一般操作 查看服务器状态 status 查看hbase版本 version DDL操作 创建表 create 'member','member_id','address','info' 创建了3个列族 ...

  4. Dance GAN 迁移不同视频中人物动作的方法

    该研究提出一种迁移不同视频中人物动作的方法.给出两个视频,一个视频中是研究者想要合成动作的目标人物,另一个是被迁移动作的源人物,研究者通过一种基于像素的端到端流程在人物之间进行动作迁移(motion ...

  5. QEMU,KVM及QEMU-KVM介绍

    What's QEMU QEMU是一个主机上的VMM(virtual machine monitor),通过动态二进制转换来模拟CPU,并提供一系列的硬件模型,使guest os认为自己和硬件直接打交 ...

  6. python气象分析

    数据分析实例 -- 气象数据 一.实验介绍 本实验将对意大利北部沿海地区的气象数据进行分析与可视化.我们在实验过程中先会运用 Python 中matplotlib库的对数据进行图表化处理,然后调用 s ...

  7. rsync同步web数据

    rsync远程同步web服务器的数据 实验拓扑                                            服务器A(rsync服务器)--------------服务器B( ...

  8. 解决 php 报错 open_basedir restriction in effect或者nginx提示No input file specified怎么办

    解决 php 报错 open_basedir restriction in effect或者nginx提示No input file specified怎么办 问题是出现在了PHP.INI上面了 ,原 ...

  9. Django中使用Celery实现定时任务(用djcelery)

    一.引言 Django是python语言下的一个比较热门的Web框架,越来越多的企业和开发者使用Django实现自己的Web服务器.在Web服务器开发过程中,有时候我们不仅仅是要实现Web服务器端和用 ...

  10. VBA 语句集400句

    定制模块行为(1) Option Explicit '强制对模块内所有变量进行声明    Option Private Module '标记模块为私有,仅对同一工程中其它模块有用,在宏对话框中不显示  ...