CodeForces - 1042D Petya and Array (树状数组+离散化)

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

        sum[i]-sum[j]<t;可得到  :sum[j]>sum[i]-t;枚举i计算每个i能够组合的j有多少即可(ps用离散化);

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 2e5 + 5;
int n;ll t; ll sot[maxn]; ll sum[maxn];
ll m[maxn];
int lowbit(int x) {
	return x&(-x);
}
void add(int x,int d) {
	while (x < maxn) {
		m[x] += d;
		x += lowbit(x);
	}
}
ll getsum(int x) {
	ll sum = 0;
	while (x) {
		sum += m[x];
		x -= lowbit(x);
	}
	return sum;
}
int main() {

	scanf("%d%lld", &n, &t);
	memset(m, 0, sizeof m);
	sum[0] = 0;
	for (int i = 1; i <= n; i++) {
		int tmp; scanf("%d", &tmp);
		sum[i] = sum[i - 1] + tmp;
		sot[i] = sum[i];
	}
	sort(sot + 1, sot + 1 + n);
	ll ans = 0;//a-b<t;  t-a>-b;  b>a-t;
	for (int i = 1; i <= n; i++) {
		if (sum[i] < t)ans++;
		ll spot = sum[i] - t;
		int x = upper_bound(sot + 1, sot + 1 + n, spot) - sot;
		int y = lower_bound(sot + 1, sot + 1 + n, sum[i]) - sot;
		ans += i - 1 - getsum(x - 1);
		add(y, 1);	
	}
	printf("%lld\n", ans);
}

 

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值