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. 用java实现删除文件夹里的所有文件

    package com.org.improve.contact; import java.io.File; public class DeletePaper { /** * @param args * ...

  2. Misc1

    什么是编译进内核与制作成模块 编译进内核意味着内核对这一类的功能不会在依赖其他的东西, 说白了就是所谓静态编译, 内核在启动的时候就会拥有这一部分的功能, 但是这样内核的体积就会变大 编译成模块, 其 ...

  3. Unity C# 用枚举(enum)制作复选框

    最近在项目中做测试脚本用到一些布尔值做方法的开关,突然想到可以制作一个复选框控制开关. 首先搜集网上的资料,基本大同小异,这里就不多做解释了,代码附上: public class EnumFlagsA ...

  4. 深入理解vertical-align和line-height的关系

    vertical-align的百分比值不是相对于字体大小或者其他什么属性计算的,而是相对于line-height计算的.举个简单的例子,如下CSS代码: { line-height: 30px; ve ...

  5. asp.net core 2.1 生成swagger文档

    新建asp.netcore2.1 api项目 “WebApplication1” 在nuget管理器中添加对Swashbuckle.AspNetCore 3.0.0.Microsoft.AspNetC ...

  6. 小程序封装一个ajax

    小程序中,我们一般习惯把提交接口请求的方法放在一个公共js里面.下面是一个简单实现. (1)我们把所有请求的地址都放在一个json里面 var postAddress = { "domin& ...

  7. lucene中Field简介

    Lucene 6.1.0中存在的field种类如下(后缀是Field): 下面介绍几个常用的Field类型: TextField A field that is indexed and tokeniz ...

  8. python3绘图示例1(基于matplotlib)

    #!/usr/bin/env python# -*- coding:utf-8 -*- import numpy as npimport matplotlib.pyplot as pltimport ...

  9. Office加载项安装

    出自我的个人主页 Alvin Blog 前言 Excel加载项离不开安装,Excel加载项本身安装及其简单,但这是在申请下来Office开发者账户之后,再次之前都得自行安装 线上安装 微软申请开发者账 ...

  10. npm升级自身

    参考:https://github.com/felixrieseberg/npm-windows-upgrade Usage First, ensure that you can execute sc ...