E - Meaningful Mean
Time limit : 2sec / Memory limit : 256MB
Score : 600 points
Problem Statement
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} (1≤l≤r≤N). Among them, how many have an arithmetic mean that is greater than or equal to K?
Constraints
- All input values are integers.
- 1≤N≤2×105
- 1≤K≤109
- 1≤ai≤109
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, 19⁄3, 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
官方题解:
![](https://images2015.cnblogs.com/blog/1148194/201707/1148194-20170706214842440-2099307622.png)
题解前半段很好懂,最后一段说可以使用树状数组,一开始不是很懂,看了别人的题解发现原来可以用求逆序对数的做法。
我一开始想到的是求[i+1,n]区间大于等于f[i]的个数,于是就用归并树做了。
#include<cstdio> #include<cstdlib> #include<cstring> #include<string> #include<algorithm> #include<iostream> #include<queue> #include<map> #include<cmath> #include<set> #include<stack> #define ll long long #define pb push_back #define max(x,y) ((x)>(y)?(x):(y)) #define min(x,y) ((x)>(y)?(y):(x)) #define cls(name,x) memset(name,x,sizeof(name)) using namespace std; const int inf=1e9+10; const ll llinf=1e16+10; const int maxn=1e6+10; const int maxm=2e5+10; const int mod=1e9+7; const double pi=acos(-1.0); int n; ll sum,k; ll f[maxn]; struct node { ll *num; int len; }tree[maxn*4]; void build(int l,int r,int rt) { tree[rt].len=r-l+1; tree[rt].num=new ll[tree[rt].len]; if(l==r) { tree[rt].num[0]=f[l]; return ; } int mid=(l+r)/2; build(l,mid,rt*2); build(mid+1,r,rt*2+1); int i=0,j=0,k=0; while(i<tree[rt*2].len&&j<tree[rt*2+1].len) { if(tree[rt*2].num[i]<tree[rt*2+1].num[j]) tree[rt].num[k++]=tree[rt*2].num[i++]; else tree[rt].num[k++]=tree[rt*2+1].num[j++]; } while(i<tree[rt*2].len) tree[rt].num[k++]=tree[rt*2].num[i++]; while(j<tree[rt*2+1].len) tree[rt].num[k++]=tree[rt*2+1].num[j++]; } int querry(int ql,int qr,ll key,int l,int r,int rt) { if(ql==l&&qr==r) { int c=lower_bound(tree[rt].num,tree[rt].num+tree[rt].len,key)-tree[rt].num; return tree[rt].len-c; } int mid=(l+r)/2; if(qr<=mid) return querry(ql,qr,key,l,mid,rt*2); else if(ql>=mid+1) return querry(ql,qr,key,mid+1,r,rt*2+1); else return querry(ql,mid,key,l,mid,rt*2)+querry(mid+1,qr,key,mid+1,r,rt*2+1); } int main() { //freopen("in.txt","r",stdin); while(~scanf("%d %lld",&n,&k)) { sum=0; f[0]=sum-k*(0+1); for(int i=1;i<=n;i++) { int t; scanf("%d",&t); sum=sum+t; f[i]=sum-k*(i+1); } build(1,n,1); ll ans=0; for(int i=0;i<=n-1;i++) { ans+=querry(i+1,n,f[i],1,n,1); } printf("%lld ",ans); } return 0; }