• poj 2104 K-th Number(主席树,详细有用)


    poj 2104 K-th Number(主席树)

    主席树就是持久化的线段树,添加的时候,每更新了一个节点的线段树都被保存下来了。
    查询区间[L,R]操作的时候,只需要用第R棵树减去第L-1棵树就是区间[L,R]中增加的元素对应的树,然后查询这棵两棵树的差值对应的树就可以达到我们的目的。
    每增加一个节点,必然有一条边被改变,那条边上的所有节点都会被改变。除这条边之外的其它节点用的是上一棵树的。
    K-th Number
    Time Limit: 20000MS   Memory Limit: 65536K
    Total Submissions: 62232   Accepted: 21860
    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.

    Source

    Northeastern Europe 2004, Northern Subregion

    添加的这个节点离散化之后是1,也就是最左下角的那个节点。

    总共是7个数,离散化管它有几个不重复的数,按照最大情况7个个数来算就对了。

    所以这7个数是依次会被放到这7个叶子节点上面的来。

    数的序列是1 5 2 6 3 7 4

    第一个数是1,离散化之后也是1,所以会被放到最左下角3那个位置,所以就是上图右边更新的情况,(注意看节点的下标)。

    第5棵树根节点的左边有3个,第一棵树根节点左边只有一个,多3-1=2个,但是我要找的是3大的,所以必定在第五棵树减去第一颗树的右边的第一个。

     

     

    测试代码(后面有AC代码)

      1 #include <cstdio>
      2 #include <cstring>
      3 #include <iostream>
      4 #include <vector>
      5 #include <algorithm>
      6 using namespace std;
      7 const int  MAXN = 100010;
      8 const int N = MAXN*40;
      9 int n,m,q,tot;
     10 int T[MAXN],A[MAXN],t[MAXN];
     11 //详单于结构体 
     12 int lson[N],rson[N],sum[N];
     13 vector<int>V;
     14 int getid(int x) //离散化
     15 {
     16     return lower_bound(V.begin(),V.end(),x)-V.begin()+1;
     17 }
     18 int build(int l,int r)  //建立一棵空树
     19 {
     20     int rt = tot++;
     21     sum[rt] = 0;//初始化,相当于初始化结构体中的sum元素 
     22     //不是叶子节点 
     23     if(l!=r){
     24         int mid=(l+r)>>1;
     25         lson[rt] = build(l,mid);
     26         rson[rt] = build(mid+1,r);
     27     }
     28     return rt;
     29 }
     30 
     31 
     32 //比如说第一组数是(0,1),表示继承的第0棵树,然后插入的那个数的id是1 
     33 int update(int rt,int pos)  //把数组中的元素一次加入新的线段树中
     34 {
     35     int nrt = tot++;//相当于节点13 
     36     int tmp = nrt;
     37     //sum[13]=sum[0]+1,这是插入第一个点的情况,表示sum[13]第一棵树比 sum[0]第0棵树多了一个元素 
     38     sum[nrt]  = sum[rt]+1;
     39     int l=1,r=m;//相当于从根节点开始更新 
     40     while(l<r) {//不停的从上往下(二分)去更新到那条路径的叶子节点 
     41         int mid = (l+r)>>1;
     42         //插入节点在线段树的左边 
     43         if(pos<=mid) {
     44             lson[nrt] = tot++;//左边的节点就是我们新创建的这个节点,相当于节点14 
     45             rson[nrt] = rson[rt];//右边节点就直接继承前一棵树 ,相当于节点8 
     46             nrt = lson[nrt];//让13节点向下走到14号节点 
     47             rt = lson[rt];//前一棵树继续往下走,把前一棵树0号节点的左孩子的值赋值给rt,方便让新的这棵树找得到5号节点,4号节点 
     48             r = mid;//二分
     49         }else {
     50             rson[nrt] = tot++;
     51             lson[nrt] = lson[rt];//左边直接继承前一棵树 
     52             nrt = rson[nrt];
     53             rt = rson[rt];
     54             l=mid+1;
     55         }
     56         sum[nrt] = sum[rt]+1;//节点在前一棵树的基础上面个数+1 
     57     }
     58     return tmp;
     59 }
     60 
     61 //第y棵树减第x-1棵树就是xy之间的元素,然后找到第k大的即可 
     62 //printf("%d
    ",V[query(T[x-1],T[y],k)-1]);
     63 //相当于是在两棵树的差树的那颗树上面找 
     64 int query(int lrt,int rrt,int k)
     65 {
     66     int l=1,r=m;//从根节点开始找 
     67     while(l<r) {//二分找 
     68         int mid = (l+r)>>1;
     69         int cnt = sum[lson[rrt]] - sum[lson[lrt]];//找到有多少个数目 
     70         if(cnt>=k) {
     71             r = mid;
     72             lrt = lson[lrt];
     73             rrt = lson[rrt];
     74         } else {
     75             l = mid+1;
     76             k-=cnt;//跑到右边去就要把左边的减掉 
     77             lrt = rson[lrt];//第一棵树和第五课树都去右孩子,然后是找差值 
     78             rrt = rson[rrt];
     79         }
     80     }
     81     return l;
     82 }
     83 //测试主席树 
     84 void print(){
     85     cout<<"i"<<"   "<<"lson[i]"<<"   "<<"rson[i]"<<"   "<<"sum[i]"<<"   "<<endl;
     86     for(int i=0;i<=6*n;i++){
     87         cout<<i<<"   "<<lson[i]<<"   "<<rson[i]<<"   "<<sum[i]<<"   "<<endl;
     88     } 
     89 } 
     90 //测试T数组
     91 void printT(){
     92     cout<<"i"<<"   "<<"T[i]"<<"   "<<endl;
     93     for(int i=0;i<=n;i++){
     94         cout<<i<<"   "<<T[i]<<"   "<<endl;
     95     } 
     96 } 
     97 int main()
     98 {
     99     freopen("in.txt","r",stdin);
    100     freopen("out.txt","w",stdout);
    101     //n个数q个操作 
    102     scanf("%d%d",&n,&q);tot=0;
    103     //A[i]存那些数 
    104     for(int i=1;i<=n;i++) {
    105         scanf("%d",&A[i]);
    106         V.push_back(A[i]);
    107     }
    108     sort(V.begin(),V.end());
    109     V.erase(unique(V.begin(),V.end()),V.end());
    110     //上面一部分是离散化 
    111     m=V.size();//找到不重复的元素的个数 
    112     cout<<m<<endl;
    113     
    114     T[0] = build(1,m);//建1到m的树 
    115     cout<<"tot: "<<tot<<endl; 
    116     print();
    117 //    cout<<T[0]<<endl; 
    118     printT();
    119     for(int i=1;i<=n;i++) {
    120         T[i] = update(T[i-1],getid(A[i]));
    121         cout<<i<<"   "<<getid(A[i])<<endl;
    122     }
    123     printT();
    124     while(q--) {
    125         int x,y,k;
    126         scanf("%d%d%d",&x,&y,&k);
    127         //第y棵树减第x-1棵树就是xy之间的元素,然后找到第k大的即可 
    128         //最后一个-1是因为V是从0开始 
    129         printf("%d
    ",V[query(T[x-1],T[y],k)-1]);
    130     }
    131     print();
    132     return 0;
    133 }

     AC代码

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <iostream>
     4 #include <vector>
     5 #include <algorithm>
     6 using namespace std;
     7 const int  MAXN = 100010;
     8 const int N = MAXN*40;
     9 int n,m,q,tot;
    10 int T[MAXN],A[MAXN],t[MAXN];
    11 int lson[N],rson[N],sum[N];
    12 vector<int>V;
    13 int getid(int x) //离散化
    14 {
    15     return lower_bound(V.begin(),V.end(),x)-V.begin()+1;
    16 }
    17 int build(int l,int r)  //建立一棵空树
    18 {
    19     int rt = tot++;
    20     sum[rt] = 0;
    21     if(l!=r){
    22         int mid=(l+r)>>1;
    23         lson[rt] = build(l,mid);
    24         rson[rt] = build(mid+1,r);
    25     }
    26     return rt;
    27 }
    28 
    29 int update(int rt,int pos)  //把数组中的元素一次加入新的线段树中
    30 {
    31     int nrt = tot++;
    32     int tmp = nrt;
    33     sum[nrt]  = sum[rt]+1;
    34     int l=1,r=m;
    35     while(l<r) {
    36         int mid = (l+r)>>1;
    37         if(pos<=mid) {
    38             lson[nrt] = tot++;
    39             rson[nrt] = rson[rt];
    40             nrt = lson[nrt];
    41             rt = lson[rt];
    42             r = mid;
    43         }else {
    44             rson[nrt] = tot++;
    45             lson[nrt] = lson[rt];
    46             nrt = rson[nrt];
    47             rt = rson[rt];
    48             l=mid+1;
    49         }
    50         sum[nrt] = sum[rt]+1;
    51     }
    52     return tmp;
    53 }
    54 
    55 int query(int lrt,int rrt,int k)
    56 {
    57     int l=1,r=m;
    58     while(l<r) {
    59         int mid = (l+r)>>1;
    60         int cnt = sum[lson[rrt]] - sum[lson[lrt]];
    61         if(cnt>=k) {
    62             r = mid;
    63             lrt = lson[lrt];
    64             rrt = lson[rrt];
    65         } else {
    66             l = mid+1;
    67             k-=cnt;
    68             lrt = rson[lrt];
    69             rrt = rson[rrt];
    70         }
    71     }
    72     return l;
    73 }
    74 int main()
    75 {//freopen("in.txt","r",stdin);
    76     scanf("%d%d",&n,&q);tot=0;
    77     for(int i=1;i<=n;i++) {
    78         scanf("%d",&A[i]);
    79         V.push_back(A[i]);
    80     }
    81     sort(V.begin(),V.end());
    82     V.erase(unique(V.begin(),V.end()),V.end());
    83     m=V.size();
    84     T[0] = build(1,m);
    85     for(int i=1;i<=n;i++) {
    86         T[i] = update(T[i-1],getid(A[i]));
    87     }
    88     while(q--) {
    89         int x,y,k;
    90         scanf("%d%d%d",&x,&y,&k);
    91         printf("%d
    ",V[query(T[x-1],T[y],k)-1]);
    92     }
    93     return 0;
    94 }
  • 相关阅读:
    java.lang.Object中的方法
    lyt经典版MySQL基础——进阶3:排序查询
    lyt经典版MySQL基础——进阶5:分组查询
    lyt经典版MySQL基础——DML语言-数据的插入、修改、删除操作
    lyt经典版MySQL基础——进阶8:联合查询
    lyt经典版MySQL基础——进阶7:子查询
    lyt经典版MySQL基础——进阶6:连接查询-sql99语法-内连接、外连接、交叉连接
    lyt经典版MySQL基础——进阶6:连接查询-sql92语法-内连接
    lyt经典版MySQL基础——进阶4:常见函数-分组函数
    lyt经典版MySQL基础——进阶2:条件查询
  • 原文地址:https://www.cnblogs.com/Renyi-Fan/p/8135856.html
Copyright © 2020-2023  润新知