E2 - Median on Segments (General Case Edition)

题目大意:给你一个数组,求以m为中位数的区间个数。

思路:很巧秒的转换,我们把<= m 数记为1, >m的数 记为-1, 求其前缀,  我们将问题转变成求以<= m 的数作为中位数的区间个数,

答案就变为ans(m) - ans(m - 1),我们可以用上面求得的前缀用bit就能求出答案。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define pii pair<int, int> using namespace std; const int N = 4e5 + ;
const int M = 1e5 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 +; int n, m, a[N], b[N];
LL val[N]; void modify(int x, int v) {
for(int i = x; i < N; i += i & -i)
val[i] += v;
} LL sum(int x) {
LL ans = ;
for(int i = x; i; i -= i & -i)
ans += val[i];
return ans;
} LL cal(int m) {
memset(val, , sizeof(val));
for(int i = ; i <= n; i++) {
b[i] = (a[i] <= m ? : -);
} for(int i = ; i <= n; i++) b[i] += b[i - ]; modify(n + , ); LL ans = ;
for(int i = ; i <= n; i++) {
ans += sum(b[i] + n + );
modify(b[i] + n + , );
}
return ans;
} int main() { scanf("%d%d", &n, &m);
for(int i = ; i <= n; i++) {
scanf("%d", &a[i]);
} printf("%lld\n", cal(m) - cal(m - ));
return ;
} /*
3
3 2
*/

Codeforces Round #496 (Div. 3) E2 - Median on Segments (General Case Edition)的更多相关文章

  1. 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 题意 ...

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

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

  3. 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 ...

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

    题意:给你一个数组,求有多少子数组的中位数等于\(m\).(若元素个数为偶数,取中间靠左的为中位数). 题解:由中位数的定义我们知道:若数组中\(<m\)的数有\(x\)个,\(>m\)的 ...

  5. Codeforces Round #535 (Div. 3) E2. Array and Segments (Hard version) 【区间更新 线段树】

    传送门:http://codeforces.com/contest/1108/problem/E2 E2. Array and Segments (Hard version) time limit p ...

  6. CodeForces - 1005E2:Median on Segments (General Case Edition) (函数的思想)

    You are given an integer sequence a1,a2,…,ana1,a2,…,an. Find the number of pairs of indices (l,r)(l, ...

  7. Codeforces Round #496 (Div. 3) ABCDE1

    //B. Delete from the Left #include <iostream> #include <cstdio> #include <cstring> ...

  8. Codeforces Round #567 (Div. 2) E2 A Story of One Country (Hard)

    https://codeforces.com/contest/1181/problem/E2 想到了划分的方法跟题解一样,但是没理清楚复杂度,很难受. 看了题解觉得很有道理,还是自己太菜了. 然后直接 ...

  9. Codeforces Round #327 (Div. 2) C. Median Smoothing 找规律

    C. Median Smoothing Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/591/p ...

随机推荐

  1. JQuery学习三(隐式迭代和节点遍历)

    在JQuery中根据id获取控件,如果输入id错误是不报错的. 必要时可以通过写判断语句进行判断是否id写错 <!DOCTYPE html> <html xmlns="ht ...

  2. 线程池 ------ linux C实现

    大多数的网络服务器,包括Web服务器都具有一个特点,就是单位时间内必须处理数目巨大的连接请求,但是处理时间却是比较短的.在传统的多线程服务器模型中是这样实现的:一旦有个请求到达,就创建一个新的线程,由 ...

  3. lnmp环境不支持require 解决方法

    lnmp环境配置: 3处改动地方---->排查过程: 从phpinfo.php中查找php.ini-----所在位置 /usr/local/php/etc/php.ini 1.display_e ...

  4. (译)理解python线程

    看到一篇老外写的线程文章,很赞,零基础都能看懂.先贴在这里,有时间再翻译出来. http://agiliq.com/blog/2013/09/understanding-threads-in-pyth ...

  5. 超酷算法-BK树

    前几天无意间遇到一个博客,觉得写得挺好的,自己之前的时候有个不好的习惯,那就是遇到了好资源第一反应就是收藏起来然后却很少再看!!这是坏习惯,要改!于是今天就开始通读了,读的第二篇是BK树.觉得有点意思 ...

  6. 【BZOJ1926】【SDOI2010】粟粟的书架 [主席树]

    粟粟的书架 Time Limit: 30 Sec  Memory Limit: 552 MB[Submit][Status][Discuss] Description 幸福幼儿园 B29 班的粟粟是一 ...

  7. github: Permission denied (publickey). 问题解决方法

    部署服务器过程中想clone自己github中的库,结果出现Permission denied (publickey).的错误,解决方法是添加服务器公钥到github的settings->SSH ...

  8. js_面向对象设计和行为委托设计模式

    最近换了新工作,面试过程中有多多少少的问题没有给的出答案,为自己的技术短板而促急. javascript中万物皆对象(键:值构成的一种数据),暂且不讨论这个句话的对与错,可以想象对象在javascri ...

  9. hdu 1272 小希的迷宫(并查集+最小生成树+队列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1272 小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)     ...

  10. Java中基于HotSpot虚拟机的垃圾收集器

    名称 过程 优缺点 Serial 进行垃圾收集时,必须暂停其他所有的工作进程,直到它收集结束.是一个单线程收集器. Stop the world. 新生代收集器. 手工设置新生代的大小:-Xmn Ed ...