Buy Tickets
Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 17326   Accepted: 8601

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
*
poj2828 BuyTickets 线段树 给你n个pos[]和val[],表示val[i]在第pos[i]个数的右边 感觉好像做过类似的题目
对于第i个数而言,它的前面必然后pos[i]个数,所以考虑从后面往前处理
(如果顺序来的话没办法解决移位)
倒序的话我们则考虑在第i个数前面留下pos[i]个空位即可,所以利用线段树找出第
pos[i]+1个空位即可 //不知为何G++会TLE hhh-2016-03-24 15:51:59
*/
#include <algorithm>
#include <cmath>
#include <queue>
#include <iostream>
#include <cstring>
#include <map>
#include <cstdio>
#include <vector>
#include <functional>
#define lson (i<<1)
#define rson ((i<<1)|1)
using namespace std;
typedef long long ll;
const int maxn = 200020;
struct node
{
int l,r;
int num;
int mid()
{
return ((l+r)>>1);
};
} tree[maxn<<2]; void update_up(int i)
{
tree[i].num = tree[lson].num + tree[rson].num;
} void build(int i,int l,int r)
{
tree[i].l = l,tree[i].r = r; if(l == r)
{
tree[i].num = 1;
return ;
}
int mid = tree[i].mid();
build(lson,l,mid);
build(rson,mid+1,r);
update_up(i);
} void update_down(int i)
{ }
int cur;
void Insert(int i,int k)
{
if(tree[i].l == tree[i].r)
{
tree[i].num = 0;
cur = tree[i].l;
return;
}
update_down(i);
int mid = tree[i].mid();
if(k <= tree[lson].num)
Insert(lson,k);
else
Insert(rson,k-tree[lson].num);
update_up(i);
} int a[maxn],val[maxn],ans[maxn]; int main()
{
int k,n,t,m,nex;
while(scanf("%d",&n) != EOF)
{
for(int i = 1; i <= n; i++)
scanf("%d%d",&a[i],&val[i]);
build(1,1,n); for(int i = n ; i > 0; i--)
{
Insert(1,a[i]+1);
//cout << cur <<endl;
ans[cur] = val[i];
}
for(int i = 1; i <= n; i++)
{
printf("%d",ans[i]);
printf("%c",i==n ? '\n':' ');
}
}
return 0;
}

  

poj2828 BuyTickets 线段树的更多相关文章

  1. poj2828(线段树查找序列第k小的值)

    题目链接:https://vjudge.net/problem/POJ-2828 题意:有n个人,依次给出这n个人进入队列时前面有多少人p[i],和它的权值v[i],求最终队列的权值序列. 思路:基本 ...

  2. 【POJ2828】Buy Tickets(线段树)

    题意:有一个输入序列,每次操作要把b[i]插入到第a[i]个,在第a[i]个后面的要后移,问最后序列. n<=200000 思路:顺序来只能用splay维护 考虑倒序,对于插入到第K个位置,在线 ...

  3. 【poj2828】Buy Tickets 线段树 插队问题

    [poj2828]Buy Tickets Description Railway tickets were difficult to buy around the Lunar New Year in ...

  4. [poj2828] Buy Tickets (线段树)

    线段树 Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must ...

  5. 【插队问题-线段树-思维巧妙】【poj2828】Buy Tickets

    可耻的看了题解 巧妙的思维 逆序插入,pos 代表的意义为前面要有pos个空格才OK: 证明:仔细思考一下就觉得是正确的,但是要想到这种方式还是要很聪明,空格是前面的几个数字所形成的,所以要特地留出来 ...

  6. POJ2828 Buy Tickets 【线段树】+【单点更新】+【逆序】

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 12296   Accepted: 6071 Desc ...

  7. POJ2828线段树单点更新——逆序更新

    Description 输入n个有序对< pi, vi >,pi表示在第pi个位置后面插入一个值为vi的人,并且pi是不降的.输出最终得到的v的序列 Input 多组用例,每组用例第一行为 ...

  8. POJ2828 Buy Tickets(线段树之插队问题)

    飞翔 问题是这样的:现在有n个人要买票,但是天黑可以随便插队.依次给出将要买票的n个人的数据信息.包含两项:pos,当前第i号人来了之后他肯定要插入到pos这个位置,如果当前pos无人,那最好了,直接 ...

  9. poj-2828 Buy Tickets(线段树,排队问题,逆向思维)

    题目地址:POJ 2828 Buy Tickets Description Railway tickets were difficult to buy around the Lunar New Yea ...

随机推荐

  1. 20145237 实验五《Java网络编程》

    20145237 实验五<Java网络编程> 一.实验内容 •1.运行下载的TCP代码,结对进行,一人服务器,一人客户端: •2.利用加解密代码包,编译运行代码,一人加密,一人解密: •3 ...

  2. bzoj千题计划214:bzoj3589: 动态树

    http://www.lydsy.com/JudgeOnline/problem.php?id=3589 树链剖分 用线段数维护扫描线的方式来写,标记只打不下传 #include<cstdio& ...

  3. V7000存储数据恢复_底层结构原理拆解及Mdisk磁盘掉线数据恢复方法

    Storwize V7000(也就是我们常说的V7000)是新推出的一款中端存储系统,这款系统的定位虽然在中端,但是Storwize V7000提供有存储管理功能,这一功能以前只有高端存储才拥有(例如 ...

  4. 【Fungus入门】10分钟快速构建Unity中的万能对话系统 / 叙事系统 / 剧情系统

    我真的很久没有写过一个完整的攻略了(笑),咸鱼了很久之后还是想来写一个好玩的.这次主要是梳理一下Unity的小众插件Fungus的核心功能,并且快速掌握其使用方法. 官方文档:http://fungu ...

  5. js判断IE浏览器版本(IE8及以下)

    var DEFAULT_VERSION = 8.0; var ua = navigator.userAgent.toLowerCase(); var isIE = ua.indexOf("m ...

  6. anguar使用指令写选项卡

    今天,我们来学习一下angular中怎么使用指令来实现两个选项卡的问题. 首先,要先引入jQuery文件与angularjs文件. <!DOCTYPE html><html lang ...

  7. java字符串类型常量拼接与变量拼接的区别

    前言 首先看下下面代码结果是什么? package cn.demo_01; public class StringDemo02 { public static void main(String[] a ...

  8. Menubutton按钮弹出菜单

    #按钮弹出菜单 from tkinter import * root =Tk() def callback(): print('我被调用了') m = Menubutton(root,text = ' ...

  9. vue-router动态路由 刷新页面 静态资源没有加载的原因

    在做项目的时候,发现刷新页面的时候,静态路由没有加载,度娘了一圈,终于解决了. vue-router使用history模式+使用嵌套路由: 访问路由和嵌套路由页面,显示正常,但是刷新页面的时候,嵌套路 ...

  10. Effective Java 第三版——38. 使用接口模拟可扩展的枚举

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...