题解
首先将大于b的赋值为1,小于b的赋值为-1,pos记录b出现的位置,l[i]记录b左边的和为i的数的个数,r[i]记录b右边的和为i的个数。最后统计一下即可。
代码
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cctype>
#define N 200010
using namespace std;
int l[N],r[N],sum[N],x,n,b,pos,ans;
inline int read()
{
int ret=0;char ch=getchar();
for (;!isdigit(ch);ch=getchar());
for (;isdigit(ch);ch=getchar()) ret=ret*10+ch-'0';
return ret;
}
int main()
{
n=read();b=read();
for (int i=1;i<=n;i++)
{
x=read();
if (x>b) sum[i]=1;
else
if (x<b) sum[i]=-1;
else pos=i;
}
l[n]=1;r[n]=1;
for (int i=pos+1;i<=n;i++) sum[i]+=sum[i-1],r[sum[i]+n]++;
for (int i=pos-1;i>=1;i--) sum[i]+=sum[i+1],l[sum[i]+n]++;
for (int i=0;i<=2*n-1;i++) ans+=(l[i]*r[2*n-i]);
printf("%d\n",ans);
return 0;
}