【POJ】2104 K-th Number(区间k大+主席树)
http://poj.org/problem?id=2104
裸题不说。主席树水过。
#include <cstdio> #include <iostream> #include <algorithm> using namespace std; #define dbg(x) cout << #x << "=" << x << endl #define read(x) x=getint() #define for1(i, a, n) for(int i=a; i<=(n); ++i) #define MID (l+r)>>1 inline int getint() { char c; int ret=0, k=1; for(c=getchar(); c<'0' || c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0' && c<='9'; c=getchar()) ret=ret*10+c-'0'; return ret*k; } const int N=100010; int id[N], a[N], b[N], n, m, root[N], cnt, key; struct node { int l, r, s; } T[N*50]; void update(const int &l, const int &r, int &pos) { T[++cnt]=T[pos]; pos=cnt; ++T[pos].s; if(l==r) return; int m=MID; if(key<=m) update(l, m, T[pos].l); else update(m+1, r, T[pos].r); } int query(const int &l, const int &r, const int &x, const int &y, const int &k) { if(l==r) return l; int m=MID, s=T[T[y].l].s-T[T[x].l].s; if(k<=s) return query(l, m, T[x].l, T[y].l, k); else return query(m+1, r, T[x].r, T[y].r, k-s); } bool cmp(const int &x, const int &y) { return a[x]<a[y]; } int main() { read(n); read(m); for1(i, 1, n) read(a[i]), id[i]=i; sort(id+1, id+1+n, cmp); for1(i, 1, n) b[id[i]]=i; for1(i, 1, n) { root[i]=root[i-1]; key=b[i]; update(1, n, root[i]); } int x, y; while(m--) { read(x); read(y); read(key); printf("%d\n", a[id[query(1, n, root[x-1], root[y], key)]]); } return 0; }
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.
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).
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
博客地址:www.cnblogs.com/iwtwiioi 本文为博主原创文章,未经博主允许不得转载。一经发现,必将追究法律责任。