POJ 2828 线段树(想法)
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 15422 | Accepted: 7684 |
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 Valiin 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
因为题目要求数据的正确性,假设插入 2 1000 的时候,那么保证他之前一定有两个人。所以一种思路呼之欲出了,反向插入人,当插入他的时候就保留前面几个空位。线段树维护区间空位数即得出答案。
- #include <cstdio>
- #include <cstring>
- #include <algorithm>
- #include <iostream>
- #include <vector>
- #include <queue>
- #include <cmath>
- #include <set>
- using namespace std;
- #define N 200005
- #define ll root<<1
- #define rr root<<1|1
- #define mid (a[root].l+a[root].r)/2
- int n;
- struct mm{
- int id, num;
- }b[N];
- struct node{
- int l, r, sum, num;
- }a[N*];
- void build(int l,int r,int root){
- a[root].l=l;
- a[root].r=r;
- a[root].sum=r-l+;
- a[root].num=;
- if(l==r) return ;
- build(l,mid,ll);
- build(mid+,r,rr);
- }
- void update(int sum,int num,int root){
- if(a[root].l==a[root].r){
- a[root].num=num;
- a[root].sum--;
- return;
- }
- if(a[ll].sum>=sum) update(sum,num,ll);
- else update(sum-a[ll].sum,num,rr);
- a[root].sum=a[ll].sum+a[rr].sum;
- }
- int ans[N];
- void out(int root){
- if(a[root].l==a[root].r){
- ans[a[root].l]=a[root].num;return;
- }
- out(ll);
- out(rr);
- }
- main()
- {
- int i, j, k;
- while(scanf("%d",&n)==){
- for(i=;i<n;i++) scanf("%d %d",&b[i].id,&b[i].num);
- build(,n,);
- for(i=n-;i>=;i--){
- update(b[i].id+,b[i].num,);
- }
- // printf("11111\n");
- out();
- printf("%d",ans[]);
- for(i=;i<=n;i++) printf(" %d",ans[i]);
- printf("\n");
- }
- }
POJ 2828 线段树(想法)的更多相关文章
- poj 2828 线段树
http://poj.org/problem?id=2828 学到的思维: 1.变化的或者后来的优先影响前面的,那么从最后一个往前看,最后一个就成了 确定的, 而且后来的也能够确定----假设从前往后 ...
- poj 2828(线段树 逆向思考) 插队是不好的行为
http://poj.org/problem?id=2828 插队问题,n个人,下面n行每行a,b表示这个人插在第a个人的后面和这个人的编号为b,最后输出队伍的情况 涉及到节点的问题可以用到线段树,这 ...
- poj 2828(线段树单点更新)
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 18561 Accepted: 9209 Desc ...
- POJ 2828 (线段树 单点更新) Buy Tickets
倒着插,倒着插,这道题是倒着插! 想一下如果 Posi 里面有若干个0,那么排在最前面的一定是最后一个0. 从后往前看,对于第i个数,就应该插在第Posi + 1个空位上,所以用线段树来维护区间空位的 ...
- POJ 2828 线段树 逆序插入
思路: 1.线段树 逆着插入就OK了 2.块状链表 (可是我并不会写) //By SiriusRen #include <cstdio> #include <cstring> ...
- POJ 2828 线段树活用
题目大意:依次描述了一个N个人的队伍,每个人所站的序号以及他的价值,依次描述每个人的过程中,存在序号相同的人,表示该人插入到了前一个序号相同的人的前面.最后输出整个队伍的值排列情况. 这个题目确实难以 ...
- POJ 2828 线段树单点更新 离线搞
Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must get ...
- poj 2886 线段树+反素数
Who Gets the Most Candies? Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 12744 Acc ...
- poj 3468(线段树)
http://poj.org/problem?id=3468 题意:给n个数字,从A1 …………An m次命令,Q是查询,查询a到b的区间和,c是更新,从a到b每个值都增加x.思路:这是一个很明显的线 ...
随机推荐
- RobotFrameWork WebService Soap接口测试 (一)
在做完基于http协议的接口测试之后,开始弄soap协议了,之前有过开发java webservice自动化框架的经验,所以我想着应该并不会很难.对于webservice的简介,服务器端和客户端的开发 ...
- 【笔记】jquery append,appendTo,prepend,prependTo 介绍
在jquery权威指南里面学习到这一章,很有必要介绍一下里面的内容: 首先是append(content)这个函数: 意思是将内容content加入到所选择的对象内容的后面 例如:$("di ...
- Android之使用Volley框架在ListView中加载大量图片
1.listview 中的条目要用 Volley 中的 NetworkImageView,如果直接用ImageView也可以,但是要在getView方法中使用url地址设置为imageView的tag ...
- UCenter整合登陆时出现’Authorization has expired’错误(2014-03-13记)
原因之可能是两台机子的时间不一致导致.
- [课程设计]Scrum 1.6 多鱼点餐系统开发进度(点餐页面按钮添加&修复)
[课程设计]Scrum 1.6 多鱼点餐系统开发进度(点餐页面按钮添加&修复) 1.团队名称:重案组 2.团队目标:长期经营,积累客户充分准备,伺机而行 3.团队口号:矢志不渝,追求完美 4. ...
- python基础(内置函数+文件操作+lambda)
一.内置函数 注:查看详细猛击这里 常用内置函数代码说明: # abs绝对值 # i = abs(-123) # print(i) #返回123,绝对值 # #all,循环参数,如果每个元素为真,那么 ...
- python协程与异步I/O
协程 首先要明确,线程和进程都是系统帮咱们开辟的,不管是thread还是process他内部都是调用的系统的API,而对于协程来说它和系统毫无关系; 协程不同于线程的是,线程是抢占式的调度,而协程是协 ...
- SQL SA密码丢失
------------记不清了,以下似乎是这样操作的-----NET STOP MSSQLSERVER Net Start MSSQLServer /m"SQLCMD" 安装并使 ...
- [问题2014S10] 复旦高等代数II(13级)每周一题(第十教学周)
[问题2014S10] 设 \(A,B\) 为 \(n\) 阶方阵, 证明: \(AB\) 与 \(BA\) 相似的充分必要条件是 \[\mathrm{rank}\big((AB)^i\big)=\ ...
- HDU 5783 Divide the Sequence(数列划分)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...