比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\n",ans);
    return 0;
}
View Code

相关文章:

  • 2022-02-08
  • 2022-01-23
  • 2022-12-23
  • 2022-01-07
  • 2021-07-02
  • 2022-03-05
  • 2021-07-12
猜你喜欢
  • 2021-11-17
  • 2021-12-16
  • 2021-07-21
  • 2022-12-23
  • 2021-07-02
  • 2021-07-26
相关资源
相似解决方案