POJ 2104 K-th Number [主席树]

来源:互联网 发布:windows android忘仙 编辑:程序博客网 时间:2024/05/16 04:47

K-th Number
Time Limit: 20000MS Memory Limit: 65536KB 64bit IO Format: %lld & %llu

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 10 9 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


一句话,静态区间第k小值的查询
节点大小问题:不带修改,那么就是 O(nloglim), <<5 就够了

#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<vector>#include<queue>#include<stack>#include<map>#include<set>#include<string>#include<iomanip>#include<ctime>#include<climits>#include<cctype>#include<algorithm>#ifdef WIN32#define AUTO "%I64d"#else#define AUTO "%lld"#endifusing namespace std;#define smax(x,tmp) x=max((x),(tmp))#define smin(x,tmp) x=min((x),(tmp))#define maxx(x1,x2,x3) max(max(x1,x2),x3)#define minn(x1,x2,x3) min(min(x1,x2),x3)const int INF=0x3f3f3f3f;const int maxn = 100005;struct Node{    int size;    int ch[2];}node[maxn<<5];int head[maxn];int maxnode;#define size(x) node[x].size#define ch(x,d) node[x].ch[d]void build(int &root,int l,int r){    root = ++maxnode;    if(l==r) return;    int m=(l+r)>>1;    build(ch(root,0),l,m);    build(ch(root,1),m+1,r);}void insert(int &root,int last,int l,int r,int key){    root = ++maxnode;    node[root] = node[last];    size(root)++;    if(l==r) return;    int m=(l+r)>>1;    if(key<=m) insert(ch(root,0),ch(last,0),l,m,key);    else insert(ch(root,1),ch(last,1),m+1,r,key);}int query(int root,int last,int l,int r,int k){    if(l==r) return l;    int m=(l+r)>>1;    int lsize = size(ch(root,0)) - size(ch(last,0));    if(k<=lsize) return query(ch(root,0),ch(last,0),l,m,k);    else return query(ch(root,1),ch(last,1),m+1,r,k-lsize);}struct Arr{    int val;    int order;    bool operator < (const Arr t) const    {        return val < t.val;    }}arr[maxn];int a[maxn]; // after discretedint rev[maxn]; // quick link from val_after_discrete to val_originalint n,q,lim;void init(){    scanf("%d%d",&n,&q);    for(int i=1;i<=n;i++) scanf("%d",&arr[i].val),arr[i].order=i;    sort(arr+1,arr+n+1);    int last = -INF , pos = 0;    for(int i=1;i<=n;i++)        if(last^arr[i].val) a[arr[i].order]=++pos , last=arr[i].val , rev[pos]=arr[i].val;        else a[arr[i].order]=pos;    lim = pos;}void work(){    build(head[0],1,lim);    for(int i=1;i<=n;i++)        insert(head[i],head[i-1],1,lim,a[i]);    while(q--)    {        int x,y,z;        scanf("%d%d%d",&x,&y,&z);        int ans = query(head[y],head[x-1],1,lim,z);        printf("%d\n",rev[ans]);    }}int main(){    freopen("chair.in","r",stdin);    freopen("chair.out","w",stdout);    init();    work();    return 0;}
0 0
原创粉丝点击