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);
}

 

全部评论

相关推荐

10-14 23:01
已编辑
中国地质大学(武汉) Java
CUG芝士圈:虽然是网上的项目,但最好还是包装一下,然后现在大部分公司都在忙校招,十月底、十一月初会好找一些。最后,boss才沟通100家,别焦虑,我去年暑假找第一段实习的时候沟通了500➕才有面试,校友加油
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务