Chairman of the tree (of sustainable tree line)

Learning crude: https: //blog.csdn.net/creatorx/article/details/75446472

Title: http: //poj.org/problem id = 2104 (Static Chairman of the tree)?

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back

inline int read(){
    int sum=0,x=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')
            x=0;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
        sum=(sum<<1)+(sum<<3)+(ch^48),ch=getchar();
    return x?sum:-sum;
}
inline void write(int x){
    if(x<0)
        putchar('-'),x=-x;
    if(x>9)
        write(x/10);
    putchar(x%10+'0');
}
const int M=1e5+5;
int n,m,cnt=1;
struct node{
    int L,R,sum;
}tree[M*20];
struct NODE{
    int x,id;
}a[M];
bool cmp(NODE p,NODE q){
    return p.x<q.x;
}
int root[M],Rank[M];
void update(int pos,int &root,int l,int r){
    tree[cnt++]=tree[root];
    root=cnt-1;
    tree[root].sum+=1;
    if(l==r)
        return ;
    int midd=(l+r)>>1;
    if(pos<=midd)
        update(pos,tree[root].L,l,midd);
    else
        update(pos,tree[root].R,midd+1,r);
    
}
int query(int i,int j,int k,int l,int r){
    int d=tree[tree[j].L].sum-tree[tree[i].L].sum;
    if(l==r)
        return l;
    int midd=(l+r)>>1;
    if(k<=d)
        return query(tree[i].L,tree[j].L,k,l,midd);
    else
        return query(tree[i].R,tree[j].R,k-d,midd+1,r);
}
int main(){
    n=read(),m=read();
//    write(n);
//    puts("");
//    write(m);
    for(int i=1;i<=n;i++){
        a[i].x=Read (); 
        A [I] .id = I; 
    } 
    // Lisan 
    Sort (A + . 1 , A + n-+ . 1 , CMP);
     for ( int I = . 1 ; I <= n-; I ++ ) { 
        Rank [A [I ] .id] = i; // Rank [i] = j: prosequence is the i-th position of the j name (ascending sequence of the original) 
    }
     for ( int i = . 1 ; i <= n-; i ++ ) { 
        the root [ I] = the root [I- . 1 ]; 
        Update (Rank [I], the root [I], . 1 , n-); 
    } 
    for ( int I = . 1 ; I <= m; I ++){
        int l=read(),r=read(),k=read();
        write(a[query(root[l-1],root[r],k,1,n)].x);
        putchar('\n');
    }
    return 0;
}
View Code

 

Guess you like

Origin www.cnblogs.com/starve/p/11415452.html