[poj 2104] K-th Number【主席树】
传送门:http://poj.org/problem?id=2104
保存模版。
#include <cstdio>
#include <algorithm>
#include <cstring> // Sora Sai Gou!!! const int maxn = 100005, maxm = 5005, maxv = 2000005, nil = maxv - 1; int n, m, a[maxn], b[maxn], siz[maxv], ch[maxv][2], root[maxv], cnt;
int t1, t2, t3; inline int fndidx(int key) {
int left = 1, right = n, mid;
while (left < right) {
mid = (left + right) >> 1;
if (a[mid] < key) {
left = mid + 1;
}
else {
right = mid;
}
}
return left;
}
void ist(int & ima, int p, int key, int ql, int qr) {
if (!ima) {
ima = ++cnt;
}
siz[ima] = siz[p] + 1;
if (ql == qr) {
return;
}
int mid = (ql + qr) >> 1;
if (key <= mid) {
ch[ima][1] = ch[p][1];
ist(ch[ima][0], ch[p][0], key, ql, mid);
}
else {
ch[ima][0] = ch[p][0];
ist(ch[ima][1], ch[p][1], key, mid + 1, qr);
}
}
inline int qry (int l, int r, int kth) {
--l;
int left = 1, right = n, mid, p1 = root[l], p2 = root[r], tem;
while (left < right) {
tem = siz[ch[p2][0]] - siz[ch[p1][0]];
mid = (left + right) >> 1;
if (kth <= tem) {
p2 = ch[p2][0];
p1 = ch[p1][0];
right = mid;
}
else {
kth -= tem;
p2 = ch[p2][1];
p1 = ch[p1][1];
left = mid + 1;
}
}
return a[left];
} int main(void) {
//freopen("in.txt", "r", stdin);
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) {
scanf("%d", a + i);
}
memcpy(b, a, sizeof b);
std::sort(a + 1, a + n + 1);
for (int i = 1; i <= n; ++i) {
b[i] = fndidx(b[i]);
} for (int i = 1; i <= n; ++i) {
ist(root[i], root[i - 1], b[i], 1, n);
}
for (int i = 0; i < m; ++i) {
scanf("%d%d%d", &t1, &t2, &t3);
printf("%d\n", qry(t1, t2, t3));
}
return 0;
}
[poj 2104] K-th Number【主席树】的更多相关文章
- 【POJ 2104】 K-th Number 主席树模板题
达神主席树讲解传送门:http://blog.csdn.net/dad3zz/article/details/50638026 2016-02-23:真的是模板题诶,主席树模板水过.今天新校网不好,没 ...
- 静态区间第k大(主席树)
POJ 2104为例(主席树入门题) 思想: 可持久化线段树,也叫作函数式线段树,也叫主席树(高大上). 可持久化数据结构(Persistent data structure):利用函数式编程的思想使 ...
- poj 2104 K-th Number 主席树+超级详细解释
poj 2104 K-th Number 主席树+超级详细解释 传送门:K-th Number 题目大意:给出一段数列,让你求[L,R]区间内第几大的数字! 在这里先介绍一下主席树! 如果想了解什么是 ...
- poj2104 k-th number 主席树入门讲解
poj2104 k-th number 主席树入门讲解 定义:主席树是一种可持久化的线段树 又叫函数式线段树 刚开始学是不是觉得很蒙逼啊 其实我也是 主席树说简单了 就是 保留你每一步操作完成之后 ...
- POJ 2104 K-th Number 主席树(区间第k大)
题目链接: http://poj.org/problem?id=2104 K-th Number Time Limit: 20000MSMemory Limit: 65536K 问题描述 You ar ...
- POJ 2104:K-th Number(主席树静态区间k大)
题目大意:对于一个序列,每次询问区间[l,r]的第k大树. 分析: 主席树模板题 program kthtree; type point=record l,r,s:longint; end; var ...
- POJ 2104 K-th Number ( 求取区间 K 大值 || 主席树 || 离线线段树)
题意 : 给出一个含有 N 个数的序列,然后有 M 次问询,每次问询包含 ( L, R, K ) 要求你给出 L 到 R 这个区间的第 K 大是几 分析 : 求取区间 K 大值是个经典的问题,可以使用 ...
- SPOJ MKTHNUM & POJ 2104 - K-th Number - [主席树模板题]
题目链接:http://poj.org/problem?id=2104 Description You are working for Macrohard company in data struct ...
- poj 2104 K-th Number(主席树 视频)
K-th Number 题意: 给你一些数,让你求一个区间内,第k大的数是多少. 题解: 主席树第一题,看的qsc视频写的,戳戳戳 学到了unique函数,他的作用是:把相邻的重复的放到后面,返回值是 ...
- Poj 2104 K-th Number(主席树&&整体二分)
K-th Number Time Limit: 20000MS Memory Limit: 65536K Case Time Limit: 2000MS Description You are wor ...
随机推荐
- eclipse 中PlantUML的安装和使用
安装: 填写的地址:http://hallvard.github.io/plantuml/ 安装完plantUML后,还要下载一个Graphviz https://pan.baidu.com/s/1g ...
- LightOJ1282 Leading and Trailing —— 指数转对数
题目链接:https://vjudge.net/problem/LightOJ-1282 1282 - Leading and Trailing PDF (English) Statistics ...
- hdu1052 田忌赛马 —— 贪心
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1052 错误代码: #include<stdio.h>//田忌赛马,错误版 #include ...
- nginx-upsync-module安装
1.安装nginx-upsync-module wget https://github.com/weibocom/nginx-upsync-module/archive/master.zip 作用:n ...
- H264 各种profile
关键字:H264 ,base profile, main profile, extend profile, high profile. 提到High Profile H.264解码许多人并不了解,那么 ...
- 存储过程系列五:完整的存储过程备份使用函数REPLACE()substr()
CREATE OR REPLACE PROCEDURE "YLQXSCXKESL_GGXKZ_TO_QB" ( ...
- Unable to create Debug Bridge:Unable to start adb server:error:cannot parse version
打开Android Studio时报如下错误提示: Unable to create Debug Bridge:Unable to start adb server:error:cannot pars ...
- cnn汉字识别 tensorflow demo
# -*- coding: utf-8 -*- import tensorflow as tf import os import random import tensorflow.contrib.sl ...
- 「LuoguP2420」 让我们异或吧(树上前缀和
P2420 让我们异或吧-洛谷 题目描述 异或是一种神奇的运算,大部分人把它总结成不进位加法. 在生活中…xor运算也很常见.比如,对于一个问题的回答,是为1,否为0.那么: (A是否是男生 )xor ...
- 选择合适的innodb_log_file_size
如果对 Innodb 数据表有大量的写入操作,那么选择合适的 innodb_log_file_size 值对提升MySQL性能很重要.然而设置太大了,就会增加恢复的时间,因此在MySQL崩溃或者突然断 ...