hdu 2665 Kth number (poj 2104 K-th Number) 划分树
划分树的基本功能是,对一个给定的数组,求区间[l,r]内的第k大(小)数。
划分树的基本思想是分治,每次查询复杂度为O(log(n)),n是数组规模。
具体原理见http://baike.baidu.com/link?url=vIUKtsKYx7byeS2KCOHUI14bt_0sdHAa9BA1VceHdGsTv5jVq36SfZgBKdaHYUGqIGvIGrE_aJtqy0D0b1fCoq
个人感觉看代码是最好的学习方法。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 100100 int a[N];
int s[][N];//划分树
int num[][N];//num[i][j] - num[i][j-1] == 1表示第i层第j个数在下层中要被划入左子树 void build(int l, int r, int dep = )
{
if(l == r){
s[dep][l] = s[dep-][l];
return;
}
int mid = l+r>>;
int cnt = mid-l+;
for(int i = l; i <= r; i++) if(s[dep-][i] < a[mid]) cnt--;
int c1 = l, c2 = mid+;
for(int i = l; i <= r; i++)
{
if(s[dep-][i] < a[mid] || ( s[dep-][i] == a[mid] && cnt-- > )) s[dep][c1++] = s[dep-][i];
else s[dep][c2++] = s[dep-][i]; num[dep-][i] = num[dep-][l-]+c1-l;
}
build(l, mid, dep+);
build(mid+, r, dep+);
} int query(int l, int r, int k, int L, int R, int dep = )
{
if(l == r) return s[dep][l];
int mid = L+R>>;
int cnt = num[dep][r] - num[dep][l-]; if(cnt >= k)
{
int nl = L+num[dep][l-]-num[dep][L-];
int nr = nl+cnt-;
return query(nl, nr, k, L, mid, dep+);
}
else
{
int nr = r+num[dep][R]-num[dep][r];
int nl = nr - (r-l-cnt);
return query(nl, nr, k-cnt, mid+, R, dep+);
}
} int main()
{
int T, n, m;
scanf("%d", &T);
while(T--)
//while(~scanf("%d %d", &n, &m))
{
memset(num, , sizeof(num));
scanf("%d %d", &n, &m);
for(int i = ; i <= n; i++) scanf("%d", &a[i]);
for(int i = ; i <= n; i++) s[][i] = a[i];
sort(a+, a+n+);
build(, n);
for(int i = ; i < m; i++)
{
int l, r, k;
scanf("%d %d %d", &l, &r, &k);
printf("%d\n", query(l, r, k, , n));
} }
return ;
}
hdu 2665 Kth number (poj 2104 K-th Number) 划分树的更多相关文章
- HDU 2665(Kth number-区间第k大[内存限制+重数])
Kth number Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- hdu 2665 Kth number
划分树 /* HDU 2665 Kth number 划分树 */ #include<stdio.h> #include<iostream> #include<strin ...
- 主席树[可持久化线段树](hdu 2665 Kth number、SP 10628 Count on a tree、ZOJ 2112 Dynamic Rankings、codeforces 813E Army Creation、codeforces960F:Pathwalks )
在今天三黑(恶意评分刷上去的那种)两紫的智推中,突然出现了P3834 [模板]可持久化线段树 1(主席树)就突然有了不详的预感2333 果然...然后我gg了!被大佬虐了! hdu 2665 Kth ...
- K-th Number POJ - 2104
K-th Number POJ - 2104 You are working for Macrohard company in data structures department. After fa ...
- K-th Number Poj - 2104 主席树
K-th Number Poj - 2104 主席树 题意 给你n数字,然后有m次询问,询问一段区间内的第k小的数. 解题思路 这个题是限时训练做的题,我不会,看到这个题我开始是拒绝的,虽然题意清晰简 ...
- 主席树 【权值线段树】 && 例题K-th Number POJ - 2104
一.主席树与权值线段树区别 主席树是由许多权值线段树构成,单独的权值线段树只能解决寻找整个区间第k大/小值问题(什么叫整个区间,比如你对区间[1,8]建立一颗对应权值线段树,那么你不能询问区间[2,5 ...
- 静态区间第k大(划分树)
POJ 2104为例[经典划分树问题] 思想: 利用快速排序思想, 建树时将区间内的值与区间中值相比,小于则放入左子树,大于则放入右子树,如果相等则放入左子树直到放满区间一半. 查询时,在建树过程中利 ...
- [NBUT 1458 Teemo]区间第k大问题,划分树
裸的区间第k大问题,划分树搞起. #pragma comment(linker, "/STACK:10240000") #include <map> #include ...
- POJ 2104&HDU 2665 Kth number(主席树入门+离散化)
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 50247 Accepted: 17101 Ca ...
随机推荐
- 《数据结构与算法(C语言版)》严蔚敏 | 第五章 建立二叉树,并完成三/四种遍历算法
PS:所有的代码示例使用的都是这个图 2019-10-29 利用p126的算法5.3建立二叉树,并完成三种遍历算法 中序 后序 先序 #include<iostream> #include ...
- spash和selenium浅析
Splash是什么: Splash是一个Javascript渲染服务.它是一个实现了HTTP API的轻量级浏览器,Splash是用Python实现的,同时使用Twisted和QT.Twisted(Q ...
- (转)Python3 zip() 函数
转:http://www.runoob.com/python3/python3-func-zip.html 描述 zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返 ...
- IDEA创建springboot异常(Failed to load class "org.slf4j.impl.StaticLoggerBinder")
IDEA中创建springboot项目遇到的问题 SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". ...
- SQL SERVER 2012文件表(FILETABLE)新体验之一
SQLSERVER 2012 文件表功能很COOL,让我们体验一下吧. 1,创建数据库 [sql] DREATE DATABASE FileTableTest ON PRIMARY ( NA ...
- 移动端rem布局屏幕适配插件(放js中便可使用)
/* doc:不用管:document对象 win:不用管:window对象 design:注意:设计稿的尺寸/物理像素*/ (function (doc, win,design) {// alert ...
- git subtree模块化代码管理
Git Subtree 的原理 首先,你有两个伟大的项目——我们叫他P1项目.P2项目,还有一个牛逼的要被多个项目共用的项目——我们叫他S项目.我们通过简要讲解使用Subtree来同步代码的过程来解释 ...
- R 保存包含中文的 eps 图片--showtext
来自统计之都,感谢 Ihavenothing(http://cos.name/cn/profile/81532) 详情参考:http://cos.name/cn/topic/151358?replie ...
- Keras 层layers总结
https://blog.csdn.net/u010159842/article/details/78983841
- redis 集群新增节点,slots槽分配,删除节点, [ERR] Calling MIGRATE ERR Syntax error, try CLIENT (LIST | KILL | GET...
redis reshard 重新分槽(slots) https://github.com/antirez/redis/issues/5029 redis 官方已确认该bug redis 集群重新(re ...