Buy Tickets

Time Limit : 8000/4000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 31   Accepted Submission(s) : 15
Problem 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
<p>There will be several test cases in the input. Each test case consists of <i>N</i> + 1 lines where <i>N</i> (1 ≤ <i>N</i> ≤ 200,000) is given in the first line of the test case. The next <i>N</i> lines contain the pairs of values <i>Pos<sub>i</sub></i> and <i>Val<sub>i</sub></i> in the increasing order of <i>i</i> (1 ≤ <i>i</i> ≤ <i>N</i>). For each <i>i</i>, the ranges and meanings of <i>Pos<sub>i</sub></i> and <i>Val<sub>i</sub></i> are as follows:</p><ul><li><i>Pos<sub>i</sub></i> ∈ [0, <i>i</i> − 1] — The <i>i</i>-th person came to the queue and stood right behind the <i>Pos<sub>i</sub></i>-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.</li><li><i>Val<sub>i</sub></i> ∈ [0, 32767] — The <i>i</i>-th person was assigned the value <i>Val<sub>i</sub></i>.</li></ul><p>There no blank lines between test cases. Proceed to the end of input.</p>
 
Output
<p>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.</p>
 
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
 
Source
PKU
 
 
还是线段树的新手,做完才恍然线段树只是可供利用的一种数据结构,具体怎么用要发挥自己的想象力。。
一开始还在想用lowbit()之类的常用函数。
这道题目的中心在,逆序插入的最后一人位置不变。在结点用ksum表示空位数量。
 
 
#include <iostream>
#include <cstdio>
#include <algorithm>
#include<vector>
#include<cstring>
using namespace std;
int a[200005],b[200005],ans[200005];
int tem,t;
struct node
{
int l,r,ksum;
}tr[200005<<2];
void build(int n,int l,int r)
{
tr[n].l=l;
tr[n].r=r;
tr[n].ksum=r-l+1;
if(l==r)
return;
int m=(l+r)>>1;
build(n<<1,l,m);
build(n<<1|1,m+1,r);
}
void f(int k,int v,int n)
{
int m=(tr[n].l+tr[n].r)>>1;
if(tr[n].l==tr[n].r)
{
tr[n].ksum=0;
ans[tr[n].l]=v;
return;
}
if(tr[n<<1].ksum>=k)
f(k,v,n<<1);
else
f(k-tr[n<<1].ksum,v,n<<1|1);
tr[n].ksum=tr[n<<1].ksum+tr[n<<1|1].ksum;
} int main()
{ while(~scanf("%d",&t))
{
build(1,1,t);
tem=0;
for(int i=1;i<=t;i++)
scanf("%d%d",&a[i],&b[i]);
for(int i=t;i>0;i--)
f(a[i]+1,b[i],1);
for(int i=1;i<t;i++)
cout<<ans[i]<<" ";
cout<<ans[t]<<endl;
}
}

  

hdu 2828 Buy Tickets的更多相关文章

  1. hdu 2828 Buy Tickets(线段树)

    一道不算复杂的线段树,就是数据处理需要好好想一下. 将输入的所有数据从后往前输入,对于最后一个值,如果它想插入第i个位置,那么他就必须在前面留下i-1个位置.对于倒数第二个人,如果他想插入j位置,那么 ...

  2. poj 2828 Buy Tickets (线段树(排队插入后输出序列))

    http://poj.org/problem?id=2828 Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissio ...

  3. POJ 2828 Buy Tickets(排队问题,线段树应用)

    POJ 2828 Buy Tickets(排队问题,线段树应用) ACM 题目地址:POJ 2828 Buy Tickets 题意:  排队买票时候插队.  给出一些数对,分别代表某个人的想要插入的位 ...

  4. poj 2828 Buy Tickets(树状数组 | 线段树)

    题目链接:poj 2828 Buy Tickets 题目大意:给定N,表示有个人,给定每一个人站入的位置,以及这个人的权值,如今按队列的顺序输出每一个人的权值. 解题思路:第K大元素,非常巧妙,将人入 ...

  5. poj 2828 Buy Tickets 【线段树点更新】

    题目:id=2828" target="_blank">poj 2828 Buy Tickets 题意:有n个人排队,每一个人有一个价值和要插的位置,然后当要插的位 ...

  6. 线段树(单点更新) POJ 2828 Buy tickets

    题目传送门 /* 结点存储下面有几个空位 每次从根结点往下找找到该插入的位置, 同时更新每个节点的值 */ #include <cstdio> #define lson l, m, rt ...

  7. poj 2828 buy Tickets 用线段树模拟带插入的队列

    Buy Tickets Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2 ...

  8. POJ 2828 Buy Tickets(线段树 树状数组/单点更新)

    题目链接: 传送门 Buy Tickets Time Limit: 4000MS     Memory Limit: 65536K Description Railway tickets were d ...

  9. poj 2828 Buy Tickets【线段树单点更新】【逆序输入】

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 16273   Accepted: 8098 Desc ...

随机推荐

  1. React.js 小书 Lesson2 - 前端组件化(一):从一个简单的例子讲起

    作者:胡子大哈 原文链接:http://huziketang.com/books/react/lesson2 转载请注明出处,保留原文链接和作者信息. 很多课程一上来就给大家如何配置环境.怎么写 Re ...

  2. 【linux】关于linux命令

    1. 删除空目录文件夹rmdir [options]      DIRECTORY Ubuntu默认的源是国外的,下载速度会比较慢,cd /etc/apt gedit /etc/apt/

  3. vue学习中遇到的onchange、push、splice、forEach方法使用

    最近在做vue的练习,发现有些js中的基础知识掌握的不牢,记录一下: 1.onchange事件:是在域的内容改变时发生,单选框与复选框改变后触发的事件. 2.push方法:向数组的末尾添加一个或多个元 ...

  4. ASP.NET操作DataTable各种方法

    转:http://www.cnblogs.com/isking/p/6178268.html http://www.cnblogs.com/sntetwt/p/3496477.html public ...

  5. Javascript 学习 Boolean

    构造函数 new Boolean(value) //构造函数 Boolean(value) //转换函数 参数 value 由布尔对象存放的值或者要转换成布尔值的值 返回值 当作为一个构造函数(带有运 ...

  6. iOS 时间戳和时间互换,计算两日期相隔天数

    /*  *获取当前系统时间的时间戳  */ +(NSInteger)getNowTimestamp;   /**  * 获取当前时间  */ + (NSString *)getNowTimeTampF ...

  7. Spring自定义注解配置切面实现日志记录

    一: spring-mvc.xml: <!--配置日志切面 start,必须与mvc配置在同一个配置文件,否则无法切入Controller层--><!-- 声明自动为spring容器 ...

  8. Hibernate课程 初探多对多映射2-1 创建数据库表

    --创建表 -- 创建项目表 create table project(  proid int(10) not null comment '项目Id',  proname varchar(30) co ...

  9. 设置Tomcat的字符编码

    在 server.xml 中的 Connector 标签在加入 URIEncoding="UTF-8" 属性. <Connector port="8080" ...

  10. jQueryMobile(一)

    一].jQuery Mobile 页面 <!DOCTYPE html> <html lang="zh-cn"> <head> <meta ...