参考:http://www.cnblogs.com/widsom/p/9290269.html

传送门:http://codeforces.com/contest/1005/problem/E2

题意:求一段数列中,取其中中位数为m的子序列个数有几个;

思路:首先我们可以先求出——序列中大于等于 m的数占多数的子序列——有多少个。然后,再求出序列中大于等于m+1的数占多数的子序列有多少个。

前面序列的个数减去后面的序列个数,就是答案。

显然这两个个数的求法是一样的。具体来说,

因为要计算区间的大于等于m个数是否占多数,把大于等于m的记为1,小于的记为-1;

计算前缀和cnt[i]。

枚举右端点t, 1 ~ t 间大于m的个数就是cnt[ t ],这个时候,找到左端点q个数,要求 1 ~ q 的cnt [ q ]小于cnt[ t ],   这个q的个数就是对应右端点为 t 时子序列的个数,加到ans中。

怎么找到q的个数,如果从1 ~ i枚举是会超时的,这时候就出了树状数组,感觉前缀和用树状数组很方便。

把cnt[q] 加上  n  再add进树状数组中。

这里有个细节就是,开始的时候要add(n+1),因为还要考虑左端点为0的情况。

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long ll; int n,m;
const int maxn = 2e5+;
int a[maxn];
ll sum[maxn*],cnt[maxn*];
int lowbit(int x){
return x & (-x);
} void add(int x){
for(int i=x; i<=*n; i += lowbit(i)){
sum[i]++;
}
} ll getsum(int x){
ll res = ;
for(int i=x; i>; i-=lowbit(i)){
res += sum[i];
}
return res;
} ll solve(int x){ memset(cnt,,sizeof(cnt));
memset(sum,,sizeof(sum)); for(int i=; i<=n; i++){
cnt[i] = cnt[i-] + (a[i]>=x?:-);
}
ll ans = ;
add(n+);
for(int i=; i<=n; i++){
ans += getsum(cnt[i]+n);
add(cnt[i]+n+);
}
return ans;
} 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 ;
}

1005E2

CodeForces -Codeforces Round #496 (Div. 3) 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 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 - 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, ...

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

  7. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  8. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

  9. Codeforces Beta Round #79 (Div. 2 Only)

    Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...

随机推荐

  1. jdk1.8HashMap底层数据结构:散列表+链表+红黑树,jdk1.8HashMap数据结构图解+源码说明

    一.前言 本文由jdk1.8源码整理而得,附自制jdk1.8底层数据结构图,并截取部分源码加以说明结构关系. 二.jdk1.8 HashMap底层数据结构图 三.源码 1.散列表(Hash table ...

  2. 显示Mac隐藏文件的命令:

    设置查看隐藏文件的方法如下:打开终端,输入命名 显示Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -bool true 隐藏 ...

  3. Java连载10-数据类型取值范围&转义字符

    一.数据类型取值范围 二.八种数据类型在成员变量中的默认值 (1)成员变量,没有赋值,编译不会报错,系统会自动给赋值 byte\int\short\long默认值为0:float\double默认值为 ...

  4. .NET Core on K8S学习实践系列文章索引(Draft版)

    一.关于这个系列 自从去年(2018年)底离开工作了3年的M公司加入X公司之后,开始了ASP.NET Core的实践,包括微服务架构与容器化等等.我们的实践是渐进的,当我们的微服务数量到了一定值时,发 ...

  5. 浅析scrapy与scrapy_redis区别

    最近在工作中写了很多 scrapy_redis 分布式爬虫,但是回想 scrapy 与 scrapy_redis 两者区别的时候,竟然,思维只是局限在了应用方面,于是乎,搜索了很多相关文章介绍,这才搞 ...

  6. React 如何搭建脚手架

    React 如何搭建脚手架   npm install -g create-react-app    //安装 create-react-app react-demo    // react-demo ...

  7. MyBatis之#{} and ${}

    #{} 和 ${} 之间最大的差别就是  #{}会在使用的时候被加上 ‘’ 引号, ${}直接传值,不做任何处理 1.#{}对传入的参数会做预编译,也就是会当做字符串来处理 select * from ...

  8. Python3源代码编译安装

    Python3源代码编译安装 安装必要工具 yum-utils ,它的功能是管理repository及扩展包的工具 (主要是针对repository) $ sudo yum install yum-u ...

  9. JavaWeb——JSP表达式语言(EL)

    1.JSP表达式语言(EL)用于在jsp从访问存储在JavaBean中的数据,例如 User ID: ${user.userId}<br /> 这里的${user.userId}就是JSP ...

  10. 洛谷 P5367 【模板】康托展开(数论,树状数组)

    题目链接 https://www.luogu.org/problem/P5367 什么是康托展开 百度百科上是这样说的:   “康托展开是一个全排列到一个自然数的双射,常用于构建哈希表时的空间压缩. ...