zoukankan      html  css  js  c++  java
  • HDU 2852 主席树

    KiKi's K-Number

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 3966    Accepted Submission(s): 1758


    Problem Description
    For the k-th number, we all should be very familiar with it. Of course,to kiki it is also simple. Now Kiki meets a very similar problem, kiki wants to design a container, the container is to support the three operations.

    Push: Push a given element e to container

    Pop: Pop element of a given e from container

    Query: Given two elements a and k, query the kth larger number which greater than a in container;

    Although Kiki is very intelligent, she can not think of how to do it, can you help her to solve this problem?
     
    Input
    Input some groups of test data ,each test data the first number is an integer m (1 <= m <100000), means that the number of operation to do. The next m lines, each line will be an integer p at the beginning, p which has three values:
    If p is 0, then there will be an integer e (0 <e <100000), means press element e into Container.

    If p is 1, then there will be an integer e (0 <e <100000), indicated that delete the element e from the container  

    If p is 2, then there will be two integers a and k (0 <a <100000, 0 <k <10000),means the inquiries, the element is greater than a, and the k-th larger number.
     
    Output
    For each deletion, if you want to delete the element which does not exist, the output "No Elment!". For each query, output the suitable answers in line .if the number does not exist, the output "Not Find!".
     
    Sample Input
    5 0 5 1 2 0 6 2 3 2 2 8 1 7 0 2 0 2 0 4 2 1 1 2 1 2 2 1 3 2 1 4
     
    Sample Output
    No Elment! 6 Not Find! 2 2 4 Not Find!
     
    Source
    题意:
    一个空的容器,进行三种操作:
    0  a :  把a加进容器里;
    1  a :  把a从容器中删除,如果没有a则输出No Elment!,如果有多个a则只删除一个;
    2  a  k : 求整个容器中所有比a大的数中的第k大的数,并输出,如果没有则输出NO Finds!。
    题解:因为数的范围比较小 所以不需要离散化。主席树记录线段树的历史版本。查找容器中比a大的第k大的数字可以 先查询范围在[1,a]的数字数量q
    求第q+k大即可。
      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstdlib>
      4 #include <cstring>
      5 #include <algorithm>
      6 #include <stack>
      7 #include <queue>
      8 #include <cmath>
      9 #include <map>
     10 #define ll  __int64
     11 #define mod 1000000007
     12 #define dazhi 2147483647
     13 const int N=100005;
     14 using namespace  std;
     15 struct chairmantree
     16 {
     17     int rt[20*N],ls[20*N],rs[20*N],sum[20*N];
     18     int tot;
     19     void init()
     20     {
     21         tot=0;
     22     }
     23     void build(int l,int r,int &pos)
     24     {
     25         pos=++tot;
     26         sum[pos]=0;
     27         if(l==r) return ;
     28         int mid=(l+r)>>1;
     29         build(l,mid,ls[pos]);
     30         build(mid+1,r,rs[pos]);
     31     }
     32     void update(int p,int c,int l,int r,int pre,int &pos)
     33     {
     34         pos=++tot;
     35         ls[pos]=ls[pre];
     36         rs[pos]=rs[pre];
     37         sum[pos]=sum[pre]+c;
     38         if(l==r) return ;
     39         int mid=(l+r)>>1;
     40         if(p<=mid)
     41             update(p,c,l,mid,ls[pre],ls[pos]);
     42         else
     43             update(p,c,mid+1,r,rs[pre],rs[pos]);
     44     }
     45     int rank(int s,int e,int l,int r,int L,int R)
     46     {
     47         if(s<=l&&e>=r)  return sum[R]-sum[L];
     48         int ans=0;
     49         int mid=(l+r)>>1;
     50         if(s<=mid)
     51             ans+=rank(s,e,l,mid,ls[L],ls[R]);
     52         if(e>mid)
     53             ans+=rank(s,e,mid+1,r,rs[L],rs[R]);
     54         return ans;
     55     }
     56     int query(int L,int R,int l,int r,int k)
     57     {
     58         if(l==r) return l;
     59         int  mid=(l+r)>>1;
     60         int x=sum[ls[R]]-sum[ls[L]];
     61         if(k<=x)
     62             query(ls[L],ls[R],l,mid,k);
     63         else
     64             query(rs[L],rs[R],mid+1,r,k-x);
     65     }
     66 } tree;
     67 int m;
     68 int exm;
     69 int e;
     70 int main()
     71 {
     72     int rr=100005;
     73     while(scanf("%d",&m)!=EOF)
     74     {
     75         tree.init();
     76         tree.build(1,rr,tree.rt[0]);
     77         for(int i=1; i<=m; i++)
     78         {
     79             scanf("%d",&exm);
     80             if(exm==0)
     81             {
     82                 scanf("%d",&e);
     83                 tree.update(e,1,1,rr,tree.rt[i-1],tree.rt[i]);
     84             }
     85             if(exm==1)
     86             {
     87                 scanf("%d",&e);
     88                 if(tree.rank(e,e,1,rr,tree.rs[0],tree.rt[i-1]))
     89                 {
     90                     tree.update(e,-1,1,rr,tree.rt[i-1],tree.rt[i]);
     91                 }
     92                 else
     93                 {
     94                     tree.update(e,0,1,rr,tree.rt[i-1],tree.rt[i]);
     95                     printf("No Elment!
    ");
     96                 }
     97             }
     98             if(exm==2)
     99             {
    100                 int a,k;
    101                 scanf("%d %d",&a,&k);
    102                 tree.update(1,0,1,rr,tree.rt[i-1],tree.rt[i]);
    103                 int q=tree.rank(1,a,1,rr,tree.rt[0],tree.rt[i]);
    104                 int xx=tree.rank(1,rr,1,rr,tree.rt[0],tree.rt[i]);
    105                 k+=q;
    106                 if(k>xx)
    107                     printf("Not Find!
    ");
    108                 else
    109                     printf("%d
    ",tree.query(tree.rt[0],tree.rt[i],1,rr,k));
    110             }
    111         }
    112     }
    113     return 0;
    114 }
  • 相关阅读:
    Liferay安装maven
    html之pre标签
    a标签使用注意事项
    AngularJS学习记录
    页面不能访问,抛出 spring java.lang.IllegalArgumentException: Name for argument type [java.lang.String] 异常
    ant编译的时候,报错文件不存在,以及版本不一致
    Eclipse 更改Java 版本的方法
    总结一下本次准备环境时遇到的问题,以供下次参考
    数据上下文中的AddOrUpdate方法
    推荐一款github管理神器SourceTree
  • 原文地址:https://www.cnblogs.com/hsd-/p/6535295.html
Copyright © 2011-2022 走看看