参考: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. 【Android】error: Error retrieving parent for item: No resource found that matches the given name 'Theme.Sherlock.Light.NoActionBar'.

    问题: res 文件夹下的 values 下的 styles.xml <style name="Sherlock.Light.NoActionBar" parent=&quo ...

  2. 自定义SWT控件六之自定义Tab

    6.自定义tab 本章节提供的自定义tab 分为两类 tab上带删除按钮和添加按钮,可删除tab和添加tab tab不可删除和添加 6.1 不可删除tab package com.view.contr ...

  3. maven 下载 安装 环境配置

    电脑系统:win10  64位   idea 2019    Java 1.8 1.链接地址,我一般都找官网 http://maven.apache.org/download.cgi 截图:注意mav ...

  4. PythonDay03

    ## 第三章 ### 今日内容 1.整型 2.布尔值 3.字符串 ​ 索引​ 切片​ 步长​ 字符串的方法 4.for循环 ### 1.整型 - python3:全部是整形- python2:整形,长 ...

  5. 【React踩坑记一】React项目中禁用浏览器双击选中文字的功能

    常规项目,我们只需要给标签加一个onselectstart事件,return false就可以 例: <div onselectstart="return false;" & ...

  6. centos开发环境安装

    执行 yum install gcc gcc-c++ gcc-g77 flex bison autoconf automake bzip2-devel zlib-devel ncurses-devel ...

  7. hadoop学习(三)----hadoop2.x完全分布式环境搭建

    今天我们来完成hadoop2.x的完全分布式环境搭建,话说学习本来是一件很快乐的事情,可是一到了搭环境就怎么都让人快乐不起来啊,搭环境的时间比学习的时间还多.都是泪.话不多说,走起. 1 准备工作 开 ...

  8. ES 26 - 通过partial update局部更新索引文档 (partial update增量修改原理)

    目录 1 什么是partial update 1.1 全量修改文档的原理 1.2 修改指定field的思路 1.3 partial update的优势 1.4 partial update的使用 2 ...

  9. 洛谷 P3338 [ZJOI2014]力

    题意简述 读入\(n\)个数\(q_i\) 设\(F_j = \sum\limits_{i<j}\frac{q_i\times q_j}{(i-j)^2 }-\sum\limits_{i> ...

  10. MTFlexbox自动化埋点探索

    1. 背景 跨平台动态化技术是目前移动互联网领域的重点关注方向,它既能节约人力,又能实现业务快速上线的需求.经过十年的发展,美团App已经变成了一个承载众多业务的超级平台,众多的业务方对业务形态的快速 ...