zoukankan      html  css  js  c++  java
  • HDU 2852 KiKi's K-Number (主席树)

    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? 

    InputInput 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. 
    OutputFor 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!

    题解:题目有3种操作,0 : 插入一个数; 1 :删除一个数(如果不存在,就输出“No Elment!”), 2 : 查询大于 a 的第K小的数,没有就输出"Not Find!" ;
    用可持久化线段树,看代码即可明白几乎是模版;
     
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 const int maxn=1e5+10;
     4 int n,tot,rt[maxn*24];
     5 
     6 struct Node{
     7     int l,r,sum;
     8 } tree[maxn*24];
     9 
    10 inline void build(int l,int r,int &pos) 
    11 {  
    12     pos = ++tot;  
    13     tree[pos].sum = 0;  
    14     if(l==r) return;  
    15     int m = (l+r) >> 1;  
    16     build(l,m,tree[pos].l);  
    17     build(m+1,r,tree[pos].r);  
    18 }  
    19 
    20 inline void update(int p,int c,int pre,int l,int r,int &pos)
    21 {
    22     pos=++tot; tree[pos]=tree[pre]; tree[pos].sum+=c;
    23     if(l==r)return;
    24     int mid=(l+r)>>1;
    25     if(p<=mid) update(p,c,tree[pre].l,l,mid,tree[pos].l);
    26     else update(p,c,tree[pre].r,mid+1,r,tree[pos].r);
    27 }
    28 
    29 inline int Rank(int s,int e,int L,int R,int l,int r)
    30 {
    31     if(L<=l&&r<=R) return tree[e].sum-tree[s].sum;
    32     int mid=(l+r)>>1;
    33     int ans=0;
    34     if(L<=mid) ans+=Rank(tree[s].l,tree[e].l,L,R,l,mid);
    35     if(R>mid) ans+=Rank(tree[s].r,tree[e].r,L,R,mid+1,r);
    36     return ans;
    37 }
    38 
    39 inline int query(int L,int R,int l,int r,int k)
    40 {
    41     if(l==r)return l;
    42     int mid=(l+r)>>1;
    43     int x=tree[tree[R].l].sum-tree[tree[L].l].sum;
    44     if(k<=x) return query(tree[L].l,tree[R].l,l,mid,k);
    45     else return query(tree[L].r,tree[R].r,mid+1,r,k-x);
    46 }
    47 
    48 int main()
    49 {
    50     int right=maxn;
    51     while(~scanf("%d",&n))
    52     {
    53         tot=0;
    54         build(1,right,rt[0]);
    55         for(int i=1;i<=n;i++)
    56         {
    57             int x;
    58             scanf("%d",&x);
    59             if(x==0)
    60             {
    61                 int z;
    62                 scanf("%d",&z);
    63                 update(z,1,rt[i-1],1,right,rt[i]);
    64             }
    65             else if(x==1)
    66             {
    67                 int z;
    68                 scanf("%d",&z);
    69                 if(Rank(rt[0],rt[i-1],z,z,1,right)) update(z,-1,rt[i-1],1,right,rt[i]);
    70                 else
    71                 {
    72                     update(z,0,rt[i-1],1,right,rt[i]);
    73                     printf("No Elment!
    ");
    74                 }
    75             }
    76             else
    77             {
    78                 update(1,0,rt[i-1],1,right,rt[i]);
    79                 int a,k;
    80                 scanf("%d%d",&a,&k);
    81                 int v=Rank(rt[0],rt[i],1,a,1,right);
    82                 k+=v;
    83                 int q=Rank(rt[0],rt[i],1,right,1,right);
    84                 if(k>q) printf("Not Find!
    ");
    85                 else printf("%d
    ",query(rt[0],rt[i],1,right,k));
    86             }
    87         }
    88     }
    89     return 0;
    90 }
    View Code
  • 相关阅读:
    改进SENet-ECA-Net: Efficient Channel Attention for Deep Convolutional Neural Networks
    组集成网络-Group Ensemble: Learning an Ensemble of ConvNets in a single ConvNet
    轻量级网络-ReXNet:Diminishing Representational Bottleneck on Convolutional Neural Network
    ULSAM:Ultra-Lightweight Subspace Attention Module for Compact Convolutional Neural Networks
    工作中常用的 Shell 命令及技巧
    程序员需要熟悉的英语单词
    程序员基本素养
    Java 基础 —— Lambda 表达式
    将博客搬至CSDN
    IDEA 插件推荐 —— 让你写出好代码的神器!
  • 原文地址:https://www.cnblogs.com/csushl/p/9469225.html
Copyright © 2011-2022 走看看