hdu 3436 Queue-jumpers 树状数组求第k大数 (不断将数调到第一个位置)

该博客介绍了一种使用树状数组求解HDU 3436问题的方法,即在队列中找到第k大数。通过压缩数据、更新树状数组以及查询操作,实现了对队列中元素的高效管理。博客提供了完整的C++代码实现,并展示了如何处理"Top"(将元素调整到队列头部)、"Rank"(查询第k大数的原始位置)和"Query"(查询当前位置的排名)等操作。
摘要由CSDN通过智能技术生成
 
Problem Description
Ponyo and Garfield are waiting outside the box-office for their favorite movie. Because queuing is so boring, that they want to play a game to kill the time. The game is called “Queue-jumpers”. Suppose that there are N people numbered from 1 to N stand in a line initially. Each time you should simulate one of the following operations:
1.  Top x :Take person x to the front of the queue
2.  Query x: calculate the current position of person x
3.  Rank x: calculate the current person at position x
Where x is in [1, N].
Ponyo is so clever that she plays the game very well while Garfield has no idea. Garfield is now turning to you for help.
 


 

Input
In the first line there is an integer T, indicates the number of test cases.(T<=50)
In each case, the first line contains two integers N(1<=N<=10^8), Q(1<=Q<=10^5). Then there are Q lines, each line contain an operation as said above.
 


 

Output
For each test case, output “Case d:“ at first line where d is the case number counted from one, then for each “Query x” operation ,output the current position of person x at a line, for each “Rank x” operation, output the current person at position x at a line.
 


 

Sample Input
  
  
3 9 5 Top 1 Rank 3 Top 7 Rank 6 Rank 8 6 2 Top 4 Top 5 7 4 Top 5 Top 2 Query 1 Rank 6
 


 

Sample Output
  
  
Case 1: 3 5 8 Case 2: Case 3: 3 6

 

 //

http://www.cppblog.com/Yuan/archive/2010/08/18/123871.html

 

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;

const int MAXN = 100010;

struct OP
{
    char cmd[10];
    int x;
}op[MAXN];

int N;
int num[MAXN];//for compress
int pos[MAXN*2],peo[MAXN];//pos[i] = who     peo[i] = where
int C[2*MAXN];//,A[2*MAXN];//    A[i] = number of people at pos[i]

inline int lowbit(int x)
{
    return x&(-x);
}
inline void add(int p,int x)
{
    while(p<=N)
    {
        C[p]+=x;
        p+=lowbit(p);
    }
}
inline int sum(int p)
{
    int ans = 0;
    while(p>0)
    {
        ans+=C[p];
        p-=lowbit(p);
    }
    return ans;
}
//下面两个函数都行
int findK(int K)//find the first one >=K
{
    int pos = 0,cnt = 0;
    for(int i=17;i>=0;i--)
    {
        pos+=(1<<i);
        if(pos>=N||cnt+C[pos]>=K)pos-=(1<<i);
        else cnt+=C[pos];
    }
    return pos+1;
}
int findK(int n,int K)//find the first one >=K
{
    int l=0,r=n;
    while(l<r)
    {
        int mid=(l+r)>>1;
        if(sum(mid)>=K) r=mid;
        else l=mid+1;
    }
    return l;
}
int main()
{
    //freopen("in","r",stdin);
    int T,t=1,n,q;
    for(scanf("%d",&T);T--;)
    {
        printf("Case %d:\n",t++);
        scanf("%d%d",&n,&q);
        num[0]=0;
        for(int i=0;i<q;i++)
        {
            scanf("%s%d",op[i].cmd,&op[i].x);
            num[i+1]=op[i].x;
        }
        sort(num+1,num+1+q);
        n=unique(num+1,num+1+q)-(num+1);
        N = q+n;
        fill(C,C+N+1,0);
        //fill(A,A+N+1,0);
        for(int i=1;i<=n;i++)
        {
           // A[q+i]=num[i]-num[i-1];
            add(q+i,num[i]-num[i-1]);
            pos[q+i]=num[i];
            peo[i]=q+i;
        }
        int top=q;
        for(int i=0;i<q;i++)
        {
            if(strcmp(op[i].cmd,"Top")==0)
            {
                int x=lower_bound(num+1,num+1+n,op[i].x)-num;
                add(peo[x],-1);
                pos[peo[x]]--;
                peo[x]=top;
                add(peo[x],+1);
                pos[top]=op[i].x;
                top--;
            }
            if(strcmp(op[i].cmd,"Rank")==0)
            {
                int K=op[i].x;
                int p=findK(N,K);//
                int sp=sum(p);
                printf("%d\n",pos[p]-(sp-K));
            }
            if(strcmp(op[i].cmd,"Query")==0)
            {
                int x=lower_bound(num+1,num+1+n,op[i].x)-num;
                printf("%d\n",sum(peo[x]));
            }
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值