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. CAD参数绘制批注(com接口)

    C#中实现代码说明: private void DrawComment() { MxDrawComment com = new MxDrawComment(); MxDrawPoint pt = ne ...

  2. 认识单文件组件.vue 文件

    vuejs 自定义了一种.vue文件,可以把html, css, js 写到一个文件中,从而实现了对一个组件的封装, 一个.vue 文件就是一个单独的组件.由于.vue文件是自定义的,浏览器不认识,所 ...

  3. oracle数据库使用hint来让模糊查询走索引

    在没有创建数据直方图之前,查询优化器是cbo,可能不会选择代价最低(效率最高)的方式查询. 先创建表 --日语假名表 CREATE TABLE JAPANESE_SOUNDMARK ( ID INTE ...

  4. 笔试算法题(45):简介 - AC自动机(Aho-Corasick Automation)

    议题:AC自动机(Aho-Corasick Automation) 分析: 此算法在1975年产生于贝尔实验室,是著名的多模式匹配算法之一:一个常见的例子就是给定N个单词,给定包含M个字符的文章,要求 ...

  5. python3.x Day5 socket编程

    socket编程: socket 是大多应用层的底层的封装,实际封装的就是 发送,接收,但其实很复杂,在传输层协议之上(TCP/IP,UDP) 既然是网络通讯,一般按照服务端,客户端来处理:服务端: ...

  6. 第十一节:pandas统计函数

    1.pct_change()计算增长比例 2.cov()协方差 3.corr()相关系数 4.rank()数据排名 5.numpy聚合函数

  7. python面向对象编程设计与开发

    一.什么是面向对象的程序设计 1.何为数据结构? 数据结构是指相互之间存在一种或多种特定关系的数据元素的集合,如列表.字典. 2.何为编程? 编程是指程序员用特定的语法+数据结构+算法,组成的代码,告 ...

  8. bootloader的移植

    jz2440开发板 在介绍bootloader里边的内容的时候,需要知道的是: bootloader的引入的目的就是启动linux内核,一个简单的bootloader编写需要以下的步骤: ①初始化硬件 ...

  9. 九度oj 题目1516:调整数组顺序使奇数位于偶数前面

    题目1516:调整数组顺序使奇数位于偶数前面 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:3416 解决:1091 题目描述: 输入一个整数数组,实现一个函数来调整该数组中数字的顺序, ...

  10. [BZOJ2462] [BeiJing2011]矩阵模板(二维Hash)

    传送门 二维哈希即可. 注意质数选的大一些,不然会超时. 还有插入的时候不判重居然比判重要快.. ——代码 #include <cstdio> int main() { ; ") ...