题目链接: 传送门

Buy Tickets

Time Limit: 4000MS     Memory Limit: 65536K

Description

Railway tickets were difficult to buy around the Lunar New Year in China, so we must get up early and join a long queue…
The Lunar New Year was approaching, but unluckily the Little Cat still had schedules going here and there. Now, he had to travel by train to Mianyang, Sichuan Province for the winter camp selection of the national team of Olympiad in Informatics.
It was one o’clock a.m. and dark outside. Chill wind from the northwest did not scare off the people in the queue. The cold night gave the Little Cat a shiver. Why not find a problem to think about? That was none the less better than freezing to death!
People kept jumping the queue. Since it was too dark around, such moves would not be discovered even by the people adjacent to the queue-jumpers. “If every person in the queue is assigned an integral value and all the information about those who have jumped the queue and where they stand after queue-jumping is given, can I find out the final order of people in the queue?” Thought the Little Cat.

Input

There will be several test cases in the input. Each test case consists of N + 1 lines where N (1 ≤ N ≤ 200,000) is given in the first line of the test case. The next N lines contain the pairs of values Posi and Vali in the increasing order of i (1 ≤ i ≤ N). For each i, the ranges and meanings of Posi and Vali are as follows:

  • Posi ∈ [0, i − 1] — The i-th person came to the queue and stood right behind the Posi-th person in the queue. The booking office was considered the 0th person and the person at the front of the queue was considered the first person in the queue.
  • Vali ∈ [0, 32767] — The i-th person was assigned the value Vali.
    There no blank lines between test cases. Proceed to the end of input.

Output

For each test cases, output a single line of space-separated integers which are the values of people in the order they stand in the queue.

Sample Input

4
0 77
1 51
1 33
2 69
4
0 20523
1 19243
1 3890
0 31492

Sample Output

77 33 69 51
31492 20523 3890 19243

HINT

The figure below shows how the Little Cat found out the final order of people in the queue described in the first test case of the sample input.

解题思路:

一开始没什么思路,以为就是普通的排队插队问题,准备用数组模拟过程,写到最后发现并非如此,因为每次有人插队进来那么这个人以及他后面的人的序号就会改变,不能实现简单的模拟。想过往线段树靠,但是却不知道怎么靠,百度了题解才恍然大悟。
鉴于插队处于动态的关系,所以逆向思维,考虑总是先确定后面的人的位置,这样题目给出的位置pos就变成了“寻找一个位置,使得这个位置左边的空位置数目为pos”,以第一个样例为例:
把样例倒过来之后是这样的:
2 69
1 33
1 51
0 77
先考虑69号,它的pos是2,说明它插入的时候需要队伍前面有2个人,这样,它就在2号位置上了(从0开始);

再考虑33号,它的pos是1,说明它插入的时候需要队伍前面有1个人,这样,它就在1号位置上了;

再考虑51号,它的pos是1,说明它插入的时候需要队伍前面有1个人,这样,它就在3号位置上了;

再考虑77号,它的pos是0,说明它插入的时候需要队伍前面有0个人,这样,它就在0号位置上了。

所以开一个empty数组保存表示[l,r]区间内的空位置的数目。更新到底是往左子树更新还是右子树更新的原则是:当前需要的空位置数x如果小于[l,mid]区间的空位置数(不包含等于),就往左子树搜(rt<<1),否则往右子树搜(往右子树搜的时候要注意参数x应该减去左子树的空位置数,也就是变为x-empty[rt<<1])。

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
const int maxn = 200005;
int empty[maxn<<2],pos[maxn],val[maxn],ans[maxn];

void PushUp(int rt)
{
    empty[rt] = empty[rt<<1] + empty[rt<<1|1];
}

void build(int l,int r,int rt)
{
    if (l == r)
    {
        empty[rt] = 1;
        return;
    }
    int m = (l + r) >> 1;
    build(lson);
    build(rson);
    PushUp(rt);
}

void upd(int p,int index,int l,int r,int rt)
{
    if (l == r)
    {
        empty[rt]--;
        ans[l] = val[index];
        return ;
    }
    int m = (l + r) >> 1;
    if (empty[rt<<1] > p)   upd(p,index,lson);
    else    upd(p - empty[rt<<1],index,rson);
    PushUp(rt);
}

int main()
{
    int N;
    while (~scanf("%d",&N))
    {
        build(0,N - 1,1);
        for (int i = 0;i < N;i++)
        {
            scanf("%d%d",&pos[i],&val[i]);
        }
        for (int i = N - 1;i >= 0;i--)
        {
            upd(pos[i],i,0,N - 1,1);
        }
        for (int i = 0;i < N;i++)
        {
            i?printf(" %d",ans[i]):printf("%d",ans[i]);
        }
        printf("\n");
    }
    return 0;
}
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = 200005;
int N,c[maxn],pos[maxn],val[maxn],ans[maxn];

void upd(int i,int v)
{
    while (i <= N)
    {
        c[i] += v;
        i += i & -i;
    }
}

int find_kth(int k)
{
    int ans = 0,cnt = 0;
    for (int i = 20;i >= 0;i--)
    {
        ans += (1 << i);
        if (ans > N || cnt + c[ans] >= k)   ans -= (1 << i);
        else    cnt += c[ans];
    }
    return ans + 1;
}

int main()
{
    while (~scanf("%d",&N))
    {
        memset(c,0,sizeof(c));
        for (int i = 1;i <= N;i++)
        {
            upd(i,1);
            scanf("%d%d",&pos[i],&val[i]);
        }
        for (int i = N;i;i--)
        {
            int p = find_kth(pos[i] + 1);
            ans[p] = i;
            upd(p,-1);
        }
        for (int i = 1;i <= N;i++)
        {
            i == 1?printf("%d",val[ans[i]]):printf(" %d",val[ans[i]]);
        }
        printf("\n");
    }
    return 0;
}

POJ 2828 Buy Tickets(线段树 树状数组/单点更新)的更多相关文章

  1. poj 2828 Buy Tickets (线段树(排队插入后输出序列))

    http://poj.org/problem?id=2828 Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissio ...

  2. POJ 2828 Buy Tickets (线段树 or 树状数组+二分)

    题目链接:http://poj.org/problem?id=2828 题意就是给你n个人,然后每个人按顺序插队,问你最终的顺序是怎么样的. 反过来做就很容易了,从最后一个人开始推,最后一个人位置很容 ...

  3. POJ 2828 Buy Tickets 线段树 倒序插入 节点空位预留(思路巧妙)

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 19725   Accepted: 9756 Desc ...

  4. poj 2828 Buy Tickets (线段树)

    题目:http://poj.org/problem?id=2828 题意:有n个人插队,给定插队的先后顺序和插在哪个位置还有每个人的val,求插队结束后队伍各位置的val. 线段树里比较简单的题目了, ...

  5. POJ 2828 Buy Tickets | 线段树的喵用

    题意: 给你n次插队操作,每次两个数,pos,w,意为在pos后插入一个权值为w的数; 最后输出1~n的权值 题解: 首先可以发现,最后一次插入的位置是准确的位置 所以这个就变成了若干个子问题, 所以 ...

  6. POJ 2828 Buy Tickets(线段树&#183;插队)

    题意  n个人排队  每一个人都有个属性值  依次输入n个pos[i]  val[i]  表示第i个人直接插到当前第pos[i]个人后面  他的属性值为val[i]  要求最后依次输出队中各个人的属性 ...

  7. POJ 2828 Buy Tickets(线段树单点)

    https://vjudge.net/problem/POJ-2828 题目意思:有n个数,进行n次操作,每次操作有两个数pos, ans.pos的意思是把ans放到第pos 位置的后面,pos后面的 ...

  8. poj 2828 Buy Tickets(树状数组 | 线段树)

    题目链接:poj 2828 Buy Tickets 题目大意:给定N,表示有个人,给定每一个人站入的位置,以及这个人的权值,如今按队列的顺序输出每一个人的权值. 解题思路:第K大元素,非常巧妙,将人入 ...

  9. POJ 2828 Buy Tickets(排队问题,线段树应用)

    POJ 2828 Buy Tickets(排队问题,线段树应用) ACM 题目地址:POJ 2828 Buy Tickets 题意:  排队买票时候插队.  给出一些数对,分别代表某个人的想要插入的位 ...

  10. poj 2828 Buy Tickets 【线段树点更新】

    题目:id=2828" target="_blank">poj 2828 Buy Tickets 题意:有n个人排队,每一个人有一个价值和要插的位置,然后当要插的位 ...

随机推荐

  1. Google Zxing 二维码生成与解析

    生成二维码的开源项目可谓是琳琅满目,SwetakeQRCode.BarCode4j.Zxing...... 前端有JQuery-qrcode,同样能实现生成二维码. 选择Zxing的原因可能是对 Go ...

  2. 安装laravel5.1项目命令

    作为程序员还有什么比命令行执行效率还要快的呢,哈哈... composer create-project laravel/laravel your-project-name --prefer-dist ...

  3. 化茧成蝶,开源NetWorkSocket通讯组件

    前言 前后历时三年,期间大量参考.Net Framework和Asp.net MVC源代码,写写删删再重构,组件如今更新到V1.5.x了.从原来的丑小鸭,变成今天拥有稳定和强大的tcp协议支持基础层, ...

  4. 部署到IIS上的网站打开时总是显示无法找到资源解决方案

    1.首先修改项目目录的访问权限:右键->属性->安全里面找到组名或用户名 ->编辑->添加一个用户取名everyOne并设置可以修改即可 2.然后在IIS下面,选中你的mvc项 ...

  5. HEU KMS Activator v11.1.0 Windows激活

    HEU KMS Activator基于MDL论坛的“KMS Server Emulator”,是一款KMS激活工具,为“知彼而知己”原创工具.主要适用于Windows以及Office的VL版本,无需联 ...

  6. 【jQuery EasyUI系列】创建CRUD数据网格

    在上一篇中我们使用对话框组件创建了CRUD应用创建和编辑用户信息.本篇我们来创建一个CRUD数据网格DataGrid 步骤1,在HTML标签中定义数据网格(DataGrid) <table id ...

  7. 使用jsp/servlet简单实现文件上传与下载

    使用JSP/Servlet简单实现文件上传与下载    通过学习黑马jsp教学视频,我学会了使用jsp与servlet简单地实现web的文件的上传与下载,首先感谢黑马.好了,下面来简单了解如何通过使用 ...

  8. 【日常笔记】datatables表格数据渲染

    现在有很多表格渲染方式 这里只是记录怎么使用datatables渲染数据 使用datatables可以更方便的来渲染数据 [中文api]http://datatables.club/index.htm ...

  9. Swift开发小技巧--自定义Log

    Swift中的自定义Log OC中有宏的定义,可以定义自己的Log,但是Swif中没有宏的定义,想要实现类似OC中的自定义Log,必须实现以下操作 1.在AppDelegate.swift文件中定义一 ...

  10. 【POJ 2653】Pick-up sticks 判断线段相交

    一定要注意位运算的优先级!!!我被这个卡了好久 判断线段相交模板题. 叉积,点积,规范相交,非规范相交的简单模板 用了“链表”优化之后还是$O(n^2)$的暴力,可是为什么能过$10^5$的数据? # ...