hdu 2852 KiKi's K-Number 线段树

来源:互联网 发布:怎么查淘宝店铺地址 编辑:程序博客网 时间:2024/04/28 16:23

三种操作,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,那么就要向下深入,一直深入到叶子节点,满足条件的,最左的叶子节点就是我们要求的值。(线段树,从左向右查找,一定可以找到第一个)


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
50 51 20 62 3 22 8 170 20 20 42 1 12 1 22 1 32 1 4
 

Sample Output
No Elment!6Not Find!224Not Find!
#include<cstdio>#include<iostream>#include<algorithm>#include<string>using namespace std;int tree[404000];int n=101000;void build(int left ,int right ,int root){    tree[root]=0;    if(left==right)        return;    int mid=(left+right)>>1;    build(left,mid,root<<1);    build(mid+1,right,root<<1|1);}void update(int d,int left,int right,int root){    int mid=(left+right)>>1;    if(left==right)    {        tree[root]++;        return ;    }    if(d<=mid)        update(d,left,mid,root<<1);    else        update(d,mid+1,right,root<<1|1);    tree[root]=tree[root<<1]+tree[root<<1|1];}void Delete(int d,int left,int right,int root){    if(left==right)    {        if(tree[root]==0)            printf("No Elment!\n");        else            tree[root]--;        return ;    }    int mid=(left+right)>>1;    if(d<=mid)        Delete(d,left,mid,root<<1);    else        Delete(d,mid+1,right,root<<1|1);    tree[root]=tree[root<<1]+tree[root<<1|1];}int qian(int a,int d,int left,int right,int root){    if(left>=a&&d>=right)    {        return tree[root];    }    int mid=(left+right)>>1;    int ret=0;    if(a<=mid)        ret+=qian(a,d,left,mid,root<<1);    if(d>mid)        ret+=qian(a,d,mid+1,right,root<<1|1);    return ret;}void query(int d,int left,int right,int root){    if(left==right)    {        printf("%d\n",left);        return ;    }    int mid=(left+right)>>1;    if(d<=tree[root<<1])        query(d,left,mid,root<<1);        else            query(d-tree[root<<1],mid+1,right,root<<1|1);}int main(){    int m;    int a,b,k;    while(~scanf("%d",&m))    {        build(1,n,1);        while(m--)        {            scanf("%d",&a);            if(a==0)            {                scanf("%d",&b);                update(b,1,n,1);            }            if(a==1)            {                scanf("%d",&b);                Delete(b,1,n,1);            }            if(a==2)            {                scanf("%d %d",&b,&k);                b=qian(1,b,1,n,1);                k+=b;                if(k>tree[1])                    printf("Not Find!\n");                else                   query(k,1,n,1);            }        }    }    return 0;}

今天做这道题目的第二遍还是感觉query这个地方卡住了。。。。。
0 0