Topic Links:
Http://arc075.contest.atcoder.jp/tasks/arc075_c
Test instructions
Give you a sequence and a number k, how many pairs of l,r, so that the arithmetic average of a[l]+a[l+1]+...+a[r] is greater than or equal to K
- 1≤N≤2×10^5
- 1≤K≤10^9
- 1≤ai≤10^9
Ideas:
First for all the numbers minus K, so that we do not have to divide (r-l+1), and then we find that is how many pairs of l,r, so Sum[r]-sum[l-1] >= 0, sum is minus K after the sequence of the prefix and
How many sequential pairs of sums are used in a tree-like array, with a number of 0, representing the contribution from sum[r]-sum[0] to the answer.
Because Sum[i] can be large, discretization is required.
Code:
1#include <bits/stdc++.h>2 using namespacestd;3typedefLong Longll;4 #defineMS (a) memset (A,0,sizeof (a))5 #defineMP Make_pair6 #definePB push_back7 Const intINF =0x3f3f3f3f;8 Constll infll =0x3f3f3f3f3f3f3f3fll;9 inline ll read () {Tenll x=0, f=1;CharCh=GetChar (); One while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} A while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} - returnx*F; - } the ////////////////////////////////////////////////////////////////////////// - Const intMAXN = 2e5+Ten; - -ll n,k,x,s[maxn],tree[maxn*4]; +Vector<ll>v; - + voidAdd (ll x) { A while(X <MAXN) { atTREE[X] + =1; -x + = x&-x; - } - } - - ll sum (ll x) { inll res =0; - while(X >0){ toRes + =Tree[x]; +X-= x&-x; - } the returnRes; * } $ Panax Notoginseng intMain () { -CIN >> N >>K; the for(intI=1; i<=n; i++){ +x =read (); AX-=K; theS[i] = s[i-1]+x; + } - for(intI=0; i<=n; i++) $ V.push_back (S[i]); $ sort (V.begin (), V.end ()); -V.resize (Unique (V.begin (), V.end ())-V.begin ()); - for(intI=0; i<=n; i++) S[i] = Lower_bound (V.begin (), V.end (), s[i])-v.begin () +1; the -ll ans =0;Wuyi for(intI=0; i<=n; i++){ theAns + =sum (s[i]); - Add (S[i]); Wu } -cout << ans <<Endl; About $ - return 0; -}
Atcoder Regular Contest 075 e-meaningful Mean tree array for sequence pairs, prefixes, and