告知每次要插到第 i 个位置上,问最后它们的顺序是什么。

这一题,不是考线段树,是考如何想出用线段树...思维很巧妙,倒过来做的话就能确定此人所在的位置....

 

线段树每个结点有一个remain,记录些线段还有多少个空位....开始时,叶结点的remain当然为1

Buy Tickets

Time Limit : 8000/4000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 10   Accepted Submission(s) : 5
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

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 ≤ iN). 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
#include<stdio.h>
#define maxn 500004
int a[maxn];
int b[maxn];
int c[maxn];
struct node
{
int left,right;
int remain;
}; node tree[*maxn];
void build(int left,int right,int i)
{
tree[i].left =left;
tree[i].right =right;
if(tree[i].left ==tree[i].right )
{
tree[i].remain=;
return ;
}
build(left,(left+right)/,*i);
build((left+right)/+,right,*i+);
tree[i].remain=tree[*i].remain+tree[*i+].remain;
}
void insert(int p,int val,int k)
{
if(tree[k].left == tree[k].right)
{
tree[k].remain--;
a[tree[k].left] = val;
return ;
}
if(p <= tree[k<<].remain)
insert(p,val,*k);
else
insert(p-tree[*k].remain,val,*k+); //右子树 tree[k].remain = tree[*k].remain + tree[*k+].remain; //更新remain
} int main()
{
int i,n;
while(~scanf("%d",&n))
{
for(i=;i<=n;i++)
scanf("%d%d",&b[i],&c[i]);
build(,n-,);
for(i=n;i>=;i--)
{
insert(b[i]+,c[i],);
}
printf("%d",a[]);
for(i=;i<n;i++)
printf(" %d",a[i]);
printf("\n");
}
return ;
}

HDU-- Buy Tickets的更多相关文章

  1. hdu 2828 Buy Tickets

    Buy Tickets Time Limit : 8000/4000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total ...

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

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

  3. HDU 1260 Tickets (普通dp)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1260 Tickets Time Limit: 2000/1000 MS (Java/Others)   ...

  4. 题解报告:hdu 1260 Tickets

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1260 Problem Description Jesus, what a great movie! T ...

  5. H - Buy Tickets POJ - 2828 逆序遍历 树状数组+二分

    H - Buy Tickets POJ - 2828 这个题目还是比较简单的,其实有思路,不过中途又断了,最后写了一发别的想法的T了. 然后脑子就有点糊涂,不应该啊,这个题目应该会写才对,这个和之前的 ...

  6. POJ2828 Buy Tickets[树状数组第k小值 倒序]

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 19012   Accepted: 9442 Desc ...

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

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

  8. Buy Tickets(线段树)

     Buy Tickets Time Limit:4000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

  9. 【poj2828】Buy Tickets 线段树 插队问题

    [poj2828]Buy Tickets Description Railway tickets were difficult to buy around the Lunar New Year in ...

  10. Buy Tickets

    Buy Tickets Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 16010 Accepted: 7983 Descript ...

随机推荐

  1. SDL实现限制帧速

    很多人都在SDL_PollEvent和SDL_WaitEvent之间纠结.前者会带来更好的帧数表现,但是CPU占用极大,可以直接吃掉一个核心.后者则基本不占用CPU,但是帧数会受到影响.有没有办法使两 ...

  2. 09.25日记(2014年9月25日23:22:06)用java这么多年面向对象我真的懂了吗,测试先行理念会玩吗

    二胡 (1)应该找些书来看看,工作N年并不代表就有N年的工作经验. (2)DiaTransit02,DiaDept02,DiaAirport02,DiaHighway02.都具有x,y属性为何不设计一 ...

  3. C语言库函数--操作文件

    //C库函数读取文件的代码 I/O缓冲机制 C语言库函数写文件都是写在内存中,然后一次写入磁盘.提高了效率. 读写文件,不对系统进行操作,一般采用C语言库函数.移植可以在任何可以对C支持的操作系统,而 ...

  4. 设置用户sudo -s拥有root权限

    开通普通用户的ROOT权限,上线了可以禁止用户使用root权限 修改配置文件 vi etc/sudoers 在 root    ALL=(ALL) ALL那么你就在下边再加一条配置:hjd ALL=( ...

  5. PHP中的预定义超全局数组

    定义 超全局变量,是在全部作用域中始终可用的内置变量. PHP中的许多预定义变量都是"超全局的",这意味着它们在一个脚本的全部作用域中都可用. 在函数或方法中无需执行 global ...

  6. MAC 终端快捷建

    常用的快捷键: Ctrl + d        删除一个字符,相当于通常的Delete键(命令行若无所有字符,则相当于exit:处理多行标准输入时也表示eof) Ctrl + h        退格删 ...

  7. alibaba的COBAR真是强大.

    近好不容易抽空研究了下Cobar,感觉这个产品确实很不错(在文档方面比Amoeba强多了),特此推荐给大家.Cobar是阿里巴巴研发的关系型数据 的分布式处理系统,该产品成功替代了原先基于Oracle ...

  8. C语言-06复杂数据类型-01数组

    01-数组 #include <stdio.h> int main() { // 使用注意 // 都是正确写法 //int ages[5] = {10 , 11, 12, 67, 56}; ...

  9. linux下测试磁盘的读写IO速度【转】

    Reference1:http://server.chinabyte.com/495/12361995.shtmlReference2:https://www.deleak.com/blog/2011 ...

  10. json序列化后日期如何变回来

    日期格式为 日期小于10的时候 占一位 比如 2019年9月1日 2015/9/1 function ChangeDateFormat(cellval) {           var date = ...