Kth number

Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 4602 Accepted Submission(s): 1468

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
刚学了划分树。来做这一题。仅仅知道划分树的原理,代码是自己写的,不是通用的写法。 G++AC C++超内存。。。
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std; const int maxn = 100000;
struct tree{
int l , r;
vector<int> element , lft;
}a[3*maxn];
vector<int> seq;
int n , m; bool cmp(int a , int b){ return a<b;} void build(int l , int r , int k){
a[k].l = l;
a[k].r = r;
if(l != r){
int mid = (l+r)/2;
int lson = 2*k , rson = 2*k+1;
a[lson].element.clear();
a[rson].element.clear();
a[lson].element.push_back(0);
a[rson].element.push_back(0);
a[lson].lft.clear();
a[rson].lft.clear();
a[lson].lft.push_back(0);
a[rson].lft.push_back(0);
for(int i = 1; i < a[k].element.size(); i++){
if(a[k].element[i] <= seq[mid]){
a[lson].element.push_back(a[k].element[i]);
a[k].lft.push_back(a[k].lft[i-1]+1);
}else{
a[rson].element.push_back(a[k].element[i]);
a[k].lft.push_back(a[k].lft[i-1]);
}
}
build(l , mid , 2*k);
build(mid+1 , r , 2*k+1);
}
} int query(int l , int r , int k , int kth){
if(a[k].l==a[k].r){
return a[k].element[1];
}else{
int tem = a[k].lft[r]-a[k].lft[l-1];
if(tem >= kth){
return query(1+a[k].lft[l-1] , 1+a[k].lft[l-1]+tem-1 , 2*k , kth);
}else{
return query(1+(l-1-a[k].lft[l-1]) , (l-1-a[k].lft[l-1])+r-l+1-tem , 2*k+1 , kth-tem);
}
}
} void initial(){
a[1].element.clear();
a[1].element.push_back(0);
a[1].lft.clear();
a[1].lft.push_back(0);
seq.clear();
} void readcase(){
scanf("%d%d" , &n , &m);
seq.push_back(0);
for(int i = 0; i < n; i++){
int num;
scanf("%d" , &num);
if(seq[0] >= num) seq[0] = num-1;
seq.push_back(num);
a[1].element.push_back(num);
}
} void computing(){
sort(seq.begin() , seq.end() , cmp);
build(1 , n , 1);
int s , t , k;
while(m--){
scanf("%d%d%d" , &s , &t , &k);
printf("%d\n" , query(s , t , 1 , k));
}
} int main(){
int t;
scanf("%d" , &t);
while(t--){
initial();
readcase();
computing();
}
return 0;
}

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 (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. Beam Search

    Q: 什么是Beam Search? 它在NLP中的什么场景里会⽤到? 传统的广度优先策略能够找到最优的路径,但是在搜索空间非常大的情况下,内存占用是指数级增长,很容易造成内存溢出,因此提出了beam ...

  2. vue 轮播插件使用

    <template> <div> <Swiper ref="swiper" v-if="list.length > 0" : ...

  3. CSU 2018年12月月赛 G(2219): Coin

    Description 有这样一个众所周知的问题: 你面前有7个硬币,其中有一个劣质的(它比正常的硬币轻一点点),你有一个天平,问需要你需要使用天平多少次能保证找到那个劣质的硬币. 众所周知的算法是: ...

  4. mysql批量插值

    将查询结果集插入到表中(适用批量插值) 将结果集插入 不需要添加VALUES INSERT INTO `erp`.`role_menu` (`ROLEUUID`, `MENUUUID`) (SELEC ...

  5. buf.toJSON()

    buf.toJSON() 返回:{Object} 返回该 Buffer 实例的 JSON 表达式.当字符串化一个 Buffer 实例时会隐式调用 JSON.stringify() 这个函数. 例子: ...

  6. ajax中文乱码解决(java)

    方法1: 页面端发出的数据做一次encodeURI,服务器端使用new String(old.getBytes("iso8859-1"), "utf-8") 方 ...

  7. [Android] java代码无错误,但跳转失败

    今天在调代码的时候,出现了这样的问题,我晕了半天,才找到解决办法. 查看日志发现:Initialize Binary Program Cache: Load Failed 从来没见过这种问题,Java ...

  8. day 21 03 补全异常处理

    day 21 03  异常处理(补全) 1.异常处理的整体几个语句: try: .......#有可能出错的代码 ret=int(input('number >>>')) print ...

  9. socket scoketserver

    import socket sk = socket.socket() # 创建了一个socket对象 # sk.setsockopt(socket.SOL_SOCKET,socket.SO_REUSE ...

  10. BNUOJ 5363 Machine Schedule

    Machine Schedule Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Origin ...