传送门

模板题一道,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]中位数(主席树+离散化)的更多相关文章

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

  2. [HDU4417]Super Mario(主席树+离散化)

    传送门 又是一道主席树模板题,注意数组从0开始,还有主席树耗费空间很大,数组开大点,之前开小了莫名其妙TLE.QAQ ——代码 #include <cstdio> #include < ...

  3. [luoguP2617] Dynamic Ranking(树状数组 套 主席树 + 离散化)

    传送门 BZOJ上是权限题,洛谷赞啊. 求区间 K 大数很简单. 但是如果修改某个数的话,那么就得把这个数及后面所建的主席树都更新一遍 nlogn,显然不行. 所以可以在外面套一个树状数组来优化,树状 ...

  4. FZU 2237 中位数 主席树 树上k大

    #include <cstdio> #include <cstring> #include <queue> #include <set> #includ ...

  5. BZOJ3932 CQOI2015 任务查询系统 - 主席树,离散化

    记录下自己写错的地方吧 1. 区间可能有重复 2. 没有出现的坐标也要计入version (因为询问里可能会有) #include <bits/stdc++.h> using namesp ...

  6. SPOJ 3267 D-query(离散化+主席树求区间内不同数的个数)

    DQUERY - D-query #sorting #tree English Vietnamese Given a sequence of n numbers a1, a2, ..., an and ...

  7. SDUT 2610 Boring Counting(离散化+主席树区间内的区间求和)

    Boring Counting Time Limit: 3000MS Memory Limit: 65536KB Submit Statistic Discuss Problem Descriptio ...

  8. HDU 4417 Super Mario(主席树求区间内的区间查询+离散化)

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  9. POJ 2104&HDU 2665 Kth number(主席树入门+离散化)

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 50247   Accepted: 17101 Ca ...

随机推荐

  1. Hash环/一致性Hash原理

    当前,Memcached.Redis这类分布式kv缓存已经非常普遍.从本篇开始,本系列将分析分布式缓存相关的原理.使用策略和最佳实践. 我们知道Memcached的分布式其实是一种“伪分布式”,也就是 ...

  2. Nginx缓存[proxy cache、memcache]

    nginx自带缓存 nginx自己有单独的进程来对磁盘上的缓存文件进行扫描,在内存中建立缓存索引.并且有管理进程来对缓存进行过期判断,更新等操作 定义:只能在http段中使用 proxy_cache_ ...

  3. 关于spring mvc 和struts2的描述与对比

    链接:https://www.nowcoder.com/questionTerminal/cf803beb7e3346caa636e4eaa3a8c2e9来源:牛客网 ---------------- ...

  4. C# 部分命名规则

    接触C#开发已经四个月,整理下C#中的命名规则: 一:变量的命名规则(和Java相似) 1.变量名由字母.数字.下划线组成 2.变量名开头只能以字母.下划线开头,不能以数字开头 3.区分大小写 4.命 ...

  5. C#的WinForm中Label透明一例

    很久之前開發的一個MIS系統,里面有個登錄界面,採用了PictureBox做背景,上面放了一些Label,試了很多方面不能實現透明,如下圖: 這次重新啟用該系統,看了一下原因,很簡單,原來Label的 ...

  6. 【转】彻底解析Android缓存机制——LruCache

    彻底解析Android缓存机制——LruCache 关于Android的三级缓存,其中主要的就是内存缓存和硬盘缓存.这两种缓存机制的实现都应用到了LruCache算法,今天我们就从使用到源码解析,来彻 ...

  7. map,reduce高阶函数

    iterator:迭代器 python的iterator是一个惰性序列(即你不主动去遍历它,他不会去计算其中元素的值) m是一个iterator,所以通过tuple()函数让整个序列计算出来,并返回一 ...

  8. hihocoder offer收割编程练习赛9 B 水陆距离

    思路: 宽搜,多个起点. 实现: #include <iostream> #include <cstdio> #include <algorithm> #inclu ...

  9. 正则表达式中的?=,?!,?<=,?<!(预查)解释小栗子

    之前在学正则表达式的时候学的并不是很透彻 感觉看看元字符(元字符要用 \ 转义),限定符(^开头 $结尾),   前面写个范围[a-z],在后面写个{n,}能匹配就行了 当时的自己 然而昨天我参加了个 ...

  10. Model 模型

    Model 模型模型是你的数据的唯一的.权威的信息源.它包含你所存储的数据的必要字段和行为.通常,每个模型对应数据库中唯一的一张表. 每个模型都是dhango.db.models.Model 的一个P ...