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 ...
随机推荐
- idea拉出Output窗口和还原窗口
拉出:按住标题可以拖出 效果: 还原:点击restore layout
- Qt的widget与Button添加图片
ui.widget->setAutoFillBackground(true); QImage image; QPalette palette; image.load("D:\\Work ...
- python中的句柄操作
python中的句柄操作 制作人:全心全意 通过窗口标题获取句柄 import win32gui hld = win32gui.FindWindow(None,u"Adobe Acrobat ...
- 83-MACD 移动平均汇总/分离指标.(2015.7.3)
MACD 移动平均汇总/分离指标 ~计算: · EMA(12,t)=EMA(12,t-1) * 11/13 + Close * 2/13 · EMA(26,t)=EMA(26,t-1) * 25/27 ...
- Uva 816 Abbott的复仇(三元组BFS + 路径还原)
题意: 有一个最多9*9个点的迷宫, 给定起点坐标(r0,c0)和终点坐标(rf,cf), 求出最短路径并输出. 分析: 因为多了朝向这个元素, 所以我们bfs的队列元素就是一个三元组(r,c,dir ...
- 慕课笔记利用css进行布局【两列布局】
<html> <head> <title>两列布局</title> <style type="text/css"> bo ...
- CF-697B Barnicle与691C Exponential notation
无聊写两个题解吧,上午做比赛拉的,感触很多! B. Barnicle time limit per test 1 second memory limit per test 256 megabytes ...
- 【转】Java读写文件大全
使用Java操作文本文件的方法详解 最初java是不支持对文本文件的处理的,为了弥补这个缺憾而引入了Reader和Writer两个类,这两个类都是抽象类,Writer中 write(ch ...
- Codeforces700C. Break Up
n<=1000,m<=30000的图,问割掉边权和尽量小的0.1或2条边使S和T不连通,输出割了哪些边,无解-1. 道理是很好懂的,先随便找S到T的一条路径,找不到输出0,找到的话这条路上 ...
- 【IntelliJ】IntelliJ IDEA的安装破解及使用
结合两位大牛CV的,写的很全面,仅供自己使用 转载地:http://www.jianshu.com/p/ad3830095fb3 https://www.cnblogs.com/kangjianwei ...