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 ≤ 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.

题意:按顺序给你n个人,给你他想要排的位置pos和他拥有的值val后来着可以插队比如pos[0]=0,pos[1]=0,那么pos[0]就变成了1被插队了。

问最后n个人排好后各自位置是多少,输出各个位置上对应的val值。

举一个例子6个人 1 2 3 4 5 6 位置,倒过来考虑因为最后一个选位置没人和他抢。假设他要2号位那么序列变为 1 0 2 3 4 5前面选2好位的只能退一位。

当前一个人选4号位那么 1 0 2 3 0 4,以此类推排好位置。这种方法用到前缀和就是一开始全部位置初值为1然后求个位置的前缀和,如果这个位置被选了

那么这个位置的值就变成0,更新。

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int M = 2e6 + 10;
int re;
int pos[M] , Val[M] , last[M];
struct TnT {
int l , r , val;
}T[M << 2];
void build(int l , int r , int p) {
int mid = (l + r) >> 1;
T[p].l = l , T[p].r = r;
if(T[p].l == T[p].r) {
T[p].val = 1;
return ;
}
build(l , mid , p << 1);
build(mid + 1 , r , (p << 1) | 1);
T[p].val = T[p << 1].val + T[(p << 1) | 1].val;
}
void updata(int num , int p) {
if(T[p].l == T[p].r) {
T[p].val = 0;
re = T[p].l;
return ;
}
if(num <= T[p << 1].val) {
updata(num , p << 1);
}
else {
updata(num - T[p << 1].val , (p << 1) | 1);
}
T[p].val = T[p << 1].val + T[(p << 1) | 1].val;
}
int main()
{
int n;
while(scanf("%d" , &n) != EOF) {
build(1 , n , 1);
for(int i = 0 ; i < n ; i++) {
scanf("%d%d" , &pos[i] , &Val[i]);
pos[i]++;
}
for(int i = n - 1 ; i >= 0 ; i--) {
updata(pos[i] , 1);
last[re] = Val[i];
}
for(int i = 1 ; i <= n ; i++) {
printf("%d " , last[i]);
}
printf("\n");
}
return 0;
}

poj 2828--Buy Tickets(线段树)的更多相关文章

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

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

  2. POJ 2828 Buy Tickets (线段树 or 树状数组+二分)

    题目链接:http://poj.org/problem?id=2828 题意就是给你n个人,然后每个人按顺序插队,问你最终的顺序是怎么样的. 反过来做就很容易了,从最后一个人开始推,最后一个人位置很容 ...

  3. POJ 2828 Buy Tickets 线段树 倒序插入 节点空位预留(思路巧妙)

    Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 19725   Accepted: 9756 Desc ...

  4. POJ 2828 Buy Tickets | 线段树的喵用

    题意: 给你n次插队操作,每次两个数,pos,w,意为在pos后插入一个权值为w的数; 最后输出1~n的权值 题解: 首先可以发现,最后一次插入的位置是准确的位置 所以这个就变成了若干个子问题, 所以 ...

  5. POJ 2828 Buy Tickets(线段树&#183;插队)

    题意  n个人排队  每一个人都有个属性值  依次输入n个pos[i]  val[i]  表示第i个人直接插到当前第pos[i]个人后面  他的属性值为val[i]  要求最后依次输出队中各个人的属性 ...

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

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

  7. poj 2828 Buy Tickets (线段树)

    题目:http://poj.org/problem?id=2828 题意:有n个人插队,给定插队的先后顺序和插在哪个位置还有每个人的val,求插队结束后队伍各位置的val. 线段树里比较简单的题目了, ...

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

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

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

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

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

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

随机推荐

  1. scroll-苹果滑动卡顿

    2018年08月02日,程序小bug. 在移动端html中经常出现横向/纵向滚动的效果,但是在iPhone中滚动速度很慢,感觉不流畅,有种卡卡的感觉,但是在安卓设备上没有这种感觉; 一行代码搞定: - ...

  2. Android buildType混淆代码

    [话题引入] ①在Android开发完成,我们会将代码打包成APK文件.选择 菜单栏 Build --> Build APK ②将查看视图切换到 Project 模式,文件夹下有一个debug模 ...

  3. Spider & CrawlSpider

    CrawlSpide 最重要的是分析源码官方文档百度收搜 Spider document 就是官方文档了1.3的源码分析CrawlSpide 是爬取一个网站常用的规则 它是对spider进一步的包装 ...

  4. SoapSerialization——手机号码归属地

    public class MainActivity extends AppCompatActivity { private EditText etNumber; private TextView tv ...

  5. 用原生JS实现AJAX和JSONP

    前端开发在需要与后端进行数据交互时,为了方便快捷,都会选择JQuery中封装的AJAX方法,但是有些时候,我们只需要JQuery的AJAX请求方法,而其他的功能用到的很少,这显然是没必要的.其实,原生 ...

  6. Xcodebuild命令使用

    Xcodebuild简介 Xcodebuild是命令行工具包的其中一项. 命令行工具包(Command Line Tools)是一个轻量的.可以与XCode分开的.在Mac上单独下载的命令行工具包. ...

  7. 微信小程序页面跳转url如何传对象参数

    两步走 首先第一步:wx.navigateTo({ url:"XXX"+"&params="+ JSON.stringify(obj); }) 第二步获 ...

  8. Go最火的Gin框架简单入门

    Gin 介绍 Gin 是一个 Golang 写的 web 框架,具有高性能的优点,,基于 httprouter,它提供了类似martini但更好性能(路由性能约快40倍)的API服务.官方地址:htt ...

  9. Python自动化开发

    阅读目录 第一篇:python入门 第二篇:字符编码.文件处理 第三篇:函数 第四篇:迭代器.生成器.三元表达式.列表生成式 第五篇:模块.包.软件开发规范 第六篇:日志模块 第七篇:常用模块 第八篇 ...

  10. spring-boot-plus项目打包(七)

    spring-boot-plus项目打包 项目打包 spring-boot-plus项目使用maven assembly插件进行打包 根据不同环境进行打包部署 包含启动.重启命令,配置文件提取到外部c ...