POJ 2104 K-th Number [主席树]

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"
#endif
using 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 discreted
int rev[maxn]; // quick link from val_after_discrete to val_original
int 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值