Problem Description
Give you a sequence and ask you the kth big number of a inteval.
 
Input
The first line is the number of the test cases.

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]
 
Output
For each test case, output m lines. Each line contains the kth big number.
 
Sample Input
1
10 1
1 4 2 3 5 6 7 8 9 0
1 3 2
 
Sample Output
2
 
Source

思路:划分树板子题。

#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(划分树)的更多相关文章

  1. hdu 2665 Kth number(划分树模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=2665 [ poj 2104 2761 ]  改变一下输入就可以过 http://poj.org/problem? ...

  2. HDU 2665 Kth number(划分树)

    Kth number Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...

  3. hdu 2665 Kth number 主席树

    Kth number Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Prob ...

  4. hdu 2665 Kth number_划分树

    题意:求区间[a,b]的第k大 因为多次询问要用到划分树 #include <iostream> #include<cstdio> #include<algorithm& ...

  5. HDU - 2665 Kth number 主席树/可持久化权值线段树

    题意 给一个数列,一些询问,问$[l,r]$中第$K$大的元素是哪一个 题解: 写法很多,主席树是最常用的一种之一 除此之外有:划分树,莫队分块,平衡树等 主席树的定义其实挺模糊, 一般认为就是可持久 ...

  6. hdu 2665 Kth number

    划分树 /* HDU 2665 Kth number 划分树 */ #include<stdio.h> #include<iostream> #include<strin ...

  7. 主席树[可持久化线段树](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 ...

  8. hdu 2665 Kth number(划分树)

    Kth number Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...

  9. hdu 2665 Kth number (poj 2104 K-th Number) 划分树

    划分树的基本功能是,对一个给定的数组,求区间[l,r]内的第k大(小)数. 划分树的基本思想是分治,每次查询复杂度为O(log(n)),n是数组规模. 具体原理见http://baike.baidu. ...

  10. POJ 2104&HDU 2665 Kth number(主席树入门+离散化)

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 50247   Accepted: 17101 Ca ...

随机推荐

  1. MySQL常用的几个数据类型详解

    一.字符串类型 类型 范围 说明   Char(N) [ binary] N=1~255 个字节binary :分辨大小写 固定长度 std_name cahr(32) not null VarCha ...

  2. SpringMVC调用过程

    SpringMVC中的四大组件: 1.前端控制器(DispatcherServlet)      =>[无需程序员开发] 主要是负责request和response对象的转发和响应. 2.处理器 ...

  3. 转:fortios 5.4后门植入

    提示: 1.经过实验,fortios 5.4 beta4也是可以的. 2.在实验时,选择先下载fortios 5.2(做了快照),再升级5.4,则虚拟机挂载需要选择FortiGate-VM-disk1 ...

  4. 【读书笔记】周志华《机器学习》第三版课后习题讨<第一章-绪论>

    虽然是绪论..但是...真的有点难!不管怎么说,一点点前进吧... 声明一下答案不一定正确,仅供参考,为本人的作答,希望大神们能多多指教~ 1.1 表1.1中若只包含编号为1和4的两个样例,试给出相应 ...

  5. Nginx日志统一格式

    统一格式如下:nginx.conf 纯文本: log_format main '$remote_addr - $remote_user [$time_local] "$request&quo ...

  6. Hydra 8.4/8.5新增功能

    Hydra 8.4/8.5新增功能   Kali Linux 2017.1自带的Hydra为8.3,现在Hydra升级到8.5,新增以下功能.   (1)为输出文件选项-o,添加一个配套选项-b,允许 ...

  7. noip 1999 回文数

    题目描述 若一个数(首位不为零)从左向右读与从右向左读都一样,我们就将其称之为回文数. 例如:给定一个10进制数56,将56加65(即把56从右向左读),得到121是一个回文数. 又如:对于10进制数 ...

  8. 【数论】nefu118 n!后面有多少个0

    就是求n!有多少个因子2和因子5,并在这两者中取较小者.因为必须要一个2和一个5才能拼出1个10. 显然2的数量多于5,因此只需要求n!有多少个因子5即可. n!中素因子p的个数= [n/p]+[n/ ...

  9. bzoj 4430: [Nwerc2015]Guessing Camels赌骆驼

    4430: [Nwerc2015]Guessing Camels赌骆 Description Jaap, Jan, and Thijs are on a trip to the desert afte ...

  10. 20172333 2017-2018-2 《Java程序设计》第4周学习总结

    20172333 2017-2018-2 <Java程序设计>第4周学习总结 教材学习内容 1.类结构的定义与概念 2.利用实例数据建立对象状态的概念 3.描述可见性修饰符作用在方法和数据 ...