题目大意:有n个的排队,每一个人都有一个val来对应,每一个后来人都会插入当前队伍的某一个位置pos。要求把队伍最后的状态输出。

个人心得:哈哈,用链表写了下,果不其然超时了,后面转念一想要用静态数组思维, 还是炸了。大牛们很给力,逆向一转,真是服气。

一想是呀,转过来的话那么此时的人必然可以得到他的位置,此时更新长度,后面的人在此时的队列中依旧可以得到他想要的位置。

就算思路知道了,怎么实现呢,大神果然不愧是大神,线段树sum表示总长度,节点表示是否存在被占据,然后更新就可以了。

真的是佩服到一派涂地,orz.....orz

题目原文:

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.

 #include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iomanip>
#include<algorithm>
using namespace std;
#define inf 1<<29
#define nu 4000005
#define maxnum 200005
int n;
int ans[maxnum],sum[maxnum<<];
int pre[maxnum],val[maxnum];
void upset(int root)
{
sum[root]=sum[root*]+sum[root*+];
}
void build(int root,int l,int r)
{
if(l==r)
{
sum[root]=;
return ;
}
int mid=(l+r)/;
build(root*,l,mid);
build(root*+,mid+,r);
upset(root);
}
void update(int root,int l,int r,int j)
{
if(l==r){
sum[root]--;
ans[l]=val[j];
return ;
}
int mid=(l+r)/;
if(sum[root*]>=pre[j])
update(root*,l,mid,j);
else
{
pre[j]-=sum[root*];
update(root*+,mid+,r,j);
}
upset(root);
}
int main()
{
int i,j;
while(scanf("%d",&n)!=EOF){
for(i=;i<=n;i++)
{
scanf("%d%d",&pre[i],&val[i]);
pre[i]++;
}
build(,,n);
for(i=n;i>;i--)
{
update(,,n,i);
}
printf("%d",ans[]);
for(i=;i<=n;i++)
printf(" %d",ans[i]);
printf("\n");
}
return ;
}

Buy Tickets(线段树单点更新,逆向思维)的更多相关文章

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

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 12930   Accepted: 6412 Desc ...

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

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

  3. POJ - 2828 Buy Tickets (段树单点更新)

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

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

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

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

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

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

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

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

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

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

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

  9. HDU 1166 敌兵布阵(线段树单点更新,区间查询)

    描述 C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况 ...

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

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

随机推荐

  1. bootstrap自定义——栅格列数修改

    从下载的bootstrap文件中找到less文件夹里面的variables.less,然后可以找到栅格列数进行修改 然后执行一下bootstrap.less,通过命令行,切换到其所在的目录D:\03 ...

  2. Memcached CAS 命令

    Memcached CAS(Check-And-Set 或 Compare-And-Swap) 命令用于执行一个"检查并设置"的操作 它仅在当前客户端最后一次取值后,该key 对应 ...

  3. max_spare_servers到底是个什么意思?

    pm.max_children=500pm.start_servers=280pm.min_spare_servers=50pm.max_spare_servers=500 把max_spare_se ...

  4. pfSense 2.4.3 发布,包含重要的安全修复补丁

    pfSense 2.4.3 已发布,本次更新包含重要的安全修复和 bug 修复,还引入了一些新特性,具体如下. 值得关注的更新 包含一些重要的安全修复补丁: Kernel PTI mitigation ...

  5. ffmpeg播放RTSP的一点优化

    简单记录一下最近使用ffmpeg播放RTSP做的一点参数优化. 先做如下定义: AVDictionary* options = NULL; 1.画质优化 原生的ffmpeg参数在对1920x1080的 ...

  6. Friendly Date Ranges

    让日期区间更友好! 把常见的日期格式如:YYYY-MM-DD 转换成一种更易读的格式. 易读格式应该是用月份名称代替月份数字,用序数词代替数字来表示天 (1st 代替 1). 记住不要显示那些可以被推 ...

  7. redisAPI整理

    全局命令 1.查看所有键 keys * 2.键总数 dbsize 3.检查键是否存在 exists key 4.删除键 del key del key1 key2 key3 5.键过期 expire ...

  8. redis的String类型以及其操作

    Redis的数据类型 String类型以及操作 String是最简单的数据类型,一个key对应一个Value,String类型是二进制安全的.Redis的String可以包含任何数据,比如jpg图片或 ...

  9. 用JQuery怎么去一次性获取 aspTextBox 文本框的值

    如:<asp:TextBox ID="txtSubject" runat="server">aa</asp:TextBox> 第一次用服 ...

  10. Makefile特殊标签

    http://www.gnu.org/software/make/manual/html_node/Special-Targets.html