[POJ2104] K-th Number/[POJ2761] Feed the dogs
来源:互联网 发布:js设置innerhtml 编辑:程序博客网 时间:2024/06/11 03:22
K-th Number
Time Limit: 20000MS Memory Limit: 65536K
Description
You are working for Macrohard company in data structures department. After failing your previous task about key insertion you were asked to write a new data structure that would be able to return quickly k-th order statistics in the array segment.
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
The 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
For each question output the answer to it — the k-th number in sorted a[i…j] segment.
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
This problem has huge input,so please use c-style input(scanf,printf),or you may got time limit exceed.
Source
Northeastern Europe 2004, Northern Subregion
题目大意
给n个数的序列,m次查询[L,R]内的第k小
题解
- 主席树裸题
- 贴一个比较个人感觉比较清楚的http://im.librazy.org/article/837/note-chairtree-via-functional-segtree/
- 如果还是不懂可以看看CLJ的论文,或者范浩强的WC讲稿
var w:array[0..2000000,1..3]of longint; x:array[0..100000,1..2]of longint; root,y:array[0..100000]of longint; i,j,k:longint; n,m:longint; len,ans,a,b:longint;procedure sort(l,r: longint);var i,j,a,z: longint;begin i:=l; j:=r; a:=x[(l+r) div 2,1]; repeat while x[i,1]<a do inc(i); while a<x[j,1] do dec(j); if not(i>j) then begin z:=x[i,1]; x[i,1]:=x[j,1]; x[j,1]:=z; z:=x[i,2]; x[i,2]:=x[j,2]; x[j,2]:=z; inc(i); dec(j); end; until i>j; if l<j then sort(l,j); if i<r then sort(i,r);end;procedure init(l,r,x:longint;var y:longint;v:longint);var mid:longint;begin y:=len+1; inc(len); w[y,1]:=w[x,1]+1; if l=r then exit; w[y,2]:=w[x,2]; w[y,3]:=w[x,3]; mid:=(l+r)>>1; if (v<=mid) then begin w[y,2]:=len+1; init(l,mid,w[x,2],w[y,2],v) end else begin w[y,3]:=len+1; init(mid+1,r,w[x,3],w[y,3],v); end;end;function query(a,b,l,r,k:longint):longint;var mid,t:longint;begin if l=r then exit(l); t:=w[w[b,2],1]-w[w[a,2],1]; mid:=(l+r)>>1; if t>=k then exit(query(w[a,2],w[b,2],l,mid,k)) else exit(query(w[a,3],w[b,3],mid+1,r,k-t));end;begin readln(n,m); for i:=1 to n do begin read(x[i,1]); x[i,2]:=i; end; sort(1,n); {x[i,1]} for i:=1 to n do y[x[i,2]]:=i; len:=0; for i:=1 to n do init(1,n,root[i-1],root[i],y[i]); for i:=1 to m do begin readln(a,b,k); ans:=query(root[a-1],root[b],1,n,k); writeln(x[ans,1]); end;end.
- 【主席树】poj2104 K-th Number && poj2761 Feed the dogs
- [POJ2104] K-th Number/[POJ2761] Feed the dogs
- POJ2104 POJ2761 K-th Number, 划分树
- 【划分树】K-th Number POJ2104 POJ2761
- [poj2761]Feed the dogs
- POJ2761 Feed the dogs
- POJ2761 Feed the dogs
- POJ2761--Feed the dogs
- poj2761 Feed the dogs
- 【poj2761】 Feed the dogs
- POJ2761 Feed the dogs(treap)
- [Treap] poj2761 Feed the dogs
- hdu 2665 Kth number || poj 2761 Feed the dogs || poj 2104 K-th Number
- poj2761 Feed the dogs【解法一】
- poj2761 Feed the dogs【解法二】
- [poj2104]K-th Number
- [Poj2104]K-th Number
- [POJ2104]K-th Number
- 导入excel数据,导出数据到excel
- Map, Map and flatMap in Scala
- 冒泡排序算法
- 拓展欧几里得学习小记
- nc后台两张单同名字字段数据交换
- [POJ2104] K-th Number/[POJ2761] Feed the dogs
- iOS 以byte数组作为密钥的3des加密
- hdu1757
- HDU 2255 奔小康赚大钱 POJ 2195 Going Home 最大权完美匹配 KM算法
- 在Eclipse中安装Maven插件以及创建Maven项目
- Java学习个人笔记---Java数组内存
- button放在form表单导致点击按钮触发的ajax报错问题
- IOS 自动约束
- POJ 1564 Sum It Up