Buy Tickets
Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 15086   Accepted: 7530

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 Valiin 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.

Source

 
 
题目意思:
给n个人信息,posi表示插到第i个人后面,vali表示这个人的价值,输出插入完毕后价值序列。
 
思路:
从前往后的话很难写出来,因为后面的人会影响前面人的位置。那么就从后往前,直接用例子解释吧
5
0 1
0 2
1 3
1 4
0 5
 
从后往前,先插入第5个人
插入的时候这个人前面肯定为0个人,那么序列为5 -1 -1 -1 -1(-1表示这个位置为空)
插入第4个人
插入的时候这个人前面肯定有1个人,那么给那个人留一个空位,序列为5 -1 4 -1 -1
插入第3个人
前面肯定有1个人,给那个人留下一个空位,序列为5 -1 4 3 -1
插入第2个人
前面肯定有0个人,不留空位,序列为5 2 4 3 -1
插入第1个人
只能放在最后面了   序列为5 2 4 3 1
 
这样的话,套个线段树就可以了。
 
代码:
 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <set>
using namespace std; #define N 2000005
#define ll root<<1
#define rr root<<1|1
#define mid (a[root].l+a[root].r)/2 int max(int x,int y){return x>y?x:y;}
int min(int x,int y){return x<y?x:y;}
int abs(int x,int y){return x<?-x:x;} int n;
int pos[N], val[N];
int ans[N]; struct node{
int l, r, num;
}a[N]; void build(int l,int r,int root){
a[root].l=l;
a[root].r=r;
if(l==r) {
a[root].num=;
return;
}
build(l,mid,ll);
build(mid+,r,rr);
a[root].num=a[ll].num+a[rr].num;
} void solve(int id,int root){
if(a[root].l==a[root].r){
ans[a[root].l]=val[id];
a[root].num--;
return;
}
if(a[ll].num>=pos[id]+) solve(id,ll);//前面有pos[id]+1个空位,其中一个是自己的
else {//否则往后面(右子树)插入
pos[id]-=a[ll].num;
solve(id,rr);
}
a[root].num=a[ll].num+a[rr].num;
} main()
{
int i, j, k;
while(scanf("%d",&n)==){
for(i=;i<=n;i++){
scanf("%d %d",&pos[i],&val[i]);
// pos[i]++;
}
build(,n,);
for(i=n;i>=;i--){
solve(i,);
}
printf("%d",ans[]);
for(i=;i<=n;i++) printf(" %d",ans[i]);
cout<<endl;
}
}

POJ 2828 单点更新(好题)的更多相关文章

  1. poj3321 dfs序+树状数组单点更新 好题!

    当初听郭炜老师讲时不是很懂,几个月内每次复习树状数组必看的题 树的dfs序映射在树状数组上进行单点修改,区间查询. /* 树状数组: lowbit[i] = i&-i C[i] = a[i-l ...

  2. poj 3321 单点更新 区间求和

    Apple Tree Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Java c ...

  3. poj 1195 单点更新 区间求和

    Mobile phones Time Limit: 5000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Jav ...

  4. HDU 1166 敌兵布阵(线段树单点更新)

    敌兵布阵 单点更新和区间更新还是有一些区别的,应该注意! [题目链接]敌兵布阵 [题目类型]线段树单点更新 &题意: 第一行一个整数T,表示有T组数据. 每组数据第一行一个正整数N(N< ...

  5. POJ 1804 Brainman(5种解法,好题,【暴力】,【归并排序】,【线段树单点更新】,【树状数组】,【平衡树】)

    Brainman Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 10575   Accepted: 5489 Descrip ...

  6. 线段树(单点更新) POJ 2828 Buy tickets

    题目传送门 /* 结点存储下面有几个空位 每次从根结点往下找找到该插入的位置, 同时更新每个节点的值 */ #include <cstdio> #define lson l, m, rt ...

  7. poj 2828【线段树 单点更新】

    POJ 2828 还是弱啊.思维是个好东西... 刚开始想来想去用线段树存人的话不仅超时,而且存不下...居然是存空位! sum[]数组存这个序列空位个数,然后逆序遍历.逆序好理解,毕竟最后一个人插进 ...

  8. poj 2892---Tunnel Warfare(线段树单点更新、区间合并)

    题目链接 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensiv ...

  9. HDU 1166 敌兵布阵(线段树单点更新,板子题)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

随机推荐

  1. [转载] 淘宝内部分享:怎么跳出MySQL的10个大坑(上)

    原文: http://mp.weixin.qq.com/s?__biz=MzAxNjAzMTQyMA==&mid=209773318&idx=1&sn=e9600d3db80b ...

  2. js学习笔记-编写高效、规范的js代码-Tom

    编写高效.规范的js代码: 1.变量命名空间问题,尽量使用局部变量,防止命名冲突(污染作用域中的全局变量):全局空间命名的变量可以在对应的文档域任意位置中使用window调用. 2.尽量使用单var定 ...

  3. x265

    1.编译库 https://bitbucket.org/multicoreware/x265/src/tip/build/README.txt?at=default 2.无法定位程序输入点x265_e ...

  4. C语言中随机数相关问题

    用C语言产生随机数重要用到rand函数.srand函数.及宏RAND_MAX(32767),它们均在stdlib.h中进行了声明. int rand(void);//生成一个随机数 voidsrand ...

  5. 能源项目xml文件标签释义--DefaultAdvisorAutoProxyCreator

    [Spring]AOP拦截-三种方式实现自动代理 这里的自动代理,我讲的是自动代理bean对象,其实就是在xml中让我们不用配置代理工厂,也就是不用配置class为org.springframewor ...

  6. struts2文件下载相关信息

    struts.xml文件配置: <span style="font-size:16px;"><?xml version="1.0" encod ...

  7. Python顺序集合之 List

    Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素. 比如,列出班里所有同学的名字,就可以用一个list表示: >>> ['Jen ...

  8. WMI远程访问问题解决方法

    WMI 全称为:Microsoft Windows Management Instrumentation (WMI)  按微软的介绍大致如下:      WMI 是 Microsoft 主要的针对 W ...

  9. (31)odoo中的时间

    * 在model中    from datetime import datetime, timedelta    import time        from openerp.tools impor ...

  10. getsockname和getpeername

    int getsockname(int sockfd, struct sockaddr *localaddr, socklen_t *addrlen);  // 获取与某个套接字关联的本地协议地址 i ...