思路分析:
根据中位数的性质,我们可以知道在一定的区间范围内,所有大于它与小于它的数的个数是相等的。
所以如果一个区间满足大于中位数与小于中位数的个数相等,且中位数在其中,则是符合题意的区间。(设大于中位数个数与小于中位数个数均为 n ,加上中位数自己本身,总长度为 图片说明,长度必定为奇数,无需特判。)
在读入的时候,将大于中位数的数据赋值 1 ,小于中位数的数据赋值 1 ,等于中位数的数据赋值 0 ,并记录中位数的下标,左右遍历统计即可。
左遍历的时候如果区间和为0 ,区间存在则 ,并利用 Map 统计 sums 的情况,如果右边有区间和为-sums ,则可以与左边的sums 配对。
中位数本身就是符合条件的区间,所以有 图片说明

Code:

#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#define MaxN 100010
using namespace std;
typedef long long LL;
int A[MaxN];
map <int,int> Map;
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int n,m,id;
    cin>>n>>m;
    for(int i=0; i<n; i++){
        cin>>A[i];
        if( A[i] < m ) A[i]=-1;
        if( A[i] == m ){
            A[i]=0;
            id=i;
        }
        if( A[i] > m ) A[i]=1;
    }
    int sums=0,ans=1;
    Map[0]++;
    for(int i=id-1; i>=0; i--){
        sums+=A[i];
        if( sums == 0 ) ans++;
        Map[sums]++;
    }
    sums=0;
    for(int i=id+1; i<n; i++){
        sums+=A[i];
        ans+=Map[-sums];
    }
    cout<<ans;
    return 0;
}