Buy Tickets
Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 22097   Accepted: 10834

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.

Source

实现代码:

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mid int m = (l + r)>>1
const int M = 2e5+;
ll sum[M<<],a[M<<],b[M<<],md,ans[M<<]; void pushup(int rt){
sum[rt] = sum[rt<<] + sum[rt<<|];
} void build(int l,int r,int rt){
if(l == r){
sum[rt] = ;
return ;
}
mid;
build(lson);
build(rson);
pushup(rt);
} void update(int p,int c,int l,int r,int rt){
if(l == r){
ans[l] = c;
sum[rt]--;
return ;
}
mid;
if(sum[rt<<] >= p) update(p,c,lson);
else update(p-sum[rt<<],c,rson);
pushup(rt);
} int main()
{
int n;
while(scanf("%d",&n)!=EOF){
memset(ans,,sizeof(ans));
build(,n,);
for(int i = ;i < n;i ++){
scanf("%d%d",&a[i],&b[i]);
}
for(int i = n-;i >= ;i --){
update(a[i]+,b[i],,n,);
}
for(int i = ;i < n;i ++)
printf("%d ",ans[i]);
printf("%d\n",ans[n]);
}
return ;
}

poj2828 Buy Tickets (线段树 插队问题)的更多相关文章

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

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

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

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

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

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

  4. [POJ2828]Buy Tickets(线段树,单点更新,二分,逆序)

    题目链接:http://poj.org/problem?id=2828 由于最后一个人的位置一定是不会变的,所以我们倒着做,先插入最后一个人. 我们每次处理的时候,由于已经知道了这个人的位置k,这个位 ...

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

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

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

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

  7. Buy Tickets(线段树)

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 16607   Accepted: 8275 Desc ...

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

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

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

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

随机推荐

  1. ASP.NET Core MVC中的IActionFilter.OnActionExecuting方法,可以获取Controller的Action方法参数值

    用过ASP.NET Core MVC中IActionFilter拦截器的开发人员,都知道这是一个非常强大的MVC拦截器.最近才发现IActionFilter的OnActionExecuting方法,甚 ...

  2. 大数据入门第十七天——storm上游数据源 之kafka详解(二)常用命令

    一.kafka常用命令 1.创建topic bin/kafka-topics. --replication-factor --zookeeper mini1: // 如果配置了PATH可以省略相关命令 ...

  3. Linux下Maven+SVN自动打包脚本

        公司的开发环境每次部署项目都很麻烦,需要手动打包并上传上去.这个太麻烦了,所以就准备搞个自动打包的脚本.脚本自动从svn代码库里面更新最新的代码下来,然后maven打包,最后把war包丢到to ...

  4. 6、使用jconsole+VisualVM分析JVM

    一.不断增加对象触发GC的代码 VM 参数:-Xms100m -Xmx100m -XX:+UseSerialGC import java.util.ArrayList; import java.uti ...

  5. VS中为非控制台程序提供控制台输出窗口

    /************************************************************************/ /* 模块名:ConsoleAdapter 文件名 ...

  6. linux 升级 5.0.2内核

    1.下载 wet https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.0.2.tar.xz -o /usr/src/ cd /usr/src ta ...

  7. [CF966F]May Holidays[分块+虚树]

    题意 给定 \(n\) 个点的树,初始所有颜色都是 \(0\) ,每个点有一个阈值 \(t\) ,每次可能会让一个点的颜色异或1,问每次操作之后有多少个点满足子树内的颜色为 \(1\) 的点的个数 \ ...

  8. Microsoft Dynamics CRM 常用JS语法(已转成vs2017语法提示)

    背景 最近接触到Microsoft Dynamics CRM的开发.前端js是必不可少的部分,奈何没有一个语法提示,点不出来后续的语句. 在vscode上面搜索插件的时候发现,有一个大神写的插件htt ...

  9. Ps矩形工具的运用

    矩形工具 1.标识位置以及快捷键 位于左侧工具栏中,快键键是u,根据需求选择里面包含的工具 2.使用方法 鼠标左键点击工具,直接在图层使用,点击后拖住不放选择想要的图形后松手即可. 可以根据自身的需求 ...

  10. jmeter的开启

    先申明我以jmeter的3.2版本来描述本文内容,通常不要选择太新的版本,因为新版本容易出现不稳定和不兼容因素.启动jmeter的前提是JDK已经安装和配置,具体的JDK已经安装和配置自行查资料,此处 ...