6018: Buy Tickets(线段树单点更新)
6018: Buy Tickets
总提交: 18 测试通过:6
描述
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.
输入
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.
输出
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.
样例输入
4
0 77
1 51
1 33
2 69
4
0 20523
1 19243
1 3890
0 31492
样例输出
77 33 69 51
31492 20523 3890 19243
提示
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.
题目来源
解题思路: 题目意思动态的插入 从后往前 如果左边还有位置往左 不行往右
1 #include <bits/stdc++.h> 2 #define ll long long 3 using namespace std; 4 5 inline int read(){ 6 int x=0,f=1; 7 char ch=getchar(); 8 while(ch<'0'||ch>'9'){ 9 if(ch=='-') f=-1; 10 ch=getchar(); 11 } 12 while(ch>='0'&&ch<='9'){ 13 x=(x<<1)+(x<<3)+(ch^48); 14 ch=getchar(); 15 } 16 return x*f; 17 } 18 19 inline void write(int x){ 20 if(x<0){ 21 putchar('-'); 22 x=-x; 23 } 24 if(x>9){ 25 write(x/10); 26 } 27 putchar(x%10+'0'); 28 } 29 30 const int maxn=2e5+5; 31 int sum[maxn<<2],res[maxn]; 32 int n,arr[maxn],brr[maxn]; 33 34 void push_up(int rt){ 35 sum[rt]=sum[rt<<1]+sum[rt<<1|1]; //下标可以放的数量 36 } 37 38 void build(int rt,int left,int right){ 39 if(left==right){ 40 sum[rt]=1; 41 return; 42 } 43 int mid=left+right>>1; 44 build(rt<<1,left,mid); 45 build(rt<<1|1,mid+1,right); 46 push_up(rt); 47 } 48 49 void query(int rt,int left,int right,int ee,int num){ 50 if(left==right){ 51 // printf("%d %d \n",left,right); 52 sum[rt]=0; //不能种了 53 res[left]=num; 54 return; 55 } 56 int mid=left+right>>1; 57 if(sum[rt<<1]>=ee){ 58 query(rt<<1,left,mid,ee,num); 59 } 60 else query(rt<<1|1,mid+1,right,ee-sum[rt<<1],num); 61 push_up(rt); 62 } 63 64 65 66 int main(){ 67 68 while(scanf("%d",&n)!=EOF){ 69 build(1,1,n); 70 for(int i=1;i<=n;i++){ 71 arr[i]=read(),brr[i]=read(); 72 arr[i]++; 73 } 74 for(int i=n;i>=1;i--){ 75 query(1,1,n,arr[i],brr[i]); 76 } 77 for(int i=1;i<=n;i++){ 78 if(i!=1) putchar(' '); 79 write(res[i]); 80 } 81 putchar('\n'); 82 } 83 return 0; 84 }
原文地址:https://www.cnblogs.com/qq-1585047819/p/11844916.html
- Keras中带LSTM的多变量时间序列预测
- Spring Cloud(十)高可用的分布式配置中心 Spring Cloud Config 中使用 Refresh
- Hibernate 的性能优化的时候碰到了"抓取策略",有四种
- 基于 Spring Cloud 完整的微服务架构实战
- maven build时报错Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test
- Spring Cloud(九)高可用的分布式配置中心 Spring Cloud Config 集成 Eureka 服务
- Spring Cloud(八)高可用的分布式配置中心 Spring Cloud Config
- 用Raspberry Pi Zero打造「即插即用」的Web服务器
- Spring Cloud(七)服务网关 Zuul Filter 使用
- 基于Metronic的Bootstrap开发框架经验总结(1)-框架总览及菜单模块的处理
- Spring Cloud(六)服务网关 zuul 快速入门
- Docker Registry Server 搭建,配置免费HTTPS证书,及拥有权限认证、TLS 的私有仓库
- Ubuntu 17.04 编译安装 Nginx 1.9.9 配置 https 免费证书
- 基于Metronic的Bootstrap开发框架经验总结(2)--列表分页处理和插件JSTree的使用
- 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 数组属性和方法