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. js区分大小写

    JavaScript 区分大小写 区分大小写 JavaScript 语言是区分大小写的,不管是命名变量还是使用关键字的时候. 如前面 alert弹出提示框 的例子,如果将 alert 命令改为 ALE ...

  2. ASP.NET Web Pages:Razor

    ylbtech-.Net-ASP.NET Web Pages:Razor 1.返回顶部 1. ASP.NET Web Pages - 添加 Razor 代码 在本教程中,我们将使用 C# 和 Visu ...

  3. 用Dockerfile生成docker image

    在docker的官方php镜像中,有独立的php和apache版本的,这里尝试用php-fpm7.2.1(alpine3.7)作为基础镜像,在把nginx1.13.8加进去. 第一步:拉取php镜像: ...

  4. mongodb中查询返回指定字段

    mongodb中查询返回指定字段   在写vue项目调用接口获取数据的时候,比如新闻列表页我只需要显示新闻标题和发表时间,点击每条新闻进入详情页的时候才会需要摘要.新闻内容等关于此条新闻的所有字段.  ...

  5. Axure8.1.0.3372 注册码

    Axure8.1.0.3372 注册码 转载:http://blog.csdn.net/cslucifer/article/details/79355007 Koshy wTADPqxn3KChzJx ...

  6. python+selenium+requests爬取我的博客粉丝的名称

    爬取目标 1.本次代码是在python2上运行通过的,python3的最需改2行代码,用到其它python模块 selenium 2.53.6 +firefox 44 BeautifulSoup re ...

  7. Python入门-散点图绘制

    Python入门-散点图绘制  废话不说 直接上代码 import matplotlib.pyplot as plt x_values = list(range(1,1001)) y_values = ...

  8. 40. Linux下7-zip解压到当前目录的命令

    7z x test.zip 解压到当前目录下,但保留原来的目录结构 7z e test.zip 解压到当前目录下,不保留原来的目录结构

  9. JAVA Spring JavaBean 引入 JavaBean ( 外部引用, 内部定义, 级联属性 )

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  10. leetcode504

    public class Solution { public string ConvertToBase7(int num) { ? "" : "-"; var ...