poj2104 主席树 区间K大 在线 无修改
关于主席树:
主席树(Chairman Tree)是一种离线数据结构,使用函数式线段树维护每一时刻离散之后的数字出现的次数,由于各历史版本的线段树结构一致,可以相减得出区间信息,即该区间内出现的数字和对应的数量,由于在线段树内,左子树代表的数字都小与右子树,便可像平衡树一样进行K大询问。新建一颗树是\(O(logn)\),查询一次也为\(O(logn)\)。
比划分树好想&写多了,但是在POJ上比划分树慢一些。
CODE:
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = + ; int id[maxn], root[maxn], a[maxn], b[maxn], n, m, size;
struct chairman_tree_node {
int ls, rs, w;
} T[maxn * ];
void insert(int l, int r, int &pos, int val) {
T[++size] = T[pos], pos = size;
++T[pos].w;
if(l == r) return ;
int mid = (l + r) >> ;
if(val <= mid) insert(l, mid, T[pos].ls, val);
else insert(mid + , r, T[pos].rs, val);
}
int query(int p, int q, int l, int r, int k) {
if(l == r) return l;
int t = T[T[q].ls].w - T[T[p].ls].w;
int mid = (l + r) >> ;
if(k <= t) return query(T[p].ls, T[q].ls, l, mid, k);
else return query(T[p].rs, T[q].rs, mid + , r, k - t);
}
bool cmp(int x, int y) {
return a[x] < a[y];
}
int main() {
scanf("%d%d", &n, &m);
for(int i = ; i <= n; ++i) {
scanf("%d", &a[i]);
id[i] = i;
}
sort(id + , id + n + , cmp);
for(int i = ; i <= n; ++i) {
b[id[i]] = i;
}
for(int i = ; i <= n; ++i) {
root[i] = root[i - ];
insert(, n, root[i], b[i]);
}
for(int i = ; i <= m; ++i) {
int l, r, k;
scanf("%d%d%d", &l, &r, &k);
printf("%d\n", a[id[query(root[l - ], root[r], , n, k)]]);
}
return ;
}
poj2104 主席树 区间K大 在线 无修改的更多相关文章
- poj2104 划分树 区间K大 在线 无修改
博主sbit....对于高级数据结构深感无力,然后这些东西在OI竟然烂大街了,不搞就整个人都不好了呢. 于是我勇猛的跳进了这个大坑 ——sbit 区间K大的裸题,在线,无修改. 可以用归并树(\(O( ...
- F. Greedy Sequence(主席树区间k的后继)(The Preliminary Contest for ICPC Asia Nanjing 2019)
题意: 查找区间k的后继. 思路: 直接主席树. #define IOS ios_base::sync_with_stdio(0); cin.tie(0); #include <cstdio&g ...
- FZU 2237 中位数 主席树 树上k大
#include <cstdio> #include <cstring> #include <queue> #include <set> #includ ...
- hdu 5919 主席树(区间不同数的个数 + 区间第k大)
Sequence II Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Tot ...
- zoj2112 树状数组+主席树 区间动第k大
Dynamic Rankings Time Limit: 10000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu Subm ...
- POJ 2104 K-th Number ( 求取区间 K 大值 || 主席树 || 离线线段树)
题意 : 给出一个含有 N 个数的序列,然后有 M 次问询,每次问询包含 ( L, R, K ) 要求你给出 L 到 R 这个区间的第 K 大是几 分析 : 求取区间 K 大值是个经典的问题,可以使用 ...
- HDU 4348 主席树区间更新
To the moon Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- HDU 4348 To the moon(主席树区间修改)
题意 给你一个区间,支持如下操作: 在一段区间内加上一个值,并生成一个历史版本 查询某个版本下一段区间内的和 回到一个历史版本上并舍弃之后的版本 做法 这就是主席树区间修改裸题啦QwQ 上一篇博客我讲 ...
- POJ 2104 K-th Number 主席树(区间第k大)
题目链接: http://poj.org/problem?id=2104 K-th Number Time Limit: 20000MSMemory Limit: 65536K 问题描述 You ar ...
随机推荐
- selenium - webdriver - ActionChains类(鼠标操作)
ActionChains 类提供了鼠标操作的常用方法: perform(): 执行所有 ActionChains 中存储的行为: context_click(): 右击: double_click() ...
- AtCoder Grand Contest 031 B - Reversi(DP)
B - Reversi 题目链接:https://atcoder.jp/contests/agc031/tasks/agc031_b 题意: 给出n个数,然后现在你可以对一段区间修改成相同的值,前提是 ...
- Codeforces Round #544 (Div. 3) 题解
Codeforces Round #544 (Div. 3) D. Zero Quantity Maximization 题目链接:https://codeforces.com/contest/113 ...
- mysql的concat用法
问题提出:mybatis的mapper文件中的模糊查询: mysql CONCAT()函数用于将多个字符串连接成一个字符串,是最重要的mysql函数之一,下面就将为您详细介绍mysql CONCAT( ...
- (转)如何在windows 2008 安装IIS
首先声明本文转自http://www.pc6.com/infoview/Article_54712.html ,作者为清晨 转载的原因有两个,一是怕原文挂了,而是打算写一下在阿里云部署django的文 ...
- [LeetCode] 9. Palindrome Number ☆
Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negativ ...
- 【PHP】Windows下配置用mail()发送邮件
ZZ:解决windows系统下php.ini邮件配置正确不发送邮件的问题 php mail()函数在windows不能用,需要安装sendmail,假如是用的XAMPP,则已经下载好,不需要重新下载~ ...
- [USACO Mar08] 牛跑步
http://www.cogs.pro/cogs/problem/problem.php?pid=133 ★★★ 输入文件:cowjog.in 输出文件:cowjog.out 简单对比时间 ...
- [USACO13FEB]出租车Taxi
洛谷题目链接:[USACO13FEB]出租车Taxi 题目描述 Bessie is running a taxi service for the other cows on the farm. The ...
- 【NOIP】提高组2013 转圈游戏
[算法]快速幂运算 [题解]ans=(m*10^k+x)%n,用快速幂计算10^k即可,复杂度为O(log k). #include<cstdio> long long n,m,k,x,a ...