L3-002 特殊堆栈

简单线段树求区间第k大数

#include <iostream>

using namespace std;

#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
const int MAXN=111111;

int stree[MAXN<<2];

void pushup(int rt)
{
    stree[rt]=stree[rt<<1]+stree[rt<<1|1];
}
void build(int l,int r,int rt)
{
    if(l==r)
    {
        stree[rt]=0;
        return;
    }
    int mid=(l+r)>>1;
    build(lson);
    build(rson);
    pushup(rt);
}

void update(int c,int c1,int l,int r,int rt)
{
    if(l==r)
    {
        stree[rt]+=c1;
        return;
    }
    int mid=(l+r)>>1;
    if(c<=mid) update(c,c1,lson);
    if(c>mid) update(c,c1,rson);
    pushup(rt);
}

int query(int c,int l,int r,int rt)
{
    if(l==r)
    {
        return l;
    }
    int mid=(l+r)>>1;
    int ans=0;
    if(c<=stree[rt<<1]) ans=query(c,lson);
    else  ans=query(c-stree[rt<<1],rson);
    return ans;
}
int num[MAXN];
int main()
{
    int n;
    scanf("%d",&n);
    build(1,MAXN,1);
    int cont=0;
    while(n--)
    {
        char str[20];
        scanf("%s",str);
        if(str[1]=='u')
        {
            int k;
            scanf("%d",&k);
            update(k,1,1,MAXN,1);
            num[cont++]=k;
        }
        else if(str[1]=='o')
        {
            if(!cont)
                printf("Invalid\n");
            else 
            {
                printf("%d\n",num[cont-1]);
                update(num[cont-1],-1,1,MAXN,1);
                cont--;
            }
        }
        else if(str[1]=='e')
        {
            int num1;
            if(cont&1)
                num1=(cont+1)/2;
            else num1=cont/2;
            if(!num1)
                printf("Invalid\n");
            else
            {
                int ans=query(num1,1,MAXN,1);
                if(!ans) printf("Invalid\n");
                else printf("%d\n",ans);
            }
        }
    }
    return 0;
}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值