Buy Tickets
Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 18561   Accepted: 9209

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 ≤ iN). 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个人要排队,第 i 个人来的时候插队的位置是他的前面有 k 个人,其价值是v[i].问最终排队的顺序,按照人的价值输出。
题解:想到线段树这题就不难了,逆序插入人数,假设当前的人的前面有 k 个人,那么就到线段树里面去找第k+1个没有被占的位置,画个图模拟一下这个过程就行了,然后通过一个数组记录下线段树每个叶子结点的value,输出即可。
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<iostream>
#define N 250005
using namespace std; int tree[N<<]; ///当前结点剩余的容量
int a[N],v[N],ans[N];
void pushup(int idx){
tree[idx] = tree[idx<<]+tree[idx<<|];
}
void build(int l,int r,int idx){
if(l==r){
tree[idx] = ;
return;
}
int mid = (l+r)>>;
build(l,mid,idx<<);
build(mid+,r,idx<<|);
pushup(idx);
}
void update(int num,int v,int l,int r,int idx){
if(l==r){
tree[idx]= ;
ans[l] = v;
return;
}
int mid = (l+r)>>;
if(tree[idx<<]>=num){ ///左子树还有位置可以插
update(num,v,l,mid,idx<<);
}else{ ///往右子树插
update(num-tree[idx<<],v,mid+,r,idx<<|);
}
pushup(idx);
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF){
build(,n,);
for(int i=;i<=n;i++){
scanf("%d%d",&a[i],&v[i]);
a[i]++;
}
for(int i=n;i>=;i--){
update(a[i],v[i],,n,);
}
for(int i=;i<=n;i++){
printf("%d ",ans[i]);
}
printf("\n");
}
return ;
}

poj 2828(线段树单点更新)的更多相关文章

  1. POJ 2828 (线段树 单点更新) Buy Tickets

    倒着插,倒着插,这道题是倒着插! 想一下如果 Posi 里面有若干个0,那么排在最前面的一定是最后一个0. 从后往前看,对于第i个数,就应该插在第Posi + 1个空位上,所以用线段树来维护区间空位的 ...

  2. POJ 2828 线段树单点更新 离线搞

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

  3. POJ 2886 线段树单点更新

    转载自:http://blog.csdn.net/sdj222555/article/details/6878651 反素数拓展参照:http://blog.csdn.net/ACdreamers/a ...

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

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

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

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

  6. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

  7. POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)

    POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...

  8. HDU 1754 I Hate It 线段树单点更新求最大值

    题目链接 线段树入门题,线段树单点更新求最大值问题. #include <iostream> #include <cstdio> #include <cmath> ...

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

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

随机推荐

  1. spark(四)

    一. spark 2  版本 相对于以前版本的变化 spark core  : Accumulators (累加器):性能更好,页面上也可以看到累加器的信息 spark sql: 1. 2.DataS ...

  2. LOJ #6037.「雅礼集训 2017 Day4」猜数列 状压dp

    这个题的搜索可以打到48分…… #include <cstdio> #include <cstring> #include <algorithm> ; bool m ...

  3. selenium测试 - open Firefox

    环境:Python2.7+selenium3+Firefox47   问题1: 在打开火狐浏览器时报错:‘geckodriver‘ executable needs to be in PATH fro ...

  4. vs下给生成的程序(exe)加入默认的申请管理员权限

    在vs下编程时,经常会用到一些特殊的权限,尤其是管理员权限,下面是在win7下笔者亲测通过的. 下面要分两种情况: 第一种情况就是在vs2003或者以下的编译器中运行 1.准备一个manifest文件 ...

  5. bzoj 2277 [Poi2011]Strongbox 数论

    2277: [Poi2011]Strongbox Time Limit: 60 Sec  Memory Limit: 32 MBSubmit: 527  Solved: 231[Submit][Sta ...

  6. LeetCode-Max Points on a Line[AC源码]

    package com.lw.leet3; import java.util.HashMap; import java.util.Iterator; import java.util.Map; imp ...

  7. Maven手动添加依赖的jar文件到本地Maven仓库

    原文出处:https://www.iteblog.com/archives/646.html 寫得很好,轉走僅供學習,望諒解! Apache Maven,是一个软件(特别是Java软件)项目管理及自动 ...

  8. [Ctsc2014]企鹅QQ

    3555: [Ctsc2014]企鹅QQ Time Limit: 20 Sec  Memory Limit: 256 MB[Submit][Status][Discuss] Description P ...

  9. 2-sat HDU 1814

    题解来自于:http://www.cnblogs.com/kuangbin/archive/2012/10/05/2712622.html 和平委员会 根据宪法,Byteland民主共和国的公众和平委 ...

  10. CS48 D BIT

    统计一个点对应的和它严格右下方的点,点对数量.由于数据规模很大,不能直接上二维的前缀和,先排一维序,然后用BIT维护前缀和即可. /** @Date : 2017-09-14 20:17:30 * @ ...