Buy Tickets

Time Limit: 4000ms
Memory Limit: 65536KB

This problem will be judged on PKU. Original ID: 2828
64-bit integer IO format: %lld      Java class name: Main

 

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

Source

 
解题:线段树。。。这题目真叼,居然可以这么求!因为最后上的,位置肯定是它的!没有别人把他挤下去了!所以逆着更新。代码很简单,思路很叼。
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct node{
int lt,rt,sum;
};
node tree[maxn<<];
int ans[maxn],ps[maxn],w[maxn],n;
void build(int lt,int rt,int v){
tree[v].lt = lt;
tree[v].rt = rt;
if(lt == rt){
tree[v].sum = ;
return;
}
int mid = (lt+rt)>>;
build(lt,mid,v<<);
build(mid+,rt,v<<|);
tree[v].sum = tree[v<<].sum + tree[v<<|].sum;
}
void update(int p,int val,int v){
int mid = (tree[v].lt+tree[v].rt)>>;
if(tree[v].lt == tree[v].rt){
tree[v].sum = ;
ans[tree[v].lt] = val;
return;
}
if(tree[v<<].sum >= p) update(p,val,v<<);
else update(p-tree[v<<].sum,val,v<<|);
tree[v].sum--;
}
int main() {
int i;
while(~scanf("%d",&n)){
for(i = ; i < n; i++)
scanf("%d %d",ps+i,w+i);
build(,n,);
for(i--; i >= ; i--)
update(ps[i]+,w[i],);
for(i = ; i < n; i++)
printf("%d ",ans[i]);
printf("%d\n",ans[i]);
}
return ;
}

最新文章

  1. nginx 安全优化
  2. Unity StartCoroutine 和 yield return 深入研究
  3. css系列-间隔与间距实例
  4. JavaPersistenceWithHibernate第二版笔记-第四章-Mapping persistent classes-003映射实体时的可选操作(&lt;delimited-identifiers/&gt;、PhysicalNamingStrategy、PhysicalNamingStrategyStandardImpl、、、)
  5. UnicodeEncodeError: &lsquo;ascii&rsquo; codec can&rsquo;t encode characters in position xxx ordinal
  6. log的6种等级
  7. CSS/CSS3长度、时间、频率、角度单位大全
  8. GO求平均值
  9. 阿里云WinServer2008下配置IIS7支持php
  10. 竞价广告系统-逻辑回归优化方法-L-BFGS
  11. 关于ng-options
  12. Leetcode题解(34)
  13. 深入理解ES6之—块级绑定
  14. nginx防盗链
  15. .NET Core 2.0迁移技巧之web.config配置文件
  16. 实验一:c++简单程序设计(1)
  17. iOS----------关于UDID和UUID的一些理解
  18. python3-面向对象进阶(内置方法)
  19. day22 time模块
  20. 微信小游戏开发之JS面向对象

热门文章

  1. 洛谷 P2742 [USACO5.1]圈奶牛Fencing the Cows || 凸包模板
  2. 题解报告:hdu 1235 统计同成绩学生人数
  3. paint之Graphics
  4. windows系统下在忘记安装make的Cygwin中如何正确安装make(图文详解)
  5. Jquery插件jqprint-0.3.js实现打印
  6. AJPFX总结IO流中的缓冲思想
  7. Swift 基础语法入门(一)
  8. lua使用lfs.dll库进行文件操作
  9. 6 Specialzed layers 特殊层 第一部分 读书笔记
  10. cookie设置和读取以及获取超链接参数