【poj2828】Buy Tickets 线段树 插队问题
【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 Posiand 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 Inpu
4
Sample Output
77 33 69 51
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 <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
#include <typeinfo>
#include <map>
#include <stack>
typedef long long ll;
#define inf 0x7fffffff
using namespace std;
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
} //**************************************************************************************
struct ss
{
int l,r,x;
} tr[*];
int n;
int a[];
void build(int k,int s,int t)
{
tr[k].l=s;
tr[k].r=t;
if(s==t)
{
tr[k].x=;
return;
}
int mid=(s+t)>>;
build(k<<,s,mid);
build(k<<|,mid+,t);
tr[k].x=tr[k<<].x+tr[k<<|].x;
}
void update(int k,int x,int y)
{
if(tr[k].l==tr[k].r)
{
tr[k].x=;
a[tr[k].l]=y;
return;
}
if(tr[k<<].x>=x)update(k<<,x,y);//能往左下放尽量放,同时你要明白这是个插队问题^-^!!
else update(k<<|,x-tr[k<<].x,y);
tr[k].x=tr[k<<].x+tr[k<<|].x;
}
int main()
{
int x[];
int y[];
while(scanf("%d",&n)!=EOF)
{
build(,,);
for(int i=; i<=n; i++)
{
scanf("%d%d",&x[i],&y[i]);
x[i]++;
}
for(int i=n; i>=; i--)
{
update(,x[i],y[i]);
}
for(int i=; i<n; i++)
{
printf("%d ",a[i]);
}
printf("%d\n",a[n]);
} return ;
}
【poj2828】Buy Tickets 线段树 插队问题的更多相关文章
- poj2828 Buy Tickets (线段树 插队问题)
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 22097 Accepted: 10834 Des ...
- [poj2828] Buy Tickets (线段树)
线段树 Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must ...
- poj-----(2828)Buy Tickets(线段树单点更新)
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 12930 Accepted: 6412 Desc ...
- [POJ2828]Buy Tickets(线段树,单点更新,二分,逆序)
题目链接:http://poj.org/problem?id=2828 由于最后一个人的位置一定是不会变的,所以我们倒着做,先插入最后一个人. 我们每次处理的时候,由于已经知道了这个人的位置k,这个位 ...
- POJ 2828 Buy Tickets(线段树·插队)
题意 n个人排队 每一个人都有个属性值 依次输入n个pos[i] val[i] 表示第i个人直接插到当前第pos[i]个人后面 他的属性值为val[i] 要求最后依次输出队中各个人的属性 ...
- poj 2828 Buy Tickets (线段树(排队插入后输出序列))
http://poj.org/problem?id=2828 Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissio ...
- Buy Tickets(线段树)
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 16607 Accepted: 8275 Desc ...
- POJ 2828 Buy Tickets 线段树 倒序插入 节点空位预留(思路巧妙)
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 19725 Accepted: 9756 Desc ...
- POJ 2828 Buy Tickets (线段树 or 树状数组+二分)
题目链接:http://poj.org/problem?id=2828 题意就是给你n个人,然后每个人按顺序插队,问你最终的顺序是怎么样的. 反过来做就很容易了,从最后一个人开始推,最后一个人位置很容 ...
随机推荐
- 简单的分页存储过程,Json格式日期转换为一般日期
简单的分页存储过程 CREATE PROC Paged @pageIndex INT, @pageCount INT OUTPUT, @pageSize INT AS DECLARE @count I ...
- jquery json ajax -2
如果使用的是虚拟空间, 那么你不能操纵/配置服务器上的php.ini配置文件 但是可以在自己的php文件中, 通过ini_set(...), 和一些对应的函数, 去重置(临时的,仅在当前文件中生效的) ...
- 记录一次centos6.4版本的VSFTP本地用户登陆的配置
其实vsftp是一个非常常用而且简单的服务,但是假如服务不是你配置的前者没有留下参考档案,的确是件头疼的事儿,特此记录下. 首先是vsftp的安装当然安装有源码的编译和yum等 这里我选择rpm包的y ...
- cocos代码研究(1)sprite学习笔记
各种方法创建Sprite和Animate //图片创建法 参数一:图片资源路径 参数二:Rect选区 auto sprite = Sprite::create(, )); addChild(sprit ...
- 关于showModalDialog()对话框点击按钮弹出新页面的问题
页面a.aspx上,单击按钮a,走脚本,弹出showModalDialog("b.aspx",....) 在b.aspx上有个服务器控件按钮b,单击按钮,更新数据后,会弹出一个新的 ...
- unity StreamingAssets路径
原地址:http://blog.csdn.net/nateyang/article/details/8493791 我们在读写例如XML和TXT文件的时候,在电脑上和手机上路径不一致,造成了很多麻烦, ...
- 多层神经网络与C++实现
BP理论部分参考:http://blog.csdn.net/itplus/article/details/11022243 参考http://www.cnblogs.com/ronny/p/ann_0 ...
- nginx: [emerg] getpwnam(“www”) failed
在配置nginx 时提示如下错误时:nginx: [emerg] getpwnam(“www”) failed 解决方案一 在nginx.conf中 把user nobody的注释去掉既可 解决方案二 ...
- 找不到提交和更新按钮,subversion不见了,无法更新和上传代码
1.查看settings/plugins/下有没有subversion 插件,如果有,确保勾上. 2.VCS->Enable Version Control Integration...
- linux使用技巧
<1>vim /etc/hosts.deny sshd : 192.168.0.25 :deny //ssh拒绝某ip或网段访问.(原理详见鸟哥基础版18章P56 ...