CF1042D Petya and Array

本文解析了CodeForces竞赛中一道关于数组区间求和的问题,通过离散化和权值线段树的方法实现了快速查询特定条件下区间的数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原题链接:http://codeforces.com/contest/1042/problem/D

Petya and Array

Petya has an array a a a consisting of n n n 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 t t t. Help Petya to calculate this number.

More formally, you are required to calculate the number of pairs l , r ( l ≤ r ) l,r (l≤r) l,r(lr) such hat a l + a l + 1 + ⋯ + a r − 1 + a r &lt; t a_l+a_{l+1}+\cdots+a_{r−1}+a_r&lt;t al+al+1++ar1+ar<t.

Input

The first line contains two integers n n n and t ( 1 ≤ n ≤ 200000 , ∣ t ∣ ≤ 2 ⋅ 1 0 14 ) t (1≤n≤200000,|t|≤2⋅10^{14}) t(1n200000,t21014).

The second line contains a sequence of integers a 1 , a 2 , ⋯ &ThinSpace; , a n ( ∣ a i ∣ ≤ 1 0 9 ) a_1,a_2,\cdots,a_n (|a_i|≤10^9) a1,a2,,an(ai109) — 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 t t t.

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 4 : [ 2 , 2 ] 4:[2,2] 4:[2,2], sum of elements is − 1 [ 2 , 3 ] −1 [2,3] 1[2,3], sum of elements is 2 [ 3 , 3 ] 2 [3,3] 2[3,3], sum of elements is 3 [ 4 , 5 ] 3 [4,5] 3[4,5], sum of elements is 3 [ 5 , 5 ] 3 [5,5] 3[5,5], sum of elements is − 1 −1 1

题解

区间求和可以转化为前缀和相减,那么对于一个前缀 p r e [ i ] pre[i] pre[i],合法的前缀满足的条件就是 p r e [ i ] − p r e [ j ] &lt; t pre[i]-pre[j]&lt;t pre[i]pre[j]<t,即查询 p r e [ j ] &lt; p r e [ i ] − t ( 0 ≤ j &lt; i ) pre[j]&lt;pre[i]-t(0\le j&lt;i) pre[j]<pre[i]t(0j<i)的个数,所以我们离散化一下开棵权值线段树即可。

考试的时候调了半天发现忘了先把 0 0 0加进去了。。。

代码
#include<bits/stdc++.h>
#define ll long long
#define ls v<<1
#define rs v<<1|1
using namespace std;
const int M=1e6+5;
int n,q;
ll sum[M],pre[M],data[M],t;
void up(int v){sum[v]=sum[ls]+sum[rs];}
void add(int v,int le,int ri,int pos)
{
	if(le==ri){++sum[v];return;}
	int mid=le+ri>>1;
	if(pos<=mid)add(ls,le,mid,pos);
	else add(rs,mid+1,ri,pos);
	up(v);
}
int ask(int v,int le,int ri,int lb,int rb)
{
	if(lb<=le&&ri<=rb){return sum[v];}
	int mid=le+ri>>1;ll ans=0;
	if(lb<=mid)ans=ask(ls,le,mid,lb,rb);
	if(mid<rb)ans+=ask(rs,mid+1,ri,lb,rb);
	return ans;
}
void in()
{
	ll ans=0;int l;
	scanf("%d%I64d",&n,&t);for(int i=1;i<=n;++i)scanf("%I64d",&pre[i]),pre[i]+=pre[i-1],data[i]=pre[i];
	sort(data+1,data+2+n);q=unique(data+1,data+2+n)-data-1;
	add(1,1,q,lower_bound(data+1,data+1+q,0)-data);
	for(int i=1;i<=n;++i)
	{
		l=upper_bound(data+1,data+1+q,pre[i]-t)-data;
		if(l>q){add(1,1,q,lower_bound(data+1,data+1+q,pre[i])-data);continue;}
		ans+=ask(1,1,q,l,q);
		add(1,1,q,lower_bound(data+1,data+1+q,pre[i])-data);
	}
	printf("%I64d",ans);
}
int main(){in();}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ShadyPi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值