poj2104 K-th Number(划分树)
| Time Limit: 20000MS | Memory Limit: 65536K | |
| Total Submissions: 66068 | Accepted: 23273 | |
| Case Time Limit: 2000MS | ||
Description
That is, given an array a[1...n] of different integer numbers, your
program must answer a series of questions Q(i, j, k) in the form: "What
would be the k-th number in a[i...j] segment, if this segment was
sorted?"
For example, consider the array a = (1, 5, 2, 6, 3, 7, 4). Let the
question be Q(2, 5, 3). The segment a[2...5] is (5, 2, 6, 3). If we sort
this segment, we get (2, 3, 5, 6), the third number is 5, and therefore
the answer to the question is 5.
Input
first line of the input file contains n --- the size of the array, and m
--- the number of questions to answer (1 <= n <= 100 000, 1 <=
m <= 5 000).
The second line contains n different integer numbers not exceeding 109 by their absolute values --- the array for which the answers should be given.
The following m lines contain question descriptions, each
description consists of three numbers: i, j, and k (1 <= i <= j
<= n, 1 <= k <= j - i + 1) and represents the question Q(i, j,
k).
Output
Sample Input
7 3
1 5 2 6 3 7 4
2 5 3
4 4 1
1 7 3
Sample Output
5
6
3
Hint
//划分树
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
#define maxn 100006
int tree[][maxn];
int sorted[maxn];
int toleft[][maxn];
int n,m;
void build(int l,int r,int dep){
if(l==r) return ;
int mid=l+r>>;
int ans=mid-l+;
for(int i=l;i<=r;i++)
if(tree[dep][i]<sorted[mid]) ans--;
int lpos=l,rpos=mid+;
for(int i=l;i<=r;i++){
if(tree[dep][i]<sorted[mid]) tree[dep+][lpos++]=tree[dep][i];
else if(tree[dep][i]==sorted[mid] && ans) tree[dep+][lpos++]=tree[dep][i],ans--;
else tree[dep+][rpos++]=tree[dep][i];
toleft[dep][i]=toleft[dep][l-]+lpos-l;
}
build(l,mid,dep+);
build(mid+,r,dep+);
}
int query(int L,int R,int l,int r,int dep,int k){
if(l==r) return tree[dep][l];
int mid=L+R>>;
int ans=toleft[dep][r]-toleft[dep][l-];
if(ans>=k){
int newl=L+toleft[dep][l-]-toleft[dep][L-];
int newr=newl+ans-;
return query(L,mid,newl,newr,dep+,k);
}else{
int newr=r+toleft[dep][R]-toleft[dep][r];
int newl=newr-(r-l-ans);
return query(mid+,R,newl,newr,dep+,k-ans);
}
}
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
scanf("%d",&tree[][i]);
sorted[i]=tree[][i];
}
sort(sorted+,sorted++n);
build(,n,);
while(m--){
int x,y,k;
scanf("%d%d%d",&x,&y,&k);
printf("%d\n",query(,n,x,y,,k));
}
return ;
}
poj2104 K-th Number(划分树)的更多相关文章
- poj 2104 K-th Number 划分树,主席树讲解
K-th Number Input The first line of the input file contains n --- the size of the array, and m --- t ...
- 静态区间第k大(划分树)
POJ 2104为例[经典划分树问题] 思想: 利用快速排序思想, 建树时将区间内的值与区间中值相比,小于则放入左子树,大于则放入右子树,如果相等则放入左子树直到放满区间一半. 查询时,在建树过程中利 ...
- [NBUT 1458 Teemo]区间第k大问题,划分树
裸的区间第k大问题,划分树搞起. #pragma comment(linker, "/STACK:10240000") #include <map> #include ...
- POJ2104 k-th number 划分树
又是不带修改的区间第k大,这次用的是一个不同的方法,划分树,划分树感觉上是模拟了快速排序的过程,依照pivot不断地往下划分,然后每一层多存一个toleft[i]数组,就可以知道在这一层里从0到i里有 ...
- [hdu2665]Kth number(划分树求区间第k大)
解题关键:划分树模板题. #include<cstdio> #include<cstring> #include<algorithm> #include<cs ...
- 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(划分树)
Problem Description Give you a sequence and ask you the kth big number of a inteval. Input The fir ...
- poj 2104 K-th Number (划分树入门 或者 主席树入门)
题意:给n个数,m次询问,每次询问L到R中第k小的数是哪个 算法1:划分树 #include<cstdio> #include<cstring> #include<alg ...
- POJ 2104 K-th Number(划分树)
题目链接 参考HH大神的模版.对其中一些转移,还没想清楚,大体明白上是怎么回事了,划分树就是类似快排,但有点点区别的.多做几个题,慢慢理解. #include <cstdio> #incl ...
随机推荐
- Educational Codeforces Round 6 A. Professor GukiZ's Robot 水
A. Professor GukiZ's Robot Professor GukiZ makes a new robot. The robot are in the point with coor ...
- UVALive 4225 / HDU 2964 Prime Bases 贪心
Prime Bases Problem Description Given any integer base b >= 2, it is well known that every positi ...
- Creating new web parts kentico 10
Developing web parts https://docs.kentico.com/k10/custom-development/developing-web-parts Web parts ...
- mfc进制转换
; CString str; m_edit1.GetWindowTextW(str); swscanf_s(str, _T("%d"), &num); _TCHAR str ...
- highcharts的使用:从数据库获取数据显示在图上
//月产量统计图 function GetHighCharts() { var date = new Date(); var year = date.getFullYear(); var month_ ...
- FPGA之阻塞赋值与非阻塞赋值
Verilog语言中讲的阻塞赋值与非阻塞赋值,但从字面意思来看,阻塞就是执行的时候在某个地方卡住了,等这个操作执行完在继续执行下面的语句,而非阻塞就是不管执行完没有,我不管执行的结果是什么,反正我继续 ...
- DISM
C:\WINDOWS\system32>DISM /Online /Cleanup-image /RestoreHealth 部署映像服务和管理工具版本: 10.0.16193.1001 映像版 ...
- EL与JSTL学习(一)EL技术
1.EL 表达式概述 EL(Express Lanuage)表达式可以嵌入在jsp页面内部,减少jsp脚本的编写,EL出现的目的是要替代jsp页面中脚本的编写. 2.EL从域中取出数据(EL最重要的作 ...
- 使用python绘制词云
最近在忙考试的事情,没什么时间敲代码,一个月也没几天看代码,最近看到可视化的词云,看到网上也很多这样的工具, 但是都不怎么完美,有些不支持中文,有的中文词频统计得莫名其妙.有的不支持自定义形状.所有的 ...
- jq不懂的地方
在循环列表中,获取input标签的值,不能用id获取,用class获取值,通过父级属性找到class,this 指当前点击的位置var UID = $(this).parents("tr&q ...