比b小的设为-1,大的设为1,前缀和搞一下就行了
#include<iostream> #include<cstring> #include<cstdlib> #include<cstdio> #include<algorithm> #define ll long long using namespace std; const int maxn=500010,inf=1e9; int n,b,x,pos; int a[maxn],v[maxn]; ll ans; void read(int &k) { int f=1;k=0;char c=getchar(); while(c<'0'||c>'9')c=='-'&&(f=-1),c=getchar(); while(c<='9'&&c>='0')k=k*10+c-'0',c=getchar(); k*=f; } int main() { read(n);read(b); for(int i=1;i<=n;i++) { read(x); if(x<b)a[i]=-1; else if(x>b)a[i]=1; else a[i]=0,pos=i; a[i]+=a[i-1]; } for(int i=0;i<pos;i++)v[a[i]+n]++; for(int i=pos;i<=n;i++)ans+=v[a[i]+n]; printf("%lld ",ans); return 0; }