题目大意:有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(线段树单点更新,逆向思维)的更多相关文章

  1. poj-----(2828)Buy Tickets(线段树单点更新)

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 12930   Accepted: 6412 Desc ...

  2. POJ 2828 Buy Tickets(线段树单点)

    https://vjudge.net/problem/POJ-2828 题目意思:有n个数,进行n次操作,每次操作有两个数pos, ans.pos的意思是把ans放到第pos 位置的后面,pos后面的 ...

  3. POJ - 2828 Buy Tickets (段树单点更新)

    Description Railway tickets were difficult to buy around the Lunar New Year in China, so we must get ...

  4. HDU 1754 I Hate It 线段树单点更新求最大值

    题目链接 线段树入门题,线段树单点更新求最大值问题. #include <iostream> #include <cstdio> #include <cmath> ...

  5. HDU 1166 敌兵布阵(线段树单点更新)

    敌兵布阵 单点更新和区间更新还是有一些区别的,应该注意! [题目链接]敌兵布阵 [题目类型]线段树单点更新 &题意: 第一行一个整数T,表示有T组数据. 每组数据第一行一个正整数N(N< ...

  6. poj 2892---Tunnel Warfare(线段树单点更新、区间合并)

    题目链接 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensiv ...

  7. HDU 1166 敌兵布阵(线段树单点更新,板子题)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  8. POJ 1804 Brainman(5种解法,好题,【暴力】,【归并排序】,【线段树单点更新】,【树状数组】,【平衡树】)

    Brainman Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 10575   Accepted: 5489 Descrip ...

  9. HDU 1166 敌兵布阵(线段树单点更新,区间查询)

    描述 C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了.A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要监视这些工兵营地的活动情况 ...

  10. POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)

    POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...

随机推荐

  1. 数据可视化——matplotlib(2)

    导入相关模块 import matplotlib.pyplot as plt import numpy as np import pandas as pd 图表设置 添加X.Y轴标签以及图标标题 a ...

  2. Java虚拟机组成详解

    导读:详细而深入的总结,是对知识“豁然开朗”之后的“刻骨铭心”,想忘记都难. Java虚拟机(Java Virtual Machine)下文简称jvm,上一篇我们对jvm有了大体的认识,进入本文之后我 ...

  3. mysql数据库优化课程---15、mysql优化步骤

    mysql数据库优化课程---15.mysql优化步骤 一.总结 一句话总结:索引优化最立竿见影 1.mysql中最常用最立竿见影的优化是什么? 索引优化 索引优化,不然有多少行要扫描多少次,1亿行大 ...

  4. UVA-1615 Highway (贪心,区间选点)

    题目大意:有一条沿x轴正方向,长为L的高速公路,n个村庄,要求修建最少的公路出口数目,使得每个村庄到出口的距离不大于D. 题目分析:区间选点问题.在x轴上,到每个村庄距离为D的点有两个(超出范围除外) ...

  5. Linux 策略路由配置

    策略路由配置 #编辑rt_tables echo "192 net_192 " >> /etc/iproute2/rt_tables echo "196 ne ...

  6. ansible入门一(Ansible介绍及安装部署)

    本节内容: 运维工具 Ansible特性 Ansible架构图和核心组件 安装Ansible 演示使用示例 一.运维工具 作为一个Linux运维人员,需要了解大量的运维工具,并熟知这些工具的差异,能够 ...

  7. Linux 挂载系统盘

    适用系统:非IO优化+SSD云盘Linux(Redhat , CentOS,Debian,Ubuntu)实例,IO优化实例+SSD云盘数据盘分区挂载建议使用脚本:工具:auto_fdisk_ssd.s ...

  8. C++复习7.虚表的概念

    C++ 类的虚表 20130929 关键技术:封装.继承.组合.虚函数.抽象基类.动态绑定.多态性等等 1.首先整理一下在阿里巴巴面试遇到的函数虚表的问题. 在C++中的Class中的函数式存储在Cl ...

  9. Java线程的五种状态详解

    状态转换图 1.new状态:通过new关键字创建了Thread或其子类的对象 2.Runnable状态:即就绪状态.可从三种状态到达,new状态的Thread对象调用start()方法,Running ...

  10. SpringAnnotation注解之@PreDestroy,@PostConstruct,@Scope

    @Scope的使用很简单,直接在类上加上就行 @PostConstruct:相当于xml配置方式的init-method方法 @PreDestroy:相当于xml配置方式的destroy-method ...