hdu 2665 Kth number(划分树模板)
http://acm.hdu.edu.cn/showproblem.php?pid=2665
[ poj 2104 2761 ] 改变一下输入就可以过
http://poj.org/problem?id=2104
http://poj.org/problem?id=2761
Kth number
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3266 Accepted Submission(s): 1090
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]
划分树是一种基于线段树的数据结构。主要用于快速求出(在log(n)的时间复杂度内)序列区间的第k大值 。
划分树和归并树都是用线段树作为辅助的,原理是基于快排 和归并排序 的。
划分树的建树过程基本就是模拟快排过程,取一个已经排过序的区间中值,然后把小于中值的点放左边,大于的放右边。并且记录d层第i个数之前(包括i)小于中值的放在左边的数。具体看下面代码注释。

查找其实是关键,因为再因查找[l,r]需要到某一点的左右孩子时需要把[l,r]更新。具体分如下几种情况讨论: 假设要在区间[l,r]中查找第k大元素,t为当前节点,lch,rch为左右孩子,left,mid为节点t左边界和中间点。
1、sum[r]-sum[l-1]>=k,查找lch[t],区间对应为[ left+sum[l-1] , left+sum[r]-1 ]
2、sum[r]-sum[l-1]<k,查找rch[t],区间对应为[ mid+1+l-left-sum[l-1] , mid+1+r-left-sum[r] ]
上面两个关系在纸上可以推出来,对着上图更容易理解关系式;
讲解转自:http://www.cnblogs.com/pony1993/archive/2012/07/17/2594544.html
AC代码:
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h> using namespace std; #define N 100010 int sorted[N]; //排序完的数组
int toleft[][N]; //toleft[i][j]表示第i层从1到k有多少个数分入左边
int tree[][N]; //表示每层每个位置的值 void buildingTree(int l,int r,int dep)
{
if(l==r) return;
int mid = (l+r)>>;
int i,sum = mid-l+; //表示等于中间值而且被分入左边的个数
for(i=l;i<=r;i++)
{
if(tree[dep][i]<sorted[mid]) sum--;
}
int lpos=l;
int rpos=mid+;
for(i=l;i<=r;i++)
{
if(tree[dep][i]<sorted[mid]) //比中间的数小,分入左边
{
tree[dep+][lpos++]=tree[dep][i];
}
else if(tree[dep][i]==sorted[mid]&&sum>) //等于中间的数值,分入左边,直到sum==0后分到右边
{
tree[dep+][lpos++]=tree[dep][i];
sum--;
}
else //右边
{
tree[dep+][rpos++]=tree[dep][i];
}
toleft[dep][i] = toleft[dep][l-] + lpos - l; //从1到i放左边的个数
}
buildingTree(l,mid,dep+);
buildingTree(mid+,r,dep+);
} //查询区间第k大的数,[L,R]是大区间,[l,r]是要查询的小区间
int queryTree(int L,int R,int l,int r,int dep,int k)
{
if(l==r) return tree[dep][l];
int mid = (L+R)>>;
int cnt = toleft[dep][r] - toleft[dep][l-]; //[l,r]中位于左边的个数
if(cnt>=k)
{
int newl = L + toleft[dep][l-] - toleft[dep][L-]; //L+要查询的区间前被放在左边的个数
int newr = newl + cnt - ; //左端点加上查询区间会被放在左边的个数
return queryTree(L,mid,newl,newr,dep+,k);
}
else
{
int newr = r + toleft[dep][R] - toleft[dep][r];
int newl = newr - (r - l - cnt);
return queryTree(mid+,R,newl,newr,dep+,k-cnt);
}
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m,i;
scanf("%d%d",&n,&m);
for(i=;i<=n;i++)
{
scanf("%d",&tree[][i]);
sorted[i] = tree[][i];
}
sort(sorted+,sorted++n);
buildingTree(,n,);
while(m--)
{
int s,t,k;
scanf("%d%d%d",&s,&t,&k);
printf("%d\n",queryTree(,n,s,t,,k));
}
}
return ;
}
hdu 2665 Kth number(划分树模板)的更多相关文章
- 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$大的元素是哪一个 题解: 写法很多,主席树是最常用的一种之一 除此之外有:划分树,莫队分块,平衡树等 主席树的定义其实挺模糊, 一般认为就是可持久 ...
- POJ2104 K-th Number 划分树 模板题啊
/*Source Code Problem: 2104 User: 96655 Memory: 14808K Time: 1282MS Language: G++ Result: Accepted S ...
- 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 ...
- [hdu2665]Kth number(划分树求区间第k大)
解题关键:划分树模板题. #include<cstdio> #include<cstring> #include<algorithm> #include<cs ...
- hdu 2665 Kth number(划分树)
Kth number Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
随机推荐
- swift switch语句
switch选择 1)case多条件匹配:条件之间用逗号隔开 用三个点表示范围:…,..<:表示不包含上边界 var tand = 1 switch tand{ case 0: ...
- 2014年6月5日 深圳 IBM 安全解决方案会议通知
2014年6月5日 深圳 IBM 安全解决方案会议通知 http://gdtesting.com/news.php?id=191 时间: 2014年6月5日 地点: 深圳大中华喜来登 议程: IBM安 ...
- oracle 不转义 &
在为表加注释,遇到有些注释包含'&' 但又不想写eacape, 在sqlplus下,set define off 即可将&输入到数据库中.
- C#总结2
第三章 对于继承:继承是根据父类创建新子类的过程: 对于继承:例: Public class Father { public void say() { //dosomething } } Pu ...
- fiddlercore 抓包获取cookie的方法
public partial class form1 : Form { public form1() { string cookies = ""; InitializeCompon ...
- MIME 部分扩展名与类型对应
svg image/svg+xml woff application/x-font-woff woff2 application/x-font-woff eot applicati ...
- ajax_jsonp —— 跨域
JSONP:原理是script标签 一.抓包 二.不用每次都连接 localhost 的方法 三.抓包后所需的参数 su?:后面跟的是传递过去的参数. cb:是 callback 后面跟的是对返回 ...
- 第四篇、C_快速、冒泡、选择、插入排序、二分查找排序、归并、堆排序
1.快速排序 实现: 1.取中间一个数作为支点 2.分别在支点的左右两边进行查找,如果左边查找到比支点大,右边查找到比支点小,就交换位置,如此循环,比支点小的数就排在了左边,比支点大的就排在右边 3. ...
- C++ IO 详细用法
http://www.cnblogs.com/keam37/ keam所有 转载请注明出处 本文将分别从<iostream>,<sstream>,<fstream> ...
- libipq —— iptables用户空间数据包排队库
LIBIPQ(3) NAMElibipq —— iptables用户空间数据包排队库 SYNOPSIS #include <linux/netfilter.h>#include <l ...