A number greater than B and a number less than B can cancel each other out, so we use 1 and 1 to denote.
Extending from B to both sides, left[i] means that the left side of B has a number of possible numbers of I smaller than B, and Right[i] indicates that the B-right offset has the number of I numbers larger than B.
Ans=sigma (Left[i]*right[i]).
#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include < Cstdio> #include <cmath>using namespace std; #define N 200010 int n,b,k; int ans; int A[n];int L[n],r[n],sum[n]; int main () { scanf ("%d%d", &n,&b); for (int i=1;i<=n;i++) { scanf ("%d", &a[i]); if (a[i]==b) k=i,a[i]=0; Else a[i]=a[i]>b? 1:-1; } L[n]=1; R[n]=1; for (int i=k-1;i>=1;i--) { sum[i]=sum[i+1]+a[i]; l[sum[i]+n]++; } for (int i=k+1;i<=n;i++) { sum[i]=sum[i-1]+a[i]; r[sum[i]+n]++; } for (int i=1-n;i<=n-1;i++) ans+=l[i+n]*r[(n<<1)-(i+n)]; printf ("%d", ans); return 0;}
"bzoj1303" [CQOI2009] median figure