CF1005E2 Median on Segments (General Case Edition)(树状数组)
题意:
给出一个序列,询问中位数为m的子序列数量(这里的子序列要求连续)
题解:
#include<bits/stdc++.h> using namespace std; const int maxn=1e6+100; typedef long long ll; #define lowbit(x) x&-x int a[maxn]; int b[maxn]; int c[maxn]; int n,m; void add (int x,int val) { for (int i=x;i<maxn;i+=lowbit(i)) c[i]+=val; } int getsum (int x) { int ans=0; for (int i=x;i;i-=lowbit(i)) ans+=c[i]; return ans; } ll wjm (int n,int m) { for (int i=1;i<=n;i++) a[i]<=m?(b[i]=1):(b[i]=-1); for (int i=1;i<=n;i++) b[i]+=b[i-1]; memset(c,0,sizeof(c)); add(n+1,1); ll ans=0; for (int i=1;i<=n;i++) ans+=getsum(n+1+b[i]),add(b[i]+n+1,1); return ans; } int main () { cin>>n>>m; for (int i=1;i<=n;i++) cin>>a[i]; cout<<wjm(n,m)-wjm(n,m-1)<<endl; }