HDU-2665-Kth number(划分树)
For each test case, the first line contain two integer n and m (n, m <= 100000), indicates the number of integers in the sequence and the number of the quaere.
The second line contains n integers, describe the sequence.
Each of following m lines contains three integers s, t, k.
[s, t] indicates the interval and k indicates the kth big number in interval [s, t]
1
10 1
1 4 2 3 5 6 7 8 9 0
1 3 2
2
思路:划分树板子题。
#include <cstdio>
#include <algorithm>
using namespace std; int n,num[100005],sorted[100005],node[20][100005],sum[20][100005]; void build(int c,int s,int e)//第c层,s到e
{
int mid,lm,lp,rp,i; mid=(s+e)>>1;
lm=0;
lp=s;//左子树的头指针
rp=mid+1;//右子树的头指针 for(i=s;i<=mid;i++) if(sorted[i]==sorted[mid]) lm++;//求出s到e有多少个数等于sorted[mid] for(i=s;i<=e;i++)
{
if(i==s) sum[c][i]=0;//计算出s到i之间有多少个被分到了左子树
else sum[c][i]=sum[c][i-1]; if(node[c][i]==sorted[mid])
{
if(lm)
{
lm--;
sum[c][i]++;
node[c+1][lp++]=node[c][i];
}
else node[c+1][rp++]=node[c][i];
}
else if(node[c][i]<sorted[mid])
{
sum[c][i]++;
node[c+1][lp++]=node[c][i];
}
else node[c+1][rp++]=node[c][i];
} if(s!=e)
{
build(c+1,s,mid);
build(c+1,mid+1,e);
}
} int query(int c,int s,int e,int l,int r,int k)
{
if(s==e) return node[c][s];
else
{
int ls,rs,mid; mid=(s+e)>>1; if(s==l)//特判
{
ls=0;
rs=sum[c][r];
}
else
{
ls=sum[c][l-1];
rs=sum[c][r]-ls;
} if(rs>=k) return query(c+1,s,mid,s+ls,s+ls+rs-1,k);//要查询的数在左子树
else return query(c+1,mid+1,e,mid-s+1+l-ls,mid-s+1+r-ls-rs,k-rs);//要查询的数在右子树
}
} int main()
{
int T,m,i,a,b,k; scanf("%d",&T); while(T--)
{
scanf("%d%d",&n,&m); for(i=1;i<=n;i++)
{
scanf("%d",&num[i]); node[0][i]=sorted[i]=num[i];
} sort(sorted+1,sorted+n+1); build(0,1,n); while(m--)
{
scanf("%d%d%d",&a,&b,&k); printf("%d\n",query(0,1,n,a,b,k));
}
}
}
HDU-2665-Kth number(划分树)的更多相关文章
- hdu 2665 Kth number(划分树模板)
http://acm.hdu.edu.cn/showproblem.php?pid=2665 [ poj 2104 2761 ] 改变一下输入就可以过 http://poj.org/problem? ...
- HDU 2665 Kth number(划分树)
Kth number Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- hdu 2665 Kth number 主席树
Kth number Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Prob ...
- hdu 2665 Kth number_划分树
题意:求区间[a,b]的第k大 因为多次询问要用到划分树 #include <iostream> #include<cstdio> #include<algorithm& ...
- HDU - 2665 Kth number 主席树/可持久化权值线段树
题意 给一个数列,一些询问,问$[l,r]$中第$K$大的元素是哪一个 题解: 写法很多,主席树是最常用的一种之一 除此之外有:划分树,莫队分块,平衡树等 主席树的定义其实挺模糊, 一般认为就是可持久 ...
- 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 ...
- hdu 2665 Kth number(划分树)
Kth number Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- hdu 2665 Kth number (poj 2104 K-th Number) 划分树
划分树的基本功能是,对一个给定的数组,求区间[l,r]内的第k大(小)数. 划分树的基本思想是分治,每次查询复杂度为O(log(n)),n是数组规模. 具体原理见http://baike.baidu. ...
- POJ 2104&HDU 2665 Kth number(主席树入门+离散化)
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 50247 Accepted: 17101 Ca ...
随机推荐
- YII2 源码阅读 综述
如何阅读源码呢? 我的方法是,打开xdebug的auto_trace [XDebug] ;xdebug.profiler_append = 0 ;xdebug.profiler_enable = 1 ...
- gdbserver静态编译
redhat9 编译gdb server(静态编译)下载gdb-6.2a.tar:http://download.chinaunix.net/download.php?id=6680&Reso ...
- Git常用命令速查手册
Git组成 1.初始化仓库 git init 2.将文件添加到仓库 git add 文件名 # 将工作区的某个文件添加到暂存区 git add -u # 添加所有被tracked文件中被修改或删除的文 ...
- ExtJs之列表(grid)
--renderers渲染器 可以格式化该列显示的数据格式或者按照你自定义的脚本显示最终数据样子 先看下renderer: function()里的参数 renderer:function(value ...
- Sqli-labs less 11
Less-11 从这一关开始我们开始进入到post注入的世界了,什么是post呢?就是数据从客户端提交到服务器端,例如我们在登录过程中,输入用户名和密码,用户名和密码以表单的形式提交,提交到服务器后服 ...
- POJ ???? Monkey King
题目描述 Once in a forest, there lived N aggressive monkeys. At the beginning, they each does things i ...
- BZOJ 3343:教主的魔法(分块)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3343 [题目大意] 给出一个数列,有区间加法操作,询问区间大于等于c的数字个数 [题解 ...
- Generator函数(二)
for...of循环 1.for...of循环可以自动遍历Generator函数,不需要再调用next方法 function* helloWorldGenerator(){ yield 'hello' ...
- JavaScript 匹配字符串偶数位置的字符 及匹配 $ 符号
已知一个字符串#####,现需要替换偶数位置的#为&. function replaceDemo(){ var s = "1#2#3#4#5#"; var regex = ...
- Android Activtity Security(转)
Android四大组件之一--Activity安全详解. 原帖地址:http://drops.wooyun.org/tips/3936 0x00 科普 Android每一个Application都是由 ...