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. Django设计模式

    单例模式: 建造者模式: 示例: from enum import Enum import time PizzaProgress = Enum('PizzaProgress', 'queued pre ...

  2. bash 变量

    本地变量: 变量赋值:name=value 变量引用:${name}  , $name "":变量名会替换为其值 '':变量名不会替换为其值 查看变量: set 撤销变量:unse ...

  3. RAC +MVVM

    https://blog.csdn.net/capf_sam/article/details/60960530 https://blog.csdn.net/capf_Sam/article/detai ...

  4. linux下如何删除行首的数字?

    举例如下: 1.某文件jello.txt中有以下行 1111-yes 2222-no 3333-yes-no-no 2.删除jello.txt中每行行首的数字 cat jello.txt | cut ...

  5. Many Website Of WallPaper

    我在这里给大家推荐几个不错的壁纸网站 毕竟一张赏心悦目的壁纸能让你的工作效率提高不少 注意前方高能 一大波网站即将来袭 一系列 如你所见 alphacoders wallpaperdj Wallhav ...

  6. 18 Issues in Current Deep Reinforcement Learning from ZhiHu

    深度强化学习的18个关键问题 from: https://zhuanlan.zhihu.com/p/32153603 85 人赞了该文章 深度强化学习的问题在哪里?未来怎么走?哪些方面可以突破? 这两 ...

  7. Graph Convolutional Networks (GCNs) 简介

    Graph Convolutional Networks 2018-01-16  19:35:17 this Tutorial comes from YouTube Video:https://www ...

  8. 《剑指Offer 1.二维数组中的查找》2019-03-25

    剑指Offer  第一题 题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数 ...

  9. Gitflow 视频讲解

      How to use a scalable Git branching model called Gitflow   git视频合集列表   github大本营   gitflow中文备忘清单   ...

  10. c# 泛型的应用

    泛型静态类 & function作为参数 /// <summary> /// /// </summary> /// <typeparam name="O ...