Kth number

Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 37 Accepted Submission(s): 25
 
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
HDU男生专场公开赛——赶在女生之前先过节(From WHU)
 
Recommend
zty
 
/*----------------------------------------------
File:
Date: 2017/6/9 21:40:22
Author: LyuCheng
----------------------------------------------*/ #include <bits/stdc++.h>
#define MAXN 100005 using namespace std; int t,n;
int q,l,r,k;
/************************************划分树模板************************************/
int a[MAXN]; //原数组
int sorted[MAXN]; //排序好的数组
//是一棵树,但把同一层的放在一个数组里。
int num[][MAXN]; //num[i] 表示i前面有多少个点进入左孩子
int val[][MAXN]; //20层,每一层元素排放,0层就是原数组
void build(int l,int r,int ceng)
{
if(l==r) return ;
int mid=(l+r)/,isame=mid-l+; //isame保存有多少和sorted[mid]一样大的数进入左孩子
for(int i=l;i<=r;i++) if(val[ceng][i]<sorted[mid]) isame--;
int ln=l,rn=mid+; //本结点两个孩子结点的开头,ln左
for(int i=l;i<=r;i++)
{
if(i==l) num[ceng][i]=;
else num[ceng][i]=num[ceng][i-];
if(val[ceng][i]<sorted[mid] || val[ceng][i]==sorted[mid]&&isame>)
{
val[ceng+][ln++]=val[ceng][i];
num[ceng][i]++;
if(val[ceng][i]==sorted[mid]) isame--;
}
else
{
val[ceng+][rn++]=val[ceng][i];
}
}
build(l,mid,ceng+);
build(mid+,r,ceng+);
} int look(int ceng,int sl,int sr,int l,int r,int k)
{
if(sl==sr) return val[ceng][sl];
int ly; //ly 表示l 前面有多少元素进入左孩子
if(l==sl) ly=; //和左端点重合时
else ly=num[ceng][l-];
int tolef=num[ceng][r]-ly; //这一层l到r之间进入左子树的有tolef个
if(tolef>=k)
{
return look(ceng+,sl,(sl+sr)/,sl+ly,sl+num[ceng][r]-,k);
}
else
{
// l-sl 表示l前面有多少数,再减ly 表示这些数中去右子树的有多少个
int lr = (sl+sr)/ + + (l-sl-ly); //l-r 去右边的开头位置
// r-l+1 表示l到r有多少数,减去去左边的,剩下是去右边的,去右边1个,下标就是lr,所以减1
return look(ceng+,(sl+sr)/+,sr,lr,lr+r-l+-tolef-,k-tolef);
}
}
/************************************划分树模板************************************/ int main(int argc, char *argv[]){ // freopen("in.txt","r",stdin);
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&q);
for(int i=;i<=n;i++){
scanf("%d",&val[][i]);
sorted[i]=val[][i];
}
sort(sorted+,sorted+n+);
build(,n,);
while(q--){
scanf("%d%d%d",&l,&r,&k);
printf("%d\n",look(,,n,l,r,k));
}
}
return ;
}

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) Prob ...

  3. hdu 2665 Kth number_划分树

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

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

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

  5. hdu 2665 Kth number

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

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

  7. hdu 2665 Kth number(划分树)

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

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

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

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

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

随机推荐

  1. GCD之after

    先介绍下C中的modf函数 函数名:modf 头文件:<math.h> 函数原型:double modf(double x, double *ipart) 函数用途:分解x,以得到x的整数 ...

  2. oracle 表查询(二)

    1.使用逻辑操作符号问题:查询工资高于500或者是岗位为manager的雇员,同时还要满足他们的姓名首字母为大写的J?select * from emp where (sal > 500 or ...

  3. 详解 HTTPS 移动端对称加密套件优

    近几年,Google.Baidu.Facebook 等互联网巨头大力推行 HTTPS,国内外的大型互联网公司很多也都已启用全站 HTTPS. Google 也推出了针对移动端优化的新型加密套件 Cha ...

  4. "HK"日常之用C# Process写一只小病毒

    众所周知,Process可以启动和关闭电脑上的进程,命名空间位于 System.Diagnostics,本次的教程就是利用其中的GetProcesses和Kill方法来实现的. 一.编码工作 首先我们 ...

  5. python之HTMLParser解析HTML文档

    HTMLParser是Python自带的模块,使用简单,能够很容易的实现HTML文件的分析.本文主要简单讲一下HTMLParser的用法. 使用时需要定义一个从类HTMLParser继承的类,重定义函 ...

  6. ZOJ 2002 Copying Books 二分 贪心

    传送门:Zoj2002 题目大意:从左到右把一排数字k分,得到最小化最大份,如果有多组解,左边的尽量小. 思路:贪心+二分(参考青蛙过河). 方向:从右向左. 注意:有可能最小化时不够k分.如     ...

  7. Java面向对象 线程技术 -- 下篇

     Java面向对象 线程技术 -- 下篇 知识概要:              (1)线程间的通信           生产者 - 消费者 (2)生产者消费者案例优化 (3)守护线程 (4)停止线 ...

  8. wpf 中英文版编写

    var uriC = new Uri("/YTManage.Language;component/Chinese.xaml", UriKind.Relative); // 得到资源 ...

  9. Babylon.GUI官方文档翻译

    Babylon.GUI是一个基于Babylon.js的WebGL库,可以用来在WebGL3D场景中生成交互性UI与动态纹理.相比于html ui,Babylon.GUI的功能较为简化,但使用起来也更加 ...

  10. 浅谈python 复制(深拷贝,浅拷贝)

    博客参考:点击这里 python中对象的复制以及浅拷贝,深拷贝是存在差异的,这儿我们主要以可变变量来演示,不可变变量则不存在赋值/拷贝上的问题(下文会有解释),具体差异如下文所示 1.赋值: a=[1 ...