hdu4006The kth great number【线段树第k大】

The kth great number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 9072    Accepted Submission(s): 3592


Problem Description
Xiao Ming and Xiao Bao are playing a simple Numbers game. In a round Xiao Ming can choose to write down a number, or ask Xiao Bao what the kth great number is. Because the number written by Xiao Ming is too much, Xiao Bao is feeling giddy. Now, try to help Xiao Bao.
 

Input
There are several test cases. For each test case, the first line of input contains two positive integer n, k. Then n lines follow. If Xiao Ming choose to write down a number, there will be an " I" followed by a number that Xiao Ming will write down. If Xiao Ming choose to ask Xiao Bao, there will be a "Q", then you need to output the kth great number.
 

Output
The output consists of one integer representing the largest number of islands that all lie on one line.
 

Sample Input
 
   
8 3 I 1 I 2 I 3 Q I 5 Q I 4 Q
 

Sample Output
 
   
1 2 3
Hint
Xiao Ming won't ask Xiao Bao the kth great number when the number of the written number is smaller than k. (1=
 

Source
The 36th ACM/ICPC Asia Regional Dalian Site —— Online Contest
 

Recommend
lcy   |   We have carefully selected several similar problems for you:   4003  4007  4004  4008  4005 
 

比上一个题弱了很多啊

/******************
hdu4006
2016.3.8
265MS	27104K	1383B	C++
******************/
#include 
#include
#include
using namespace std;
#define maxn 1000000
struct node
{
    int l,r,tot;
}num[maxn*8];
void build(int rt,int l,int r)
{
    num[rt].l=l;num[rt].r=r;
    if(l==r)
    {
        num[rt].tot=0;
        return;
    }
    int mid=(l+r)/2;
    build(rt<<1,l,mid);
    build(rt<<1|1,mid+1,r);
    num[rt].tot=0;
}
void update(int rt,int pos)
{
    if(num[rt].l==num[rt].r)
    {
        if(num[rt].l==pos) num[rt].tot++;
        return;
    }
    int mid=(num[rt].l+num[rt].r)/2;
    if(pos<=mid) update(rt<<1,pos);
    else update(rt<<1|1,pos);
    num[rt].tot=num[rt<<1].tot+num[rt<<1|1].tot;
}
int query(int rt,int k)
{
    if(k>num[rt<<1|1].tot)
    {
        if(num[rt].l==num[rt].r)
        {
            return num[rt].l;
        }
        query(rt<<1,k-num[rt<<1|1].tot);
    }
    else query(rt<<1|1,k);
}
int main()
{
   // freopen("cin.txt","r",stdin);
    int n,k,a;
    char st[3];
    while(~scanf("%d%d",&n,&k))
    {
        build(1,1,maxn);
        while(n--)
        {
            scanf("%s",st);
            if(st[0]=='I')
            {
                scanf("%d",&a);
                update(1,a);
            }
            else printf("%d\n",query(1,k));
        }
    }
    return 0;
}


你可能感兴趣的:(—数据结构,———线段树,树状数组)