E2 - Median on Segments (General Case Edition)

思路:

首先我们计算出solve(m):中位数大于等于m的方案数,那么最后答案就是solve(m) - solve(m+1)

那么怎么计算sovle(m)呢?

对于一个区间[l,r],如果它的中位数大于等于m,那么这个区间中 (大于等于m的数的个数) > (小于m的数的个数)

如果记a[i]大于等于m为+1,小于m 为 -1,即 sum(l, r)  > 0

我们枚举右端点 i ,并且同时计算sum(1, i) ,那么对于这个右端点,我们只要找到之前的 sum 中 < sum(1, i)的个数(左端点的个数),这个可以用树状数组维护

但是我们有一个O(n)的方法求,用了类似莫队的方法,记s[i]为之前的sum为i的个数,add为上一个小于sum(1, i-1)的个数,对于当前的sum,

如果它要加1,add += s[sum],  sum++

如果它要减1,sum --, add -= s[sum]

这样得出的add就是当前的小于sum(1, i)的个数

代码:

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pii pair<int, int>
#define piii pair<int,pii>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head const int N = 2e5 + ;
int a[N], cnt[N*], n, m;
LL solve(int m) {
int s = n;
mem(cnt, );
cnt[s] = ;
LL add = , ans = ;
for (int i = ; i <= n; i++) {
if(a[i] >= m) add += cnt[s], s++;
else s--, add -= cnt[s];
cnt[s]++;
ans += add;
}
return ans;
}
int main() {
scanf("%d %d", &n, &m);
for (int i = ; i <= n; i++) scanf("%d", &a[i]);
printf("%lld\n", solve(m) - solve(m+));
return ;
}

Codeforces 1005 E2 - Median on Segments (General Case Edition)的更多相关文章

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

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

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

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

  4. Codeforces #496 E1. Median on Segments (Permutations Edition)

    http://codeforces.com/contest/1005/problem/E1 题目 https://blog.csdn.net/haipai1998/article/details/80 ...

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

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

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

  8. Codeforces Round #535 E2-Array and Segments (Hard version)

    Codeforces Round #535 E2-Array and Segments (Hard version) 题意: 给你一个数列和一些区间,让你选择一些区间(选择的区间中的数都减一), 求最 ...

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

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

随机推荐

  1. Vue小案例 之 商品管理------为之前的页面修改样式

    最终修改的页面效果: 修改的css: <style> #container{ margin: auto; text-align: center; width: 1000px; border ...

  2. SPOJ SUBLEX Lexicographical Substring Search - 后缀数组

    题目传送门 传送门I 传送门II 题目大意 给定一个字符串,多次询问它的第$k$大本质不同的子串,输出它. 考虑后缀Trie.依次考虑每个后缀新增的本质不同的子串个数,显然,它是$n - sa[i] ...

  3. C++max的使用方法

    #include <iostream> //#include <algorithm>//std::min std::max #include <stdint.h> ...

  4. Node.js实践

    在 iOS 模拟器中调试 Web 页面 safari调试iOS App web 1, npm init 2, npm install ejs --save 简单Node 指令 $ node -v  / ...

  5. topcoder srm 695 div1 -3

    1.称一个串的子串(连续一段)为$same$当且仅当这个子串所有字符都一样.子串不同当且仅当在原串中的起始位置不同.现在构造一个长度为$n$的只包含字符'a','b'的串$s$,使得$s$满足长度为$ ...

  6. topcoder srm 708 div1 -3

    1.定义一个字符串s,定义函数$f(s)=\sum_{i=1}^{i<|s|}[s_{i-1}\neq s_{i}]$,给定字符串$p,q$,定义函数$g(p,q)=\sum_{c='a'}^{ ...

  7. Mac通过安装Go2Shell实现“在当前目录打开iTerm2”

    先上效果图: 1.从官网下载最新的版本,不要从苹果商店下载,因为苹果商店的版本比较旧,只支持Finders10.6~10.10,不支持最新的版本 http://zipzapmac.com/Go2She ...

  8. cygwin如何下编译安装tmux?

    1. 准备工作 1.1 安装ncurses开发库 apt-cyg install libncurses-deve 1.2 安装libevent apt-cyg install libevent-dev ...

  9. Spring整合Redis&JSON序列化&Spring/Web项目部署相关

    几种JSON框架用法和效率对比: https://blog.csdn.net/sisyphus_z/article/details/53333925 https://blog.csdn.net/wei ...

  10. (转) The care and maintenance of your adviser

    The care and maintenance of your adviser Ever since the advent of graduate school, students have com ...