题目描述

维护一个队列,初始为空。依次加入 n(1≤n≤105)个数 ai(-109≤ai≤109),第 i(1≤i≤n)个数加入到当前序列第 bi(0≤bi≤当前序列长度)个数后面。输出最终队列。

输入格式

输入包含一个数 n(1≤n≤3×105),表示最终序列长度。

接下来 n 行,每行两个数 ai,bi,表示把 ai 放在当前序列第 bi 个数后面。

输出格式

输出 n 行,每行一个数,表示最终序列。

样例数据 1

输入


1 0 
2 0 
3 0 
4 0 
5 0

输出





1

题目分析

  平衡树如splay/treap应该都能完成,这里给出无旋treap的做法。用无旋treap的话简直就是裸题,时间复杂度nlogn

code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std; const int N = 3e5 + ;
int n;
#define SZ(x) (x?x->sze:0)
inline void wr(int);
struct node{
node *lc, *rc;
int pri, sze, val;
inline node* upt(){
sze = SZ(lc) + SZ(rc) + ;
return this;
}
inline void print(){
if(lc) lc->print();
wr(val), putchar('\n');
if(rc) rc->print();
}
}pool[N], *tail = pool, *root = NULL; inline int read(){
int i = , f = ; char ch = getchar();
for(; (ch < '' || ch > '') && ch != '-'; ch = getchar());
if(ch == '-') f = -, ch = getchar();
for(; ch >= '' && ch <= ''; ch = getchar())
i = (i << ) + (i << ) + (ch - '');
return i * f;
} inline void wr(int x){
if(x < ) putchar('-'), x = -x;
if(x > ) wr(x / );
putchar(x % + '');
} inline int Rand(){
static int RAND_VAL = ;
return RAND_VAL += RAND_VAL << | ;
} inline node* newNode(int v){
node *x = tail++;
x->lc = x->rc = NULL;
x->pri = Rand();
x->val = v;
x->sze = ;
return x;
} inline node* Merge(node *u, node *v){
if(!u) return v->upt();
if(!v) return u->upt();
if(u->pri < v->pri){
u->rc = Merge(u->rc, v);
return u->upt();
}
else{
v->lc = Merge(u, v->lc);
return v->upt();
}
} inline void Split_k(node *u, int k, node *&x, node *&y){
if(!u){
x = y = NULL;
return;
}
if(SZ(u->lc) < k){
Split_k(u->rc, k - SZ(u->lc) - , x, y);
u->rc = NULL, u->upt();
x = Merge(u, x);
}
else{
Split_k(u->lc, k, x, y);
u->lc = NULL, u->upt();
y = Merge(y, u);
}
} inline node* Insert(node *u, int k, int v){
node *L, *R;
Split_k(u, k, L, R);
node *res = newNode(v);
return Merge(Merge(L, res), R);
} int main(){
n = read();
for(int i = ; i <= n; i++){
int a = read(), b = read();
root = Insert(root, b, a);
}
root->print();
return ;
}

最新文章

  1. FrozenUI - 专注于移动web的UI框架
  2. QA is more than Testing
  3. JS中的事件类型和事件属性的基础知识
  4. PL/SQL Developer连接本地64位Oracle数据库
  5. TFSF边界条件
  6. script的defer和async
  7. AngularJS:实现动态添加输入控件功能(转)
  8. Jedis操作redis(转)
  9. 百度JS模板引擎 baiduTemplate 1.0.6 版
  10. 在IE6里面当元素浮动后再设置margin那么就会产生双倍边距
  11. dubbo源码分析(一)
  12. maven私服nexus搭建(windows)
  13. 软件工程个人第二小项目——wc
  14. unix网络编程第2版(卷1)_第6章_同步_异步
  15. python webdriver环境搭建
  16. 《java入门第一季》之面向对象多态面试题(多态收尾)
  17. java获取一个月的天数
  18. Java String 常量池理解
  19. JS /javascript 解除网页屏蔽右键(无法复制)的代码
  20. jQuery 初知

热门文章

  1. 截止频率-3db
  2. DIV+CSS学习笔记
  3. HttpWatch--time chart分析
  4. 洛谷——P1022 计算器的改良
  5. js进阶 12-8 如何知道鼠标和键盘当前操作的是哪个键
  6. MacBook Touch Bar 使用技巧
  7. Yii Framework2.0开发教程(1)配置环境及第一个应用HelloWorld
  8. jquery 无刷新上传的小function
  9. 【30.36%】【codeforces 740D】Alyona and a tree
  10. boost::any的一般使用方法