AtCoder - 2581 树状数组

You are given an integer sequence of length N, a= {a1,a2,…,aN}, and an integer K.

a has N(N+1)2 non-empty contiguous subsequences, {al,al+1,…,ar} (1lrN). Among them, how many have an arithmetic mean that is greater than or equal to K?


Constraints
  • All input values are integers.
  • 1N2×105
  • 1K109
  • 1ai109
Input

Input is given from Standard Input in the following format:

N K
a1
a2
:
aN
Output

Print the number of the non-empty contiguous subsequences with an arithmetic mean that is greater than or equal to K.

Sample Input 1
3 6
7
5
7
Sample Output 1
5

All the non-empty contiguous subsequences of a are listed below:

  • {a1} = {7}
  • {a1,a2} = {7,5}
  • {a1,a2,a3} = {7,5,7}
  • {a2} = {5}
  • {a2,a3} = {5,7}
  • {a3} = {7}

Their means are 7, 6, 193, 5, 6 and 7, respectively, and five among them are 6 or greater. Note that {a1} and {a3} are indistinguishable by the values of their elements, but we count them individually.

Sample Input 2
1 2
1
Sample Output 2
0
Sample Input 3
7 26
10
20
30
40
30
20
10
Sample Output 3
13


询问有多少区间和满足<=k*len;
暴力肯定不行;
我们要求的就是sum[r]-sum[l-1]>=k*(r-l+1)
--> sum[r]-k*r-(sum[l-1]-k*(l-1))>=0;
也就是求sum[r]-k*r>=sum[l-1]-k*(l-1)的数量;
树状数组!;
当然由于数量级太大,我们可以先离散化;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 2000005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-4
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
	ll x = 0;
	char c = getchar();
	bool f = false;
	while (!isdigit(c)) {
		if (c == '-') f = true;
		c = getchar();
	}
	while (isdigit(c)) {
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return f ? -x : x;
}

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; }


/*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
	if (!b) {
		x = 1; y = 0; return a;
	}
	ans = exgcd(b, a%b, x, y);
	ll t = x; x = y; y = t - a / b * y;
	return ans;
}
*/


ll n, k;
ll s[maxn];
ll c[maxn<<2];
vector<ll>vc;
void add(ll  x) {
	while (x <maxn) {
		c[x]++; x += x & -x;
	}
}

ll query(ll x) {
	ll res = 0;
	while (x > 0) {
		res += c[x]; x -= x & -x;
	}
	return res;
}


int main() {
//	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n >> k;
	for (int i = 1; i <= n; i++) {
		ll x;
		rdllt(x);
		x -= k;
		s[i] = s[i - 1] + x;
	}
	for (int i = 0; i <= n; i++)vc.push_back(s[i]);
	sort(vc.begin(), vc.end());
	vc.resize(unique(vc.begin(), vc.end()) - vc.begin());
	for (int i = 0; i <= n; i++)s[i] = lower_bound(vc.begin(), vc.end(), s[i]) - vc.begin() + 1;
	ll res = 0;
	for (int i = 0; i <= n; i++) {
		res += query(s[i]); add(s[i]);
	}
	cout << res << endl;
	return 0;
}

 


posted @ 2019-01-25 13:41  NKDEWSM  阅读(179)  评论(0编辑  收藏  举报