poj-----(2828)Buy Tickets(线段树单点更新)
Buy Tickets
Time Limit: 4000MS |
Memory Limit: 65536K |
|
---|---|---|
Total Submissions: 12930 |
Accepted: 6412 |
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
POJ Monthly--2006.05.28, Zhu, Zeyuan
代码: 倒着插入数据....
代码:
1 #include<cstdio>
2 #include<cstring>
3 const int maxn=200005;
4 struct node
5 {
6 int lef,rig,ps; //ps-->在前方有多少个空位
7 int val;
8 int mid(){
9 return lef+((rig-lef)>>1);
10 }
11 };
12 struct sac
13 {
14 int pos,val;
15 };
16 sac sav[maxn];
17 node reg[maxn<<2];
18 int ans[maxn];
19 int cnt;
20 void Build(int left,int right,int pos)
21 {
22 reg[pos]=(node){left,right,1,0};
23 if(left==right) return ;
24 int mid=reg[pos].mid();
25 Build(left,mid,pos<<1);
26 Build(mid+1,right,pos<<1|1);
27 reg[pos].ps=reg[pos<<1].ps+reg[pos<<1|1].ps;
28 }
29
30 void Update(int ps,int val ,int pos)
31 {
32 if(reg[pos].lef==reg[pos].rig)
33 {
34 reg[pos].ps=0;
35 reg[pos].val=val;
36 return ;
37 }
38 int mid=reg[pos].mid();
39 if(reg[pos<<1].ps>ps)
40 Update(ps,val,pos<<1);
41 else Update(ps-reg[pos<<1].ps,val,pos<<1|1);
42 reg[pos].ps=reg[pos<<1].ps+reg[pos<<1|1].ps;
43 return ;
44 }
45 void Query(int pos)
46 {
47 if(reg[pos].lef==reg[pos].rig)
48 {
49 if(cnt==0)
50 printf("%d",reg[pos].val);
51 else printf(" %d",reg[pos].val);
52 cnt++;
53 return ;
54 }
55 int mid=reg[pos].mid();
56 Query(pos<<1);
57 Query(pos<<1|1);
58 return ;
59 }
60 int main()
61 {
62 int n;
63 while(scanf("%d",&n)!=EOF)
64 {
65 cnt=0;
66 Build(1,n,1);
67 for(int i=0;i<n;i++)
68 scanf("%d%d",&sav[i].pos,&sav[i].val);
69 for(int i=n-1;i>=0;i--)
70 Update(sav[i].pos,sav[i].val,1);
71 Query(1);
72 puts("");
73 }
74 return 0;
75 }
优化代码:
1 #include<cstdio>
2 #include<cstring>
3 const int maxn=200005;
4 struct node
5 {
6 int lef,rig,ps; //ps-->在前方有多少个空位
7 int mid(){
8 return lef+((rig-lef)>>1);
9 }
10 };
11 struct sac
12 {
13 int pos,val;
14 };
15 sac sav[maxn];
16 node reg[maxn<<2];
17 int ans[maxn];
18 void Build(int left,int right,int pos)
19 {
20 reg[pos]=(node){left,right,1};
21 if(left==right) return ;
22 int mid=reg[pos].mid();
23 Build(left,mid,pos<<1);
24 Build(mid+1,right,pos<<1|1);
25 reg[pos].ps=reg[pos<<1].ps+reg[pos<<1|1].ps;
26 }
27
28 void Update(int ps,int val ,int pos)
29 {
30 if(reg[pos].lef==reg[pos].rig)
31 {
32 reg[pos].ps=0;
33 ans[reg[pos].lef]=val;
34 return ;
35 }
36 int mid=reg[pos].mid();
37 if(reg[pos<<1].ps>ps)
38 Update(ps,val,pos<<1);
39 else Update(ps-reg[pos<<1].ps,val,pos<<1|1);
40 reg[pos].ps=reg[pos<<1].ps+reg[pos<<1|1].ps;
41 return ;
42 }
43 int main()
44 {
45 int n;
46 while(scanf("%d",&n)!=EOF)
47 {
48 Build(1,n,1);
49 for(int i=0;i<n;i++)
50 scanf("%d%d",&sav[i].pos,&sav[i].val);
51 for(int i=n-1;i>=0;i--)
52 Update(sav[i].pos,sav[i].val,1);
53 printf("%d",ans[1]);
54 for(int i=2;i<=n;i++)
55 printf(" %d",ans[i]);
56 puts("");
57 }
58 return 0;
59 }
- Python编写渗透工具学习笔记一 | 0x06 Zip包破解程序
- Java 并发排序
- Python编写渗透工具学习笔记一 | 0x07 Python实现键盘记录器
- Python编写渗透工具学习笔记一 | 0x08字典生成程序
- 漫步VR——Unity语音聊天室开发
- Python编写渗透工具学习笔记一 | 0x01 目录扫描程序
- JDK容器学习之HashMap (一) : 底层存储结构分析
- Python编写渗透工具学习笔记一 | 0x02实现一个反弹shell
- 动手实现MVC: 1. Java 扫描并加载包路径下class文件
- 动手实现MVC: 2. bean加载, IoC依赖注入
- Python编写渗透工具学习笔记一 | 0x04 nmap实现端口扫描(准确性更高)
- spring-boot & ffmpeg 搭建一个音频转码服务
- java 实现二维码生成工具类
- WriteUp分享 | CTF-web
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 《自然语言处理实战入门》 ---- 第4课 :中文分词原理及相关组件简介 之 汉语分词领域主要分词算法、组件、服务(上)...
- MySQL索引 Krains 2020-08-09
- 「查缺补漏」巩固你的Redis知识体系
- MySQL事务 Krains 2020-08-09
- Linux本地提权漏洞复现与检测思路
- 内容安全策略( CSP )
- [译] 优化 React APP 的 10 种方法
- 如何免登陆观看b站大会员番剧
- 聊聊越来越火的对象存储
- AJAX的基本原理及实例解析。
- Docker私有镜像仓库是什么?
- React Native布局详细指南
- 走进Golang之Context的使用
- 「Workshop」第十一期:降维
- 开始在 GitHub 上写博客