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. 找不到 libgtk-x11-2.0.so.0

    找不到 libgtk-x11-2.0.so.0 安装 yum groupinstall "Development Tools" yum install gtk+-devel gtk ...

  2. (error) DENIED Redis is running in protected mode because protected mode is enabled

    在通过Java程序链接配置好的redis服务时出现 DENIED Redis is running in protected mode because protected mode is enable ...

  3. Linux下python版本的升级步骤

    1.先下载,你要升级的python版本(我升级的是python3.3.0) 可使用系统自带下载工具wget下载: wget http://www.python.org/ftp/python/3.3.0 ...

  4. AtCoder Regular Contest 103 Problem D Robot Arms (构造)

    题目链接  Problem D 给定$n$个坐标,然后让你构造一个长度为$m$的序列, 然后给每个坐标规定一个长度为$m$的序列,ULRD中的一个,意思是走的方向, 每次从原点出发按照这个序列方向,每 ...

  5. 进入CentOS7紧急模式恢复root密码

    第一步.重启CentOS7,在以下界面选择要编辑的内核(一般第一个),按e进入编辑界面 第二步.在编辑界面找到如下一行,将ro改为rw init=/sysroot/bin/sh.改完后<Ctrl ...

  6. VB查询数据库之登陆窗体——机房收费总结(一)

    机房收费系统已经做了很长一段时间了,虽然到目前为止,仍然没有结束,但已经结节尾声了.我感觉现在有必要回首总结一下整个机房收费系统. 除了结账做了一半,报表接触一点之外,其他的都基本上差不多了.从做过的 ...

  7. (原创)Stanford Machine Learning (by Andrew NG) --- (week 5) Neural Networks Learning

    本栏目内容来自Andrew NG老师的公开课:https://class.coursera.org/ml/class/index 一般而言, 人工神经网络与经典计算方法相比并非优越, 只有当常规方法解 ...

  8. LNMP一键包屏蔽PHP报错信息开启方法

    在 php-fpm.conf中的[www]下添加php_flag[display_errors] = On 重启php-fpm即可 /etc/init.d/php-fpm restart

  9. Pointers and Strings

    The special relationship between arrays and pointers extends to C-style strings.Consider the followi ...

  10. VUE2.0学习总结

    摘要: 年后公司项目开始上vue2.0,自己对学习进行了总结,希望对大家有帮助! VUE2.0学习 vue介绍 vue是什么? https://vuefe.cn/guide vue也是一个数据驱动框架 ...