hdu2852--KiKi's K-Number(线段树,求第k个数)

KiKi‘s K-Number

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2546    Accepted Submission(s): 1174

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!

三种操作,0 代表添加一个数x,1代表删除一个数x,2代表 找比a大的第k个数,使用线段树求解,线段树统计在一个区间内出现的数的个数,对于找比a大的第k个数,从a开始向后查找,如果在某段中累加的和大于k,就让它跳入这段中,直到深入到一个叶子节点时,刚好ans >= k。

对线段树的更新不解释,主要是查询的时候

1.如果当前区间[l,r]中 r<=a那么这一段的数都不用统计。

2.如果r >a,代表该段中存在比a大的数就要向下深入。

3.如果l > a,那么该段中所有的点都会大于a,开始判断,如果该段全部加入后仍然小于k,那么就可以全部加入,如果加进去以后大于等于k,那么就要向下深入,一直深入到叶子节点,满足条件的,最左的叶子节点就是我们要求的值。(线段树,从左向右查找,一定可以找到第一个)

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define maxn 110000
#define lmin 1
#define rmax n
#define lson l,(l+r)/2,rt<<1
#define rson (l+r)/2+1,r,rt<<1|1
#define root lmin,rmax,1
#define now l,r,rt
#define int_now int l,int r,int rt
int cl[maxn<<2] , k1[maxn] , k2[maxn<<2] , top ;
void push_up(int_now)
{
    cl[rt] = cl[rt<<1] + cl[rt<<1|1] ;
}
void creat(int_now)
{
    cl[rt] = 0 ;
    if(l != r)
    {
        creat(lson);
        creat(rson);
        push_up(now);
    }
    else
    {
        cl[rt] = 0 ;
        k2[rt] = ++top ;
    }
}
void update(int x,int d,int_now)
{
    if( l > x || r < x )
        return ;
    if( l == r && l == x )
    {
        cl[rt] += d ;
        return ;
    }
    update(x,d,lson);
    update(x,d,rson);
    push_up(now);
}
int query(int ll,int ans,int num,int_now)
{
    if( r <= ll )
        return 0;
    if( ll < l )
    {
        if( ans + cl[rt] < num )
        return ans + cl[rt] ;
        if(ans < num && ans + cl[rt] >= num && l == r )
        {
            printf("%d\n", k2[rt] );
            return ans + cl[rt] ;
        }
        if(ans < num && ans + cl[rt] >= num )
        {
            if( ans + cl[rt<<1] >= num )
                ans = query(ll,ans,num,lson);
            else
                ans = query(ll,ans+cl[rt<<1],num,rson);
            return ans ;
        }
    }
    else
    {
        if( ans < num )
            ans = query(ll,ans,num,lson);
        if(ans < num)
            ans = query(ll,ans,num,rson);
        return ans;
    }
}
int main()
{
    int m , i , n , l , r , x , temp , num ;
    while(scanf("%d", &m) != EOF)
    {
        top = 0 ;
        n = maxn ;
        creat(root);
        memset(k1,0,sizeof(k1));
        while(m--)
        {
            scanf("%d", &temp);
            if( temp == 0 )
            {
                scanf("%d", &x);
                k1[x]++ ;
                update(x,1,root);
            }
            else if( temp == 1 )
            {
                scanf("%d", &x);
                if( k1[x] == 0 )
                    printf("No Elment!\n");
                else
                {
                    k1[x]-- ;
                    update(x,-1,root);
                }
            }
            else
            {
                scanf("%d %d", &l, &num);
                x = query(l,0,num,root);
                if(x < num)
                    printf("Not Find!\n");
            }
        }
    }

}

hdu2852--KiKi's K-Number(线段树,求第k个数),布布扣,bubuko.com

hdu2852--KiKi's K-Number(线段树,求第k个数)

时间: 2024-08-04 12:36:19

hdu2852--KiKi's K-Number(线段树,求第k个数)的相关文章

HDU 1394- Minimum Inversion Number(线段树求逆序数)

Minimum Inversion Number Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1394 Appoint description:  System Crawler  (2015-04-13) Description The inversion number of a given number sequence a1, a

POJ2985 并查集+线段树 求第k大的数

其实这题之前做过,线段树一直不熟,所以也一直没有搞懂 本题的关键是线段树原始区间代表的是每一种容器(size不同)的数量 比如 刚开始都是互不相关的,所以1的容器有n个 2 3 4...为0个 线段树每个结点的附加信息是该区间的和 本题查找出的代码是关键 比如左右子树分别为sum 27 25 ,则第26大的容器必然在左子树上,继续递归下去,则要在该左子树找 (26-25)大的容器... 通俗讲 本题就是改变点修改 求和变化(附加信息)的情况 只是用k大转化了一下 线段树还是做的太少,近期还要加强

hdu1394--Minimum Inversion Number(线段树求逆序数,纯为练习)

Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10326 Accepted Submission(s): 6359 Problem Description The inversion number of a given number sequence a1, a2, ..., an is t

hdu 2665 可持久化线段树求区间第K大值(函数式线段树||主席树)

http://acm.hdu.edu.cn/showproblem.php?pid=2665 Problem Description Give you a sequence and ask you the kth big number of a inteval. Input The first line is the number of the test cases. For each test case, the first line contain two integer n and m (

HDU 1394 Minimum Inversion Number(线段树求逆序数)

题目地址:HDU 1394 这题可以用线段树来求逆序数. 这题的维护信息为每个数是否已经出现.每次输入后,都从该点的值到n-1进行查询,每次发现出现了一个数,由于是从该数的后面开始找的,这个数肯定是比该数大的.那就是一对逆序数,然后逆序数+1.最后求完所有的逆序数之后,剩下的就可以递推出来了.因为假如目前的第一个数是x,那当把他放到最后面的时候,少的逆序数是本来后面比他小的数的个数.多的逆序数就是放到后面后前面比他大的数的个数.因为所有数都是从0到n-1.所以比他小的数就是x,比他大的数就是n-

HDU 1394 Minimum Inversion Number(线段树求最小逆序数对)

HDU 1394 Minimum Inversion Number(线段树求最小逆序数对) ACM 题目地址:HDU 1394 Minimum Inversion Number 题意: 给一个序列由[1,N]构成,可以通过旋转把第一个移动到最后一个. 问旋转后最小的逆序数对. 分析: 注意,序列是由[1,N]构成的,我们模拟下旋转,总的逆序数对会有规律的变化. 求出初始的逆序数对再循环一遍就行了. 至于求逆序数对,我以前用归并排序解过这道题:点这里. 不过由于数据范围是5000,所以完全可以用线

HDU 1394 Minimum Inversion Number(线段树求逆序对)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1394 解题报告:给出一个序列,求出这个序列的逆序数,然后依次将第一个数移动到最后一位,求在这个过程中,逆序数最小的序列的逆序数是多少? 这题有一个好处是输入的序列保证是0 到 n-1,所以不许要离散化,还有一个好处就是在计算在这个序列中比每个数大和小的数一共有多少个的时候可以在O(1)时间计算出来,一开始我没有意识到,还傻傻的用了两层for循环来每次都计算,当然这样果断TLE了.把一个数从第一个移

hdoj 1394 Minimum Inversion Number【线段树求逆序对】

求逆序对有很多算法,这里说一下线段树求逆序对的思想. 知识点:线段树,逆序对,单点更新,成段求和 算法:线段树求逆序数的前提条件是要离散化,变成连续的点,首先建树,每个节点设置一个num值为0. 然后根据逆序对的定义,前面出现过的比当前数大的个数的和,我们需要求前面的比他大的数,其实就相当于从当前a[i]点对他后面所有出现过的数求和一次.然后把当前点的值在线段树叶子节点变为1,表示出现过,并向上更新到线段树里面.比如说样例4 2 1 5 3 首先4后面没有值,4更新为1,4--5区间更新为1,1

HDU - 1394 Minimum Inversion Number (线段树求逆序数)

Description The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that satisfy i < j and ai > aj. For a given sequence of numbers a1, a2, ..., an, if we move the first m >= 0 numbers to the end of the seq

UVA - 11983 Weird Advertisement (线段树求并面积)

Description G Weird Advertisement Renat Mullakhanov (rem), one of the most talented programmers in the world, passed away on March 11, 2011. This is very sad news for all of us. His team went to ACM ICPC World Finals - 2004, placed 4th and won gold m