hdu 2665 Kth number

Written with StackEdit.node

Description

Give you a sequence and ask you the kth big number of a interval.c++

Input

The first line is the number of the test cases.
For each test case, the first line contain two integer n and m (n, m <= 100000), indicates the number of integers in the sequence and the number of the queries.
The second line contains n integers, describe the sequence.
Each of following m lines contains three integers s, t, k.
[s, t] indicates the interval and k indicates the kth big number in interval [s, t]學習

Output

For each test case, output m lines. Each line contains the kth big number.ui

Sample Input

1
10 1
1 4 2 3 5 6 7 8 9 0
1 3 2spa

Sample Output

2code

Solution

發現我一直不會主席樹,因而今天找了個板子題來學習一下...遞歸

  • 主席樹是可持久化線段樹,而對區間查詢第\(k\)大問題,更準確的說,是可持久化權值線段樹.因此須要有權值線段樹的前置技能.
  • 考慮對區間\([1,1],[1,2],[1,3]...[1,n]\)都各自維護一顆權值線段樹.那麼詢問\([L,R]\)時,某個數的出現次數就應該是這個數在\([1,R]\)這顆權值線段樹中的出現次數減去在\([1,L-1]\)這顆權值線段樹中的出現次數.
  • 這就是其餘博客中所說的\(T[R]-T[L-1]\)的含義.咱們在查詢時,就能夠當作是在一顆權值線段樹裏查詢第\(k\)大,這裏就是基本操做了.
  • 然而,若是真的去建\(n\)顆線段樹,空間沒法承受.注意到後一顆線段樹比前一顆線段樹,只有修改的位置到根節點的路徑有變化,最多\(logn\)個節點.因此咱們新建線段樹的時候,能夠直接複製上顆樹對應節點的信息.遞歸後鏈上的信息會自動被修改(參見代碼).
#include<bits/stdc++.h>
using namespace std;
typedef long long LoveLive;
inline int read()
{
    int out=0,fh=1;
    char jp=getchar();
    while ((jp>'9'||jp<'0')&&jp!='-')
        jp=getchar();
    if (jp=='-')
        {
            fh=-1;
            jp=getchar();
        }
    while (jp>='0'&&jp<='9')
        {
            out=out*10+jp-'0';
            jp=getchar();
        }
    return out*fh;
}
const int MAXN=1e5+10;
int a[MAXN],b[MAXN];
int n,m;
int rt[MAXN];
struct PreSegTree{
    struct node{
        int lson,rson,cnt;
    }Tree[MAXN*20];
    int idx,siz;
    PreSegTree()
        {
            idx=0;
            Tree[0].lson=Tree[0].rson=Tree[0].cnt=0;
        }
    int BuildTree(int l,int r)
        {
            int cur=++idx;
            Tree[cur].cnt=0;
            if(l==r)
                return cur;
            int mid=(l+r)>>1;
            Tree[cur].lson=BuildTree(l,mid);
            Tree[cur].rson=BuildTree(mid+1,r);
            return cur;
        }
    void update(int &cur,int last,int l,int r,int pos)
        {
            cur=++idx;
            Tree[cur]=Tree[last];
            ++Tree[cur].cnt;
            if(l==r)
                return;
            int mid=(l+r)>>1;
            if(pos<=mid)
                update(Tree[cur].lson,Tree[last].lson,l,mid,pos);
            else
                update(Tree[cur].rson,Tree[last].rson,mid+1,r,pos);
        }
    int query(int L,int R,int l,int r,int k)
        {
            if(l==r)
                return l;
            int p=Tree[Tree[R].lson].cnt-Tree[Tree[L].lson].cnt;
            int mid=(l+r)>>1;
            if(p>=k)
                return query(Tree[L].lson,Tree[R].lson,l,mid,k);
            else
                return query(Tree[L].rson,Tree[R].rson,mid+1,r,k-p);
        }
}T;
int main()
{
    int cases=read();
    while(cases--)
        {
            n=read(),m=read();
            for(int i=1;i<=n;++i)
                b[i]=a[i]=read();
            sort(b+1,b+1+n);
            int siz=unique(b+1,b+1+n)-(b+1);
            T.idx=0;
            rt[0]=T.BuildTree(1,siz);
            for(int i=1;i<=n;++i)
                {
                    a[i]=lower_bound(b+1,b+1+siz,a[i])-b;
                    T.update(rt[i],rt[i-1],1,siz,a[i]);
                }
            for(int i=1;i<=m;++i)
                {
                    int L=read(),R=read(),k=read();
                    int ans=T.query(rt[L-1],rt[R],1,siz,k);
                    printf("%d\n",b[ans]);
                }
        }
    return 0;
}
相關文章
相關標籤/搜索