Buy Tickets(线段树单点更新,逆向思维)
题目大意:有n个的排队,每一个人都有一个val来对应,每一个后来人都会插入当前队伍的某一个位置pos。要求把队伍最后的状态输出。
个人心得:哈哈,用链表写了下,果不其然超时了,后面转念一想要用静态数组思维, 还是炸了。大牛们很给力,逆向一转,真是服气。
一想是呀,转过来的话那么此时的人必然可以得到他的位置,此时更新长度,后面的人在此时的队列中依旧可以得到他想要的位置。
就算思路知道了,怎么实现呢,大神果然不愧是大神,线段树sum表示总长度,节点表示是否存在被占据,然后更新就可以了。
真的是佩服到一派涂地,orz.....orz
题目原文:
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.
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iomanip>
#include<algorithm>
using namespace std;
#define inf 1<<29
#define nu 4000005
#define maxnum 200005
int n;
int ans[maxnum],sum[maxnum<<];
int pre[maxnum],val[maxnum];
void upset(int root)
{
sum[root]=sum[root*]+sum[root*+];
}
void build(int root,int l,int r)
{
if(l==r)
{
sum[root]=;
return ;
}
int mid=(l+r)/;
build(root*,l,mid);
build(root*+,mid+,r);
upset(root);
}
void update(int root,int l,int r,int j)
{
if(l==r){
sum[root]--;
ans[l]=val[j];
return ;
}
int mid=(l+r)/;
if(sum[root*]>=pre[j])
update(root*,l,mid,j);
else
{
pre[j]-=sum[root*];
update(root*+,mid+,r,j);
}
upset(root);
}
int main()
{
int i,j;
while(scanf("%d",&n)!=EOF){
for(i=;i<=n;i++)
{
scanf("%d%d",&pre[i],&val[i]);
pre[i]++;
}
build(,,n);
for(i=n;i>;i--)
{
update(,,n,i);
}
printf("%d",ans[]);
for(i=;i<=n;i++)
printf(" %d",ans[i]);
printf("\n");
}
return ;
}
Buy Tickets(线段树单点更新,逆向思维)的更多相关文章
- poj-----(2828)Buy Tickets(线段树单点更新)
Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 12930 Accepted: 6412 Desc ...
- POJ 2828 Buy Tickets(线段树单点)
https://vjudge.net/problem/POJ-2828 题目意思:有n个数,进行n次操作,每次操作有两个数pos, ans.pos的意思是把ans放到第pos 位置的后面,pos后面的 ...
- POJ - 2828 Buy Tickets (段树单点更新)
Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must get ...
- HDU 1754 I Hate It 线段树单点更新求最大值
题目链接 线段树入门题,线段树单点更新求最大值问题. #include <iostream> #include <cstdio> #include <cmath> ...
- HDU 1166 敌兵布阵(线段树单点更新)
敌兵布阵 单点更新和区间更新还是有一些区别的,应该注意! [题目链接]敌兵布阵 [题目类型]线段树单点更新 &题意: 第一行一个整数T,表示有T组数据. 每组数据第一行一个正整数N(N< ...
- poj 2892---Tunnel Warfare(线段树单点更新、区间合并)
题目链接 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensiv ...
- HDU 1166 敌兵布阵(线段树单点更新,板子题)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- POJ 1804 Brainman(5种解法,好题,【暴力】,【归并排序】,【线段树单点更新】,【树状数组】,【平衡树】)
Brainman Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 10575 Accepted: 5489 Descrip ...
- HDU 1166 敌兵布阵(线段树单点更新,区间查询)
描述 C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况 ...
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
随机推荐
- Gtk基础学习总结(一)
第一个GTK程序例子: #include <stdio.h> #include <gtk/gtk.h> int main(int argc, char *argv[]) { g ...
- Struts2框架学习第二章——Struts2下的HelloWorld
本章要点 — Struts 2的下载和安装 — 纯手工创建一个Web应用 — 纯手工创建一个Struts 2应用 — 实现Struts 2的Action — 配置Struts 2的Action — ...
- nethogs 按进程实时统计网络带宽利用率的工具
安装 sudo apt-get install nethogs 使用 sudo nethogs
- 【Python】模块学习之ConfigParser读写配置信息
前言 使用配置文件可以在不修改程序的情况下,做到对程序功能的定制.Python 使用自带的configParser模块可以很方便的读写配置文件的信息. configParser 支持的方法 Confi ...
- phpcms v9 栏目伪静态完全自定义为栏目英文目录名
1,后台增加url规则,增加后.导航上,或分页号上,会自动替换为静态的样式.类似www.abc.com/news/2/ 2表示页码 phpcms v9 的后台扩展,url规则,添加两个规则, 一个是名 ...
- [spring mvc]Hello World入门
1.新建项目 File->New->Other,选择Dynamic web project: 项目建好之后,目录结构如下: 2.WEB-INF/web.xml 中配置 dispatcher ...
- js中的内部属性与delete操作符
本文正式地址:http://www.xiabingbao.com/javascript/2015/08/03/javascript-delete-configurable 在讲解Configurabl ...
- Kafka消息topic分区
kafka是为分布式环境设计的,因此如果日志文件,其实也可以理解成消息数据库,放在同一个地方,那么必然会带来可用性的下降,一挂全挂,如果全量拷贝到所有的机器上,那么数据又存在过多的冗余,而且由于每 ...
- rdlc报表出现多余空白页面
1.RDLC报表设计好后,在ReportViewer预览报表时,页数都正常:但在切换为整页模式时,常造成多了不少空白页出来.第一时间觉得不可思议,在ReportViewer看来正常,怎可能在整页预览时 ...
- IOS-H5容器的一些探究:UIWebView和WKWebView的比较和选择
一.Native开发中为什么需要H5容器 Native开发原生应用是手机操作系统厂商(目前主要是苹果的iOS和google的Android)对外界提供的标准化的开发模式,他们对于native开发提供了 ...