hdu 2665 Kth number(划分树)
Kth number
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4602 Accepted Submission(s): 1468
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]
1
10 1
1 4 2 3 5 6 7 8 9 0
1 3 2
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(划分树)的更多相关文章
- hdu 2665 Kth number(划分树模板)
http://acm.hdu.edu.cn/showproblem.php?pid=2665 [ poj 2104 2761 ] 改变一下输入就可以过 http://poj.org/problem? ...
- HDU 2665 Kth number(划分树)
Kth number Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- hdu 2665 Kth number 主席树
Kth number Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Prob ...
- hdu 2665 Kth number_划分树
题意:求区间[a,b]的第k大 因为多次询问要用到划分树 #include <iostream> #include<cstdio> #include<algorithm& ...
- HDU - 2665 Kth number 主席树/可持久化权值线段树
题意 给一个数列,一些询问,问$[l,r]$中第$K$大的元素是哪一个 题解: 写法很多,主席树是最常用的一种之一 除此之外有:划分树,莫队分块,平衡树等 主席树的定义其实挺模糊, 一般认为就是可持久 ...
- hdu 2665 Kth number
划分树 /* HDU 2665 Kth number 划分树 */ #include<stdio.h> #include<iostream> #include<strin ...
- 主席树[可持久化线段树](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 ...
- hdu 2665 Kth number (poj 2104 K-th Number) 划分树
划分树的基本功能是,对一个给定的数组,求区间[l,r]内的第k大(小)数. 划分树的基本思想是分治,每次查询复杂度为O(log(n)),n是数组规模. 具体原理见http://baike.baidu. ...
- POJ 2104&HDU 2665 Kth number(主席树入门+离散化)
K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 50247 Accepted: 17101 Ca ...
随机推荐
- C# 使用MongoDB(学习记录)
1)下载MongoDB https://www.mongodb.com/download-center#community 2)在D盘新建Data->db 3)执行命令 mongod --dbp ...
- 奇异值分解 SVD 的数学解释
奇异值分解(Singular Value Decomposition,SVD)是一种矩阵分解(Matrix Decomposition)的方法.除此之外,矩阵分解还有很多方法,例如特征分解(Eigen ...
- JS如何禁用浏览器的退格键
<script type="text/javascript"> //处理键盘事件 禁止后退键(Backspace)密码或单行.多行文本框除外 function forb ...
- C++ STL容器之 stack
STL 中的 stack 是一种容器适配器,而不是一种容器. 它是容器适配器是指,只要支持一系列方法的容器(empty, size, back, push_back, pop_back),都能作为st ...
- 笔试算法题(18):常数时间删除节点 & 找到仅出现一次的两个数字
出题:给定链表的头指针和一个节点指针,要求在O(1)的时间复杂度下删除该节点 分析: 如果需要删除的节点为A,其前序节点为A-,其后续节点为A+,所以删除A之后,需要使得A-的下一个节点就是A+,常规 ...
- Python re模块 subprocess模块
re模块 内部实现不是Python 而是调用了c的库 re是什么 正则 表达 式子 就是一些带有特殊含义的符号或者符号的组合作用: 对字符串进行过滤 在一对字符串中找到所关心的内容 你就需要告诉计算机 ...
- 自动清除日期目录shell脚本
很多时候备份通常会使用到基于日期来创建文件夹,对于这些日期文件夹下面又有很多子文件夹,对于这些日期文件整个移除,通过find结合rm或者delete显得有些力不从心.本文提供一个简单的小脚本,可以嵌入 ...
- C#上位机开发(一)—— 了解上位机
在单片机项目开发中,上位机也是一个很重要的部分,主要用于数据显示(波形.温度等).用户控制(LED,继电器等),下位机(单片机)与 上位机之间要进行数据通信的两种方式都是基于串口的: USB转串口 — ...
- private关键字
Student.java /* * 学生类 * * 通过对象直接访问成员变量,会存在数据安全问题 * 这个时候,我们就想能不能不让外界对象直接访问成员变量呢? * 答案:能 * 如何实现呢? * pr ...
- Floyd算法实现总结
问题描述 给出图,求任意两点的最短距离 算法思路 定义n+1个矩阵矩阵A,和记录路径的矩阵path 依次求A0~An的值,最后的An即为最短路径矩阵 // int A[8][7][7],path[7] ...