Median on Segments (Permutations Edition)
time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a permutation p1,p2,…,pnp1,p2,…,pn. A permutation of length nn is a sequence such that each integer between 11 and nn occurs exactly once in the sequence.

Find the number of pairs of indices (l,r)(l,r) (1≤l≤r≤n1≤l≤r≤n) such that the value of the median of pl,pl+1,…,prpl,pl+1,…,pr is exactly the given number mm.

The median of a sequence is the value of the element which is in the middle of the sequence after sorting it in non-decreasing order. If the length of the sequence is even, the left of two middle elements is used.

For example, if a=[4,2,7,5]a=[4,2,7,5] then its median is 44 since after sorting the sequence, it will look like [2,4,5,7][2,4,5,7] and the left of two middle elements is equal to 44. The median of [7,1,2,9,6][7,1,2,9,6] equals 66 since after sorting, the value 66 will be in the middle of the sequence.

Write a program to find the number of pairs of indices (l,r)(l,r) (1≤l≤r≤n1≤l≤r≤n) such that the value of the median of pl,pl+1,…,prpl,pl+1,…,pr is exactly the given number mm.

Input

The first line contains integers nn and mm (1≤n≤2⋅1051≤n≤2⋅105, 1≤m≤n1≤m≤n) — the length of the given sequence and the required value of the median.

The second line contains a permutation p1,p2,…,pnp1,p2,…,pn (1≤pi≤n1≤pi≤n). Each integer between 11 and nn occurs in pp exactly once.

Output

Print the required number.

Examples
input

Copy
5 4
2 4 5 3 1
output

Copy
4
input

Copy
5 5
1 2 3 4 5
output

Copy
1
input

Copy
15 8
1 15 2 14 3 13 4 8 12 5 11 6 10 7 9
output

Copy
48
Note

In the first example, the suitable pairs of indices are: (1,3)(1,3), (2,2)(2,2), (2,3)(2,3) and (2,4)(2,4).

题意:给你n个数和m,问在这n个数中以m为中位数的区间有多少个?

因为要使m为中位数,肯定是m的值位于这些数的中间的部分,即要有比m大的数和比m小的数,且大的数和小的数要相等或者大的数比小的数多一

#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define debug(a) cout << #a << " " << a << endl
using namespace std;
const int maxn = 2e5 + ;
const int mod = 1e9 + ;
typedef long long ll;
ll a[maxn], vis[maxn];
int main() {
ll n, m;
while( cin >> n >> m ) {
map<ll,ll> mm;
ll pos;
for( ll i = ; i <= n; i ++ ) {
cin >> a[i];
if( a[i] == m ) {
pos = i;
}
}
ll cnt = ;
for( ll i = pos; i <= n; i ++ ) {
if( a[i] > m ) {
cnt ++;
} else if( a[i] < m ) {
cnt --;
}
mm[cnt] ++;
}
ll ans = ;
cnt = ;
for( ll i = pos; i >= ; i -- ) {
if( a[i] > m ) {
cnt ++;
} else if( a[i] < m ) {
cnt --;
}
ans += mm[-cnt];
ans += mm[-cnt]; //个数为偶数,中位数在中间两位的左边一位
}
cout << ans << endl;
}
return ;
}

最新文章

  1. 深入理解CSS动画animation
  2. PAT线性结构_一元多项式求导、按给定步长反转链表、出栈序列存在性判断
  3. 详解rsync算法--如何减少同步文件时的网络传输量
  4. 《深入理解bootstrap》读书笔记:第二章 整体架构
  5. java基础-操作符
  6. Android逆向工程初步(一) 15.4.24
  7. 小白学习mysql之优化基础(EXPLAIN的连接类型)
  8. 【转】const 是左结合的,若左边为空,则再向右结合
  9. [vijos P1512] SuperBrother打鼹鼠
  10. c#动态加载dll文件
  11. GET和POST的主要区别
  12. 巨大bug
  13. [六]SpringMvc学习-文件上传
  14. ubuntu13.10 下一个 g++和gcc 4.8不兼容的问题不能被安装
  15. JDK版本问题 发展史
  16. 事件驱动的简明讲解(python实现)
  17. Mac系统下XAMPP的简单使用
  18. Android广播的发送与接收
  19. java class反编译工具----JD-GUI
  20. TCP/IP通信协议

热门文章

  1. dubbo负载均衡是如何实现的?
  2. 第十五章 LVM管理和ssm存储管理器使用 随堂笔记
  3. c#实现深拷贝的几种方法
  4. 3、K-近邻算法
  5. 记我的一次 Java 服务性能优化
  6. React躬行记(13)——React Router
  7. 一文了解:Redis的AOF持久化
  8. 【Vue的路由,SPA概念】
  9. windows下通过idea连接hadoop和spark集群
  10. 简单了解一下事件循环(Event Loop)