poj 2104 查询区间第k小 主席树 (递归和非递归)模板

题目链接:http://poj.org/problem?id=2104
K-th Number

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.

题意:区间第k大。思路:主席树(裸模板)

以前听说过,觉得很难就不敢学,毕竟线段树我基础也不扎实,最开始看了一个视频才了解到主席树,这个视频把主席树的思想讲了出来,在看代码的时候省了不少事,https://www.bilibili.com/video/av10796614/   对主席树建的n颗树不懂的的话最好看看。
http://blog.csdn.net/htt_h/article/details/47704209这个是我代码的来源,
非递归AC代码:(我先看着模板打了一遍,然后按自己理解又打了一遍,感觉还不算太难)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;

const int maxn=1e5+10;
const int M=maxn*30;

int n,q,m,tot;
int a[maxn], t[maxn];
int T[maxn], lson[M], rson[M], c[M];

void init_hash()
{
    for(int i=1;i<=n;i++)
        t[i]=a[i];
    sort(t+1,t+n+1);
    m=unique(t+1,t+1+n)-t-1;
}

int hash(int x)
{
    return lower_bound(t+1,t+1+m,x)-t;
}

int build(int l,int r)
{
    int rt=tot++;
    c[rt]=0;
    if(l<r){
        int mid=(l+r)>>1;
        lson[rt]=build(l,mid);
        rson[rt]=build(mid+1,r);
    }
    return rt;
}

int update(int rt,int pos,int val)
{
    int newrt=tot++,tmp=newrt;
    c[newrt]=c[rt]+val;
    int l=1,r=m;
    while(l<r){
        int mid=(l+r)>>1;
        if(pos<=mid){
            lson[newrt]=tot++; rson[newrt]=rson[rt];
            newrt=lson[newrt]; rt=lson[rt];
            r=mid;
        }
        else{
            rson[newrt]=tot++; lson[newrt]=lson[rt];
            newrt=rson[newrt]; rt=rson[rt];
            l=mid+1;
        }
        c[newrt]=c[rt]+val;
    }
    return tmp;
}

int query(int lrt,int rrt,int k)
{
    int l=1,r=m;
    while(l<r){
        int mid=(l+r)>>1;
        if(c[lson[lrt]]-c[lson[rrt]]>=k){
            r=mid;
            lrt=lson[lrt];
            rrt=lson[rrt];
        }
        else{
            l=mid+1;
            k-=c[lson[lrt]]-c[lson[rrt]];
            lrt=rson[lrt];
            rrt=rson[rrt];
        }
    }
    return l;
}

int main()
{
    while(~scanf("%d%d",&n,&q)){
        for(int i=1;i<=n;i++)
            scanf("%d",a+i);
        tot=0;
        init_hash();
        T[n+1]=build(1,n);
        for(int i=n;i;i--){
            int pos=hash(a[i]);
            T[i]=update(T[i+1],pos,1);
        }
        while(q--){
            int l,r,k;
            scanf("%d%d%d",&l,&r,&k);
            printf("%d\n",t[query(T[l],T[r+1],k)]);
        }
    }
    return 0;
}

递归AC代码:(看了俩小时终于懂了是怎么递归的)代码来源(里面有讲解):  http://www.cnblogs.com/oyking/p/3230296.html
#include <cstdio>
#include <algorithm>
using namespace std;

const int MAXN = 100010;

struct Node {
    int L, R, sum;//L为左区间节点的编号,R为右区间节点的编号。注意:L,R是节点编号,不是区间两端点值,
};
Node T[MAXN * 20];
int T_cnt;

void insert(int &num, int &x, int L, int R) {//x为引用,所以最后的x值就是它引用对象的值。***先复制x节点***再更改x值去回溯***!!!
    T[T_cnt++] = T[x]; x = T_cnt - 1;
    ++T[x].sum;
    if(L == R) return ;
    int mid = (L + R) >> 1;
    if(num <= mid) insert(num, T[x].L, L, mid);
    else insert(num, T[x].R, mid + 1, R);
}

int query(int i, int j, int k, int L, int R) {
    if(L == R) return L;
    int t = T[T[j].L].sum - T[T[i].L].sum;
    int mid = (R + L) >> 1;
    if(k <= t) return query(T[i].L, T[j].L, k, L, mid);
    else return query(T[i].R, T[j].R, k - t, mid + 1, R);
}

struct A {
    int x, idx;
    bool operator < (const A &rhs) const {
        return x < rhs.x;
    }
};

A a[MAXN];
int rank[MAXN], root[MAXN];
int n, m;

int main() {
    T[0].L = T[0].R = T[0].sum = 0;//初始节点。
    root[0] = 0;
    while(scanf("%d%d", &n, &m) != EOF) {
        for(int i = 1; i <= n; ++i) {
            scanf("%d", &a[i].x);
            a[i].idx = i;
        }
        sort(a + 1, a + n + 1);
        for(int i = 1; i <= n; ++i) rank[a[i].idx] = i;//离散化
        T_cnt = 1;
        for(int i = 1; i <= n; ++i) {
            root[i] = root[i - 1];
            insert(rank[i], root[i], 1, n);
        }
        while(m--) {
            int i, j, k;
            scanf("%d%d%d", &i, &j, &k);
            printf("%d\n", a[query(root[i - 1], root[j], k, 1, n)].x);
        }
    }
}








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值