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=<k<=n<=1000000).
 

Source
 

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 <iostream>
#include<cstdio>
#include<cstring>
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;
}


全部评论

相关推荐

我的offer呢😡:这不才9月吗,26到明年毕业前能一直找啊,能拿下提前批,转正的,offer打牌的都是有两把刷子的,为什么非要跟他们比。如果别人是9本硕+金牌+好几段大厂实习呢?如果别人是双非通天代呢?如果别人是速通哥呢?,做好自己就行了,我们做不到他们一样提前杀死比赛,但晚点到终点也没啥关系吧
双非应该如何逆袭?
点赞 评论 收藏
分享
09-09 06:44
已编辑
浙江大学 深度学习
无敌王八拳:貌似10月线下面?
投递华为技术有限公司等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务