# BZOJ 1303: [CQOI2009]中位数图（前缀/后缀和乱搞）

Posted by 111qqz on Monday, January 30, 2017

## 1303: [CQOI2009]中位数图

Time Limit: 1 Sec  Memory Limit: 162 MB
Submit: 2480  Solved: 1529
[Submit][Status][Discuss]

7 4
5 7 2 4 3 1 6

4

## HINT

N<=100000

``````/* ***********************************************
Author :111qqz
Created Time :2017年01月30日 星期一 20时13分11秒
File Name :code/bzoj/1303.cpp
************************************************ */

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#define fst first
#define sec second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ms(a,x) memset(a,x,sizeof(a))
typedef long long LL;
#define pi pair < int ,int >
#define MP make_pair

using namespace std;
const double eps = 1E-8;
const int dx4[4]={1,0,0,-1};
const int dy4[4]={0,-1,1,0};
const int inf = 0x3f3f3f3f;
const int N=1E5+7;
int a[N];
int sum[N];
int l[N*2],r[N*2];
int n,b;
int pos;
int main()
{
#ifndef  ONLINE_JUDGE
freopen("code/in.txt","r",stdin);
#endif
scanf("%d%d",&n,&b);
for ( int i = 1 ; i <= n ; i++)
{
scanf("%d",&a[i]);
if (a[i]==b) a[i] = 0,pos = i;
if (a[i]<b) a[i] = -1;
if (a[i]>b) a[i] = 1;
}
l[n]=r[n]=1;//b所在的位置
int ans = 0;
for ( int i = pos-1 ; i >=1 ; i--) {sum[i]=sum[i+1]+a[i];l[sum[i]+n]++;}
for ( int i = pos+1 ; i <=n ; i++) {sum[i]=sum[i-1]+a[i];r[sum[i]+n]++;}
for ( int i = 0 ; i <= 2*n ; i++) ans +=l[i]*r[-i+2*n];
cout<<ans<<endl;

#ifndef ONLINE_JUDGE
fclose(stdin);
#endif
return 0;
}
``````

「真诚赞赏，手留余香」