【HDU3436】 Queue-jumpers (Splay tree)

本文解析了HDU3436 Queue-jumpers问题,介绍了使用Splay Tree解决队列中的人向前移动、查询位置及排名的操作,并提供了完整的代码实现。

HDU 3436 Queue-jumpers (Splay tree)http://acm.hdu.edu.cn/showproblem.php?pid=3436


Queue-jumpers

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2521    Accepted Submission(s): 662


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
 

Author
wzc1989
 

Source


#include <cstdio>
#include <cstring>
#include <algorithm>

const int MAXD = 100010;
using namespace std;

int node, tx[MAXD], begin[MAXD], end[MAXD], nodep[MAXD];
int num[2 * MAXD], size[2 * MAXD], left[2 * MAXD], right[2 * MAXD], pre[2 * MAXD], key[2 * MAXD];
char b[MAXD][10];
int ob[MAXD], T, N, Q;

inline void update(int cur)
{
    size[cur] = size[left[cur]] + size[right[cur]] + num[cur];
}

inline void newnode(int &cur, int k)
{
    cur = ++ node;
    size[cur] = num[cur] = end[k] - begin[k] + 1;
    key[cur] = k;
    nodep[k] = cur;
    left[cur] = right[cur] = 0;
}

void build(int &cur, int x, int y, int p)
{
    int mid = (x + y) / 2;
    newnode(cur, mid);
    pre[cur] = p;
    if (x == y) return ;
    if (x < mid) build(left[cur], x, mid - 1, cur);
    if (mid < y) build(right[cur], mid + 1, y, cur);
    update(cur);
}

inline void init(void)
{
    int k;
    scanf("%d%d", &N, &Q);
    k = 0;
    tx[k ++] = 0;
    for (int i = 0; i < Q; i ++)
    {
        scanf("%s%d", b[i], &ob[i]);
        if (b[i][0] == 'T' || b[i][0] == 'Q') tx[k ++] = ob[i];
    }
    tx[k ++] = N;
    sort(tx, tx + k);
    N = 0;
    for (int i = 1; i < k; i ++)
        if (tx[i] != tx[i - 1])
        {
            if (tx[i] - tx[i - 1] > 1)
            {
                begin[N] = tx[i - 1] + 1, end[N] = tx[i] - 1;
                ++ N;
            }
            begin[N] = end[N] = tx[i];
            ++ N;
        }
    T = node = left[0] = right[0] = size[0] = num[0] = 0;
    build(T, 0, N - 1, 0);
}

inline void leftrotate(int x)
{
    int y = right[x], p = pre[x];
    right[x] = left[y];
    if (right[x]) pre[right[x]] = x;
    left[y] = x;
    pre[x] = y;
    pre[y] = p;
    if (p == 0) T = y;
    else right[p] == x ? right[p] = y : left[p] = y;
    update(x);
}

inline void rightrotate(int x)
{
    int y = left[x], p = pre[x];
    left[x] = right[y];
    if (left[x]) pre[left[x]] = x;
    right[y] = x;
    pre[x] = y;
    pre[y] = p;
    if (p == 0) T = y;
    else right[p] == x ? right[p] = y : left[p] = y;
    update(x);
}

inline void splay(int x, int goal)
{
    int y, z;
    for ( ; ; )
    {
        if ((y = pre[x]) == goal) break;
        if ((z = pre[y]) == goal) right[y] == x ? leftrotate(y) : rightrotate(y);
        else
        {
            if (right[z] == y)
            {
                if (right[y] == x) leftrotate(z), leftrotate(y);
                else rightrotate(y), leftrotate(z);
            }
            else
            {
                if (left[y] == x) rightrotate(z), rightrotate(y);
                else leftrotate(y), rightrotate(z);
            }
        }
    }
    update(x);
}

inline int BS(int x)
{
    int max, mid, min;
    min = 0, max = N;
    for (;;)
    {
        mid = (max + min) / 2;
        if (mid == min) break;
        if (begin[mid] <= x) min = mid;
        else max = mid;
    }
    return mid;
}

int Delete(int &cur, int p)
{
    int k;
    if (cur == T || right[cur] == 0)
    {
        if (cur == T)
        {
            k = Delete(left[cur], cur);
            left[k] = left[T], right[k] = right[T];
            if (left[k]) pre[left[k]] = k;
            if (right[k]) pre[right[k]] = k;
            T = k;
            pre[T] = 0;
            update(T);
        }
        else
        {
            k = cur;
            if (left[k]) pre[left[k]] = p;
            cur = left[k];
        }
        return k;
    }
    else
    {
        k = Delete(right[cur], cur);
        update(cur);
        return k;
    }
}

void Insert(int &cur, int k, int p)
{
    if (cur == 0)
    {
        newnode(cur, k);
        pre[cur] = p;
        return ;
    }
    Insert(left[cur], k, cur);
    update(cur);
}

inline void Top(int x)
{
    int k, cur;
    k = BS(x);
    cur = nodep[k];
    splay(cur, 0);
    if (left[T] == 0 || right[T] == 0)
    {
        T = left[T] + right[T];
        pre[T] = 0;
    }
    else Delete(T, 0);
    Insert(T, k, 0);
    splay(node, 0);
}

inline void Query(int x)
{
    int k, cur;
    k = BS(x);
    cur = nodep[k];
    splay(cur, 0);
    printf("%d\n", size[left[cur]] + 1);
}

inline int Search(int cur, int x)
{
    int ls = left[cur], rs = right[cur], k = key[cur];
    if (x <= size[ls]) return Search(left[cur], x);
    else if (x <= size[ls] + num[cur]) return begin[k] + (x - size[ls]) - 1;
    else return Search(right[cur], x - size[ls] - num[cur]);
}

inline void Rank(int x)
{
    printf("%d\n", Search(T, x));
}

inline void solve(void)
{
    for (int i = 0; i < Q; i ++)
    {
        if (b[i][0] == 'T') Top(ob[i]);
        else if (b[i][0] == 'Q') Query(ob[i]);
        else Rank(ob[i]);
    }
}

int main(void)
{
    int t;
    scanf("%d", &t);
    for (int i = 0; i < t; i ++)
    {
        init();
        printf("Case %d:\n", i + 1);
        solve();
    }
    return 0;
}

3种操作:

RANK:求第K位

TOP:将某个人移至队首,对中间区间没有影响

QUERY:求某个人的位置

N≤10^8,要离散化。

离散化之后就是Splay的基本操作:

TOP:将目标点旋转至根部,删除后插入到队首

RANK:通过size查找

QUERY:把该点旋转至根部,左子树的大小+1


内容概要:本文为《科技类企业品牌传播白皮书》,系统阐述了新闻媒体发稿、自媒体博主种草与短视频矩阵覆盖三大核心传播策略,并结合“传声港”平台的AI工具与资源整合能力,提出适配科技企业的品牌传播解决方案。文章深入分析科技企业传播的特殊性,包括受众圈层化、技术复杂性与传播通俗性的矛盾、产品生命周期影响及2024-2025年传播新趋势,强调从“技术输出”向“价值引领”的战略升级。针对三种传播方式,分别从适用场景、操作流程、效果评估、成本效益、风险防控等方面提供详尽指南,并通过平台AI能力实现资源智能匹配、内容精准投放与全链路效果追踪,最终构建“信任—种草—曝光”三位一体的传播闭环。; 适合人群:科技类企业品牌与市场负责人、公关传播从业者、数字营销管理者及初创科技公司创始人;具备一定品牌传播基础,关注效果可量化与AI工具赋能的专业人士。; 使用场景及目标:①制定科技产品全生命周期的品牌传播策略;②优化媒体发稿、KOL合作与短视频运营的资源配置与ROI;③借助AI平台实现传播内容的精准触达、效果监测与风险控制;④提升品牌在技术可信度、用户信任与市场影响力方面的综合竞争力。; 阅读建议:建议结合传声港平台的实际工具模块(如AI选媒、达人匹配、数据驾驶舱)进行对照阅读,重点关注各阶段的标准化流程与数据指标基准,将理论策略与平台实操深度融合,推动品牌传播从经验驱动转向数据与工具双驱动。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值