[luoguP1168]中位数(主席树+离散化)
模板题一道,1A。
——代码
#include <cstdio>
#include <algorithm>
#define ls son[now][0], l, mid
#define rs son[now][1], mid + 1, r using namespace std; const int N = ; int n, sz, tot;
int a[N], b[N], sum[ * N], rt[ * N], son[ * N][]; inline void build(int &now, int l, int r)
{
now = ++tot;
if(l == r) return;
int mid = (l + r) >> ;
build(ls);
build(rs);
} inline void update(int &now, int l, int r, int last, int x)
{
now = ++tot;
sum[now] = sum[last] + ;
son[now][] = son[last][];
son[now][] = son[last][];
if(l == r) return;
int mid = (l + r) >> ;
if(x <= mid) update(ls, son[now][], x);
else update(rs, son[now][], x);
} inline int query(int now, int l, int r, int x)
{
if(l == r) return l;
int mid = (l + r) >> , cnt = sum[son[now][]];
if(x <= cnt) return query(ls, x);
else return query(rs, x - cnt);
} int main()
{
int i, k;
scanf("%d", &n);
for(i = ; i <= n; i++) scanf("%d", &a[i]), b[i] = a[i];
sort(b + , b + n + );
sz = unique(b + , b + n + ) - (b + );
build(rt[], , sz);
for(i = ; i <= n; i++)
{
k = lower_bound(b + , b + sz + , a[i]) - b;
update(rt[i], , sz, rt[i - ], k);
if(i % == ) printf("%d\n", b[query(rt[i], , sz, (i + ) >> )]);
}
return ;
}
[luoguP1168]中位数(主席树+离散化)的更多相关文章
- Bzoj 2588: Spoj 10628. Count on a tree 主席树,离散化,可持久,倍增LCA
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2588 2588: Spoj 10628. Count on a tree Time Limit ...
- [HDU4417]Super Mario(主席树+离散化)
传送门 又是一道主席树模板题,注意数组从0开始,还有主席树耗费空间很大,数组开大点,之前开小了莫名其妙TLE.QAQ ——代码 #include <cstdio> #include < ...
- [luoguP2617] Dynamic Ranking(树状数组 套 主席树 + 离散化)
传送门 BZOJ上是权限题,洛谷赞啊. 求区间 K 大数很简单. 但是如果修改某个数的话,那么就得把这个数及后面所建的主席树都更新一遍 nlogn,显然不行. 所以可以在外面套一个树状数组来优化,树状 ...
- FZU 2237 中位数 主席树 树上k大
#include <cstdio> #include <cstring> #include <queue> #include <set> #includ ...
- BZOJ3932 CQOI2015 任务查询系统 - 主席树,离散化
记录下自己写错的地方吧 1. 区间可能有重复 2. 没有出现的坐标也要计入version (因为询问里可能会有) #include <bits/stdc++.h> using namesp ...
- SPOJ 3267 D-query(离散化+主席树求区间内不同数的个数)
DQUERY - D-query #sorting #tree English Vietnamese Given a sequence of n numbers a1, a2, ..., an and ...
- SDUT 2610 Boring Counting(离散化+主席树区间内的区间求和)
Boring Counting Time Limit: 3000MS Memory Limit: 65536KB Submit Statistic Discuss Problem Descriptio ...
- HDU 4417 Super Mario(主席树求区间内的区间查询+离散化)
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- POJ 2104&HDU 2665 Kth number(主席树入门+离散化)
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 50247 Accepted: 17101 Ca ...
随机推荐
- Apache Kylin Cube 的存储
不多说,直接上干货! 简单的说Cuboid的维度会映射为HBase的Rowkey,Cuboid的指标会映射为HBase的Value. Cube映射成HBase存储 如上图原始表所示:Hive表有两个维 ...
- AJPFX辨析Java中运算符 ++ 和 += 的区别
我们都知道Java中 ++ 和 +=1 都是把数字增加一后,把值赋给左边,那二者有什么区别呢? i+=1 运行的底层Heap申请一个区域存放i,在数据区域开劈一个区域存放1,2个内存段被数据被送入到 ...
- PHP设计模式 原型模式(Prototype)
定义 和工厂模式类似,用来创建对象.但实现机制不同,原型模式是先创建一个对象,采用clone的方式进行新对象的创建. 场景 大对象的创建. 优点 1.可以在运行时刻增加和删除产品 2.可以改变值或结构 ...
- poj3280 Cheapest Palindrome
思路: 区间dp.添加和删除本质相同. 实现: #include <iostream> #include <cstdio> using namespace std; int n ...
- requirejs&&springboot
1.Spring Boot Spring boot 基础结构主要有三个文件夹: (1)src/main/java 程序开发以及主程序入口 (2)src/main/resources 配置文件 (3) ...
- canvas防画图工具
<style> body { background: black; text-align: center; } #cans { background: white; } < ...
- Android 仿百度医生的智能分诊界面
http://blog.csdn.net/jiang89125/article/details/49622853
- Redis为什么这么快
Redis为什么这么快 1.完全基于内存,绝大部分请求是纯粹的内存操作,非常快速.数据存在内存中,类似于HashMap,HashMap的优势就是查找和操作的时间复杂度都是O(1): 2.数据结构简单, ...
- RFS自动化测试(一)
RFS 即 Robot Framework + Selenium RFS 的安装 1. python https://www.python.org/ RF框架是基于python的,所以要先安装有pyt ...
- Spotlight安装
刚才技术群的一个朋友在安装Spotligh出现了一些问题,所以本人临时写个简单的教程 1.下载安装包(安装包地址:https://pan.baidu.com/s/1c2tmqyc),解压,然后傻瓜式安 ...