You are given an integer sequence a1,a2,…,ana1,a2,…,an.

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

The median of a sequence is the value of an 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 median of al,al+1,…,aral,al+1,…,ar is exactly the given number mm.

Input

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

The second line contains an integer sequence a1,a2,…,ana1,a2,…,an (1≤ai≤2⋅1051≤ai≤2⋅105).

Output

Print the required number.

Examples

Input
5 4
1 4 5 60 4
Output
8
Input
3 1
1 1 1
Output
6
Input
15 2
1 2 3 1 2 3 1 2 3 1 2 3 1 2 3
Output
97

Note

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

题意:给定N,M,已经N个数。问有多少哥区间,排序后其中位数是M。

思路:这个序列中如果只有一个M,那么可以理由前缀和的思路求解。我们把大于M的数看成1,小于M的数看成-1,那么问题就成了有多少个区间的区间和为0或者1,直接用map搞前缀和即可。现在有多个M,那么再这么搞可能会重复。

我们把此题用函数的思想来搞,同样的,把大于等于M的数看成1,小于M的数看成-1,令F(x)表示M=x的时候,有多少个区间和大于0。

那么结果就是F(M)-F(M+1)。那么现在问题就算求解函数F。

对于函数F(x),把大于大于x的数转化为1,否则为-1,那么现在数列是一系列的1和-1串,记录前缀和,然后可以用树状数组搞定即可,复杂度为O(N*lgN)。

但是由于只有1和-1,我们耶可以利用其特殊性,保留有效信息,把复杂度做到O(N);now表示前缀和,delta表示前面有多个位置可以满足区间和大于0,sum[x]表示前缀和为x的个数,那么新加入一个1时,delta显然会增加sum[now]个,然后now++。 加入一个-1时,delta会减少sum[now-1]个,now--。

(此题转化为函数的思想,然后做减法,妙的。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=;
int N,M,a[maxn],sum[maxn]; ll ans;
ll solve(int num)
{
memset(sum,,sizeof(sum));
int now=N; ll res=,delta=; sum[now]=;
for(int i=;i<=N;i++){
if(a[i]>=num) delta+=sum[now],now++;
else now--,delta-=sum[now];
res+=delta;
sum[now]++;
}
return res;
}
int main()
{
scanf("%d%d",&N,&M);
for(int i=;i<=N;i++) scanf("%d",&a[i]);
printf("%I64d\n",solve(M)-solve(M+));
return ;
}

CodeForces - 1005E2:Median on Segments (General Case Edition) (函数的思想)的更多相关文章

  1. Codeforces 1005 E2 - Median on Segments (General Case Edition)

    E2 - Median on Segments (General Case Edition) 思路: 首先我们计算出solve(m):中位数大于等于m的方案数,那么最后答案就是solve(m) - s ...

  2. Codeforces Round #496 (Div. 3) E2 - Median on Segments (General Case Edition)

    E2 - Median on Segments (General Case Edition) 题目大意:给你一个数组,求以m为中位数的区间个数. 思路:很巧秒的转换,我们把<= m 数记为1, ...

  3. CodeForces -Codeforces Round #496 (Div. 3) E2. Median on Segments (General Case Edition)

    参考:http://www.cnblogs.com/widsom/p/9290269.html 传送门:http://codeforces.com/contest/1005/problem/E2 题意 ...

  4. Codeforces Round #496 (Div. 3 ) E1. Median on Segments (Permutations Edition)(中位数计数)

    E1. Median on Segments (Permutations Edition) time limit per test 3 seconds memory limit per test 25 ...

  5. CF1005E1 Median on Segments (Permutations Edition) 思维

    Median on Segments (Permutations Edition) time limit per test 3 seconds memory limit per test 256 me ...

  6. 贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas

    题目传送门 /* 题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步 贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0 ...

  7. 构造 Codeforces Round #310 (Div. 2) B. Case of Fake Numbers

    题目传送门 /* 题意:n个数字转盘,刚开始每个转盘指向一个数字(0~n-1,逆时针排序),然后每一次转动,奇数的+1,偶数的-1,问多少次使第i个数字转盘指向i-1 构造:先求出使第1个指向0要多少 ...

  8. 找规律/贪心 Codeforces Round #310 (Div. 2) A. Case of the Zeros and Ones

    题目传送门 /* 找规律/贪心:ans = n - 01匹配的总数,水 */ #include <cstdio> #include <iostream> #include &l ...

  9. Codeforces Round #538 (Div. 2) F 欧拉函数 + 区间修改线段树

    https://codeforces.com/contest/1114/problem/F 欧拉函数 + 区间更新线段树 题意 对一个序列(n<=4e5,a[i]<=300)两种操作: 1 ...

随机推荐

  1. APU的Vsense引脚的作用

    JACK学习文档推荐: 开关电源PCB布局注意事项 开关电源PCB布线注意事项 一.Sense电压检测(FB) “Sense+”和“Sense-”,就是四线制中的电压检测线,high-sense 和l ...

  2. Oracle 10g ORA-12154: TNS: could not resolve the connect identifier specified 问题解决! 我同事遇到的问题。 username/

    Oracle 10g ORA-12154: TNS: could not resolve the connect identifier specified 问题解决! 我同事遇到的问题. userna ...

  3. react build和server start

    先到项目目录build项目 npm run build 项目会打包到dist文件夹下 index.html和index.js等 react的项目build后不能直接访问的问题 先执行 npm inst ...

  4. python 基础 2.6 for 循环 和if循环 中break

    python中最基本的语法格式大概就是缩进了.python中常用的循环:for循环,if循环.一个小游戏说明for,if ,break的用法. 猜数字游戏: 1.系统生成一个20以内的随机数 2.玩家 ...

  5. TP框架部分---空控制器

    <?php namespace Admin\Controller; use Think\Controller; class DengLuController extends Controller ...

  6. 编译安装Heartbeat常见错误

    -----------那些需要升级包还有少包的错误就不写了---------- <b>1</b>. Reusable-Cluster-Components-glue-glue- ...

  7. AWS:2.根设备类型、EC2生命周期状态、User Data

    主要内容 1.根设备类型 linux: /dev/sda1 windows: 系统盘 2.实例生命周期 生命周期状态:停止.终止.重启 3.用户数据(UserData) 实例在初始化,运行之前给定的用 ...

  8. Netty聊天室-源码

    目录 Netty聊天室 源码工程 写在前面 [百万级流量 聊天室实战]: [分布式 聊天室] [Spring +Netty]: [Netty 原理] 死磕 系列 [提升篇]: [内力大增篇]: 疯狂创 ...

  9. 我的Android进阶之旅------>Android中ListView中嵌套(ListView)控件时item的点击事件不起作的问题解决方法

    开发中常常需要自己定义Listview,去继承BaseAdapter,在adapter中按照需求进行编写,问题就出现了,可能会发生点击每一个item的时候没有反应,无法获取的焦点. 如果你的自定义Li ...

  10. 我的Java开发学习之旅------>解惑Java进行三目运算时的自动类型转换

    今天看到两个面试题,居然都做错了.通过这两个面试题,也加深对三目运算是的自动类型转换的理解. 题目1.以下代码输出结果是(). public class Test { public static vo ...