【可持久化线段树】POJ2104 查询区间第k小值

K-th Number
Time Limit: 20000MS   Memory Limit: 65536K
Total Submissions: 61284   Accepted: 21504
Case Time Limit: 2000MS

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.

题解

可持久化线段树,就是动态的去开点

如果一段区间并没有被修改,那么我们就可以直接让指针指向这一段区间

如果被修改,那么我们再新建点去存它修改过后的区间

这样一来显然每次最多新建树的深度个点,也就是logn个点

然后说说基本操作

单点修改:

显然我们要修改某个点的值而不影响历史版本的线段树

那么我们就在从根节点向下找目标节点时,把路径上的点都复制一份,再在回溯时修改

最多增加logn个点

区间修改:

显然我们要修改某个区间的值而不影响历史版本的线段树

那么我们就往下走,记得pushdown和新开点

找到区间后就打标记

区间查询:

从root[now]出发,向下走,记得pushdown(pushdown的时候也要新开节点,不要忘了)

代码

//by 减维
#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<map>
#include<bitset>
#include<algorithm>
#define ll long long
#define ls l,mid
#define rs mid+1,r
using namespace std;

struct number{
    int val,pos;
}a[200005];

struct tree{
    int l,r,sum;
}t[12000005];

int n,m,cnt,ys[200005],root[200005];

bool cmp(const number&x,const number&y){return x.val<y.val;}

void build(int num,int &x,int l,int r)
{
    t[++cnt]=t[x];x=cnt;
    ++t[x].sum;
    if(l==r)return ;
    int mid=(l+r)>>1;
    if(num<=mid)build(num,t[x].l,ls);
    else build(num,t[x].r,rs);
}

int ask(int x,int y,int k,int l,int r)
{
    if(l==r)return l;
    int  tt=t[t[y].l].sum-t[t[x].l].sum;
    int mid=(l+r)>>1;
    if(k<=tt)return ask(t[x].l,t[y].l,k,ls);
    else return ask(t[x].r,t[y].r,k-tt,rs);//记得一定要减去tt
}

int main()
{
    scanf("%d%d",&n,&m);
    t[0]=(tree){0,0,0};
    for(int i=1;i<=n;++i)
    {
        scanf("%d",&a[i].val);
        a[i].pos=i;
    }
    sort(a+1,a+n+1,cmp);
    for(int i=1;i<=n;++i)ys[a[i].pos]=i;
    for(int i=1;i<=n;++i)
    {
        root[i]=root[i-1];
        build(ys[i],root[i],1,n);
    }
    for(int i=1,x,y,z;i<=m;++i)
    {
        scanf("%d%d%d",&x,&y,&z);
        printf("%d
",a[ask(root[x-1],root[y],z,1,n)].val);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/rir1715/p/7839639.html