正题
题目链接:https://www.luogu.com.cn/problem/P2717
题目大意
nnn个数,求有多少个连续子序列的平均值大于等于kkk。
解题思路
因为长度会十分干扰,所以我们将所有数减去kkk。问题就变为了求有多少连续子序列的和非负。用前缀和+逆序对求就好了。
codecodecode
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
#define lowbit(x) (x&-x)
using namespace std;
const ll N=1e5+10;
ll n,k,a[N],b[N],t[N],ans;
void change(ll x,ll num)
{while(x<=n)t[x]+=num,x+=lowbit(x);return;
}
ll ask(ll x)
{ll ans=0;while(x)ans+=t[x],x-=lowbit(x);return ans;
}
int main()
{scanf("%lld%lld",&n,&k);for(ll i=1;i<=n;i++)scanf("%lld",&a[i]),a[i]+=a[i-1]-k,b[i]=a[i];b[n+1]=0;sort(b+1,b+2+n);ll m=unique(b+1,b+2+n)-b-1;change(lower_bound(b+1,b+1+m,0)-b,1);for(ll i=1;i<=n;i++){a[i]=lower_bound(b+1,b+1+m,a[i])-b;ans+=ask(a[i]);change(a[i],1);}printf("%lld",ans);
}