POJ2828 Buy Tickets 树状数组
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
//By zZhBr
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std; int n; struct pro
{
int pos;
int num;
}pr[]; int ans[]; int tr[]; int lowbit(int x)
{
return x & -x;
} void add(int x, int y)
{
while(x <= n)
{
tr[x] += y;
x += lowbit(x);
}
} int sum(int x)
{
int ans = ;
while(x != )
{
ans += tr[x];
x -= lowbit(x);
}
return ans;
} int main()
{
while(scanf("%d", &n) != EOF)
{
for(register int i = ; i <= n ; i ++) ans[i] = ;
for(register int i = ; i <= n ; i ++)
{
scanf("%d%d", &pr[i].pos, &pr[i].num);
add(i, );
} for(register int i = n ; i >= ; i --)
{
int p = pr[i].pos + ; if(sum(p) == p)
{
ans[p] = pr[i].num;
add(p, -);
continue;
} int l = p, r = n;
while(l < r)
{
int mid = l + r >> ;
if(sum(mid) >= p) r = mid;
else l = mid + ;
} ans[l] = pr[i].num;
add(l, -); } for(register int i = ; i <= n ; i ++)
{
printf("%d ", ans[i]);
}
printf("\n"); }
return ;
}
zZhBr
POJ2828 Buy Tickets 树状数组的更多相关文章
- POJ2828 Buy Tickets[树状数组第k小值 倒序]
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 19012 Accepted: 9442 Desc ...
- POJ2828 Buy Tickets [树状数组,二分答案]
题目传送门 Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 22611 Accepted: 110 ...
- poj 2828 Buy Tickets(树状数组 | 线段树)
题目链接:poj 2828 Buy Tickets 题目大意:给定N,表示有个人,给定每一个人站入的位置,以及这个人的权值,如今按队列的顺序输出每一个人的权值. 解题思路:第K大元素,非常巧妙,将人入 ...
- poj 2828 Buy Tickets 树状数组
Buy Tickets Description Railway tickets were difficult to buy around the Lunar New Year in China, so ...
- H - Buy Tickets POJ - 2828 逆序遍历 树状数组+二分
H - Buy Tickets POJ - 2828 这个题目还是比较简单的,其实有思路,不过中途又断了,最后写了一发别的想法的T了. 然后脑子就有点糊涂,不应该啊,这个题目应该会写才对,这个和之前的 ...
- POJ 2828 Buy Tickets (线段树 or 树状数组+二分)
题目链接:http://poj.org/problem?id=2828 题意就是给你n个人,然后每个人按顺序插队,问你最终的顺序是怎么样的. 反过来做就很容易了,从最后一个人开始推,最后一个人位置很容 ...
- POJ 2828 Buy Tickets (线段树 || 树状数组)
题目大意 一些小朋友在排队,每次来一个人,第i个人会插到第x个人的后面.权值为y.保证x∈[0,i-1]. 按照最后的队伍顺序,依次输出每个人的权值. 解题分析 好气吖.本来是在做splay练习,然后 ...
- 【poj2182】【poj2828】树状数组/线段树经典模型:逆序查找-空位插入法
poj2182题意:有一个1~n的排列,现在给定每个人前面有多少个人的编号比他大,求这个排列是什么.n<=8000 poj2182题解: 逆序做,可以确定二分最后一个是什么,然后删除这个数.树状 ...
- 小结:线段树 & 主席树 & 树状数组
概要: 就是用来维护区间信息,然后各种秀智商游戏. 技巧及注意: 一定要注意标记的下放的顺序及影响!考虑是否有叠加或相互影响的可能! 和平衡树相同,在操作每一个节点时,必须保证祖先的tag已经完全下放 ...
随机推荐
- WeakMap 本身释放,而 keyObject 没有释放的情况下,value 会释放吗?
const keyObject = ['keyObject']; new WeakMap().set(keyObject, ['value']); 问题:现在 ['value'] 会被释放吗? 听说W ...
- CTF常用软件/工具
慢慢更新 整合版: http://www.jz5u.com/Soft/Progra/tool/163275.html/ 各种在线工具以及工具整合 http://www.ctftools.com/ 逆向 ...
- 【linux】【redis】redis安装及开启远程访问
系统环境:Centos7 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API. 1.yum安装过程参考:https ...
- 利用Python进行数据分析:【Matplotlib】
一.简单介绍Matplotlib 1.Matplotlib是一个强大的Python绘图和数据可视化的工具包2.安装方法:pip install matplotlib 3.引用方法:import mat ...
- .NetCore技术研究-一套代码同时支持.NET Framework和.NET Core
在.NET Core的迁移过程中,我们将原有的.NET Framework代码迁移到.NET Core.如果线上只有一个小型的应用还好,迁移升级完成后,只需要维护.NET Core这个版本的代码. 但 ...
- rt.jar包添加源文件只需要关联到已安装对应jdk目录下source.zip源码文件即可
项目中配置的JRE System Libriry下的rt.jar包,需要关联源文件时候,只需要点击“Attach Source...“按钮,选择"External File..." ...
- jquery 动态控制显隐
1.第1种方法 ,给元素设置style属性 $("#hidediv").css("display", "block"); 2.第2种方法 , ...
- jQuery常用方法(五)-jQuery CSS
JQuery CSS 方法说明 css( name ) 访问第一个匹配元素的样式属性. css( properties ) 把一个"名/值对"对象设置为所有匹配元素的样式属性. $ ...
- Shell之StdI/O和Pipe
目录 Shell之StdI/O和Pipe 参考 StdI/O重定向 Pipe 常用组合 Shell之StdI/O和Pipe
- 品Spring:对@Autowired和@Value注解的处理方法
在Spring中能够完成依赖注入的注解有JavaSE提供的@Resource注解,就是上一篇文章介绍的. 还有JavaEE提供的@javax.inject.Inject注解,这个用的很少,因为一般都不 ...