http://poj.org/problem?id=2828

一开始敲了个splay,直接模拟。

tle了。。

常数太大。。

好吧,说是用线段树。。

而且思想很拽。。

(貌似很久以前写过貌似的,,)

我们线段树维护的区间不再是人了。。

而是这个区间剩余的的座位。。

比如我现在要坐第一张,但是人已经坐了,即这个区间已经没有位置了。。那就要往后坐。

所以我们逆序添加,,因为后来人插队前边人管不着。。。

所以后来人一定是先定座位的。。

每一次维护这个座位区间。。

如果左边这个区间座位比我要坐的座位号要多(即我要坐的这个座位前边有空位(包括这个))

那么我就往前坐,否则向后坐。。

splay:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; }const int N=200005;
int n;
struct node* null;
struct node {int key, s;node *ch[2], *fa;node(const int &_k=0, const int &_s=1) : key(_k), s(_s) { ch[0]=ch[1]=fa=null; }inline void setc(node* c, const bool d) { ch[d]=c; c->fa=this; }inline int d() { return fa->ch[1]==this; }inline void pushup() { s=1+ch[0]->s+ch[1]->s; }
}*root;
inline void rot(node* r) {node* fa=r->fa; bool d=r->d();fa->fa->setc(r, fa->d());fa->setc(r->ch[!d], d);r->setc(fa, !d);fa->pushup();if(fa==root) root=r;
}
inline void splay(node* r, node* fa) {while(r->fa!=fa) if(r->fa->fa==fa) rot(r);else r->d()==r->fa->d()?(rot(r->fa), rot(r)):(rot(r), rot(r));r->pushup();
}
inline node* select(node* r, const int &k) {if(r==null) return null;int s=r->ch[0]->s+1;if(s==k) return r;if(s>k) return select(r->ch[0], k);else return select(r->ch[1], k-s);
}
void getans(node* r) {if(r==null) return;getans(r->ch[0]);printf("%d ", r->key);getans(r->ch[1]);delete r;
}
int main() {null=new node(0, 0); null->ch[0]=null->ch[1]=null->fa=null;int pos, id;while(~scanf("%d", &n)) {root=null;rep(i, n) {read(pos); read(id);++pos;node* c=new node(id);if(root==null) root=c;else if(pos>root->s) {splay(select(root, root->s), null);root->setc(c, 1);splay(c, null);}else {splay(select(root, pos), null);c->setc(root->ch[0], 0);c->pushup();root->setc(c, 0);splay(c, null);}}getans(root);puts("");}return 0;
}

线段树:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; }
#define lc x<<1
#define rc x<<1|1
#define lson l, m, lc
#define rson m+1, r, rc
#define MID (l+r)>>1
const int N=200005;
int n, sum[N*10], a[N], b[N], ans[N];
inline void pushup(const int &x) { sum[x]=sum[lc]+sum[rc]; }
void build(const int &l, const int &r, const int &x) {if(l==r) { sum[x]=1; return; }int m=MID;build(lson); build(rson);pushup(x);
}
void update(const int &l, const int &r, const int &x, const int &id, const int &s) {if(l==r) { ans[l]=id; sum[x]=0; return; }int m=MID;if(sum[lc]>=s) update(lson, id, s);else update(rson, id, s-sum[lc]);pushup(x);
}
int main() {while(~scanf("%d", &n)) {build(1, N, 1);rep(i, n) { read(a[i]); read(b[i]); }for3(i, n-1, 0) update(1, N, 1, b[i], a[i]+1);for1(i, 1, n) printf("%d ", ans[i]);puts("");}return 0;
}


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.

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

Hint

The figure below shows how the Little Cat found out the final order of people in the queue described in the first test case of the sample input.

Source

POJ Monthly--2006.05.28, Zhu, Zeyuan

【POJ】2828 Buy Tickets(线段树+特殊的技巧/splay)相关推荐

  1. POJ 2828 Buy Tickets 线段树

    题目: http://poj.org/problem?id=2828 很巧妙的题,逆序插入线段树,这样元素不用移动.用二叉排序树也能过. 1 #include <stdio.h> 2 #i ...

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

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

  3. POJ - 2828 Buy Tickets(线段树+思维/Splay+模拟)

    题目链接:点击查看 题目大意:给出n个人,一个队列,一开始队列是空的,每个人加入的时候都会插入到指定位置pos的后面,即插队,问最后队列的排列情况是怎么样的 题目分析:一开始很难想到是线段树的题目,毕 ...

  4. poj 2828 Buy Tickets

    http://poj.org/problem?id=2828 题意:在一个队列里,有人依次向第pos个位置插入,求最后的序列: 思路:用线段树存储区间内剩余的没有被占的位置,注意插入的时候要从后向前插 ...

  5. #树状数组#poj 2828 Buy Tickets

    题目 输入插队的人和插队的位置,求最终所有人的位置. 分析 树状数组,从后往前(倒推),然后就是要让在前面的次序-1. 代码 #include <cstdio> #include < ...

  6. POJ 2828. Buy Tickets

    链接 http://poj.org/problem?id=2828 题意 有 NNN 个人排队,每一个人都有一个权值 valvalval ,每一个人都会按顺序插入到当前队伍的某一个位置 posposp ...

  7. POJ 3667 Hotel(线段树)

    POJ 3667 Hotel 题目链接 题意:有n个房间,如今有两个操作 1.找到连续长度a的空房间.入住,要尽量靠左边,假设有输出最左边的房间标号,假设没有输出0 2.清空[a, a + b - 1 ...

  8. poj 2892---Tunnel Warfare(线段树单点更新、区间合并)

    题目链接 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensiv ...

  9. poj/OpenJ_Bailian - 2528 离散化+线段树

    传送门:http://bailian.openjudge.cn/practice/2528?lang=en_US //http://poj.org/problem?id=2528 题意: 给你n长海报 ...

最新文章

  1. 根据数组中对象的属性值排序倒叙
  2. Redis:master/slave、sentinel、Cluster简单总结
  3. [HDU 1015] Safecracker
  4. JAVA 编程开发入门-张晨光-专题视频课程
  5. Python绘制雷达图展示学生各科考试成绩
  6. Mac 升级 PHP 7
  7. 入门学习asp.net mvc
  8. zeptojs库解读1之整体框架
  9. 如何成为一名游戏设计师
  10. tablayout 增加数字小标_Android中TabLayout添加小红点的示例代码
  11. python123 测验三_作业要求 20190919-3 效能分析
  12. MATLAB自定义拟合函数
  13. Harbor镜像清理
  14. 【codevs2853】方格游戏 DP
  15. mybatis入门笔记(一)
  16. Java 导出CSV文件及实现web下载CSV
  17. Excel常用公式大全
  18. Flink-clickhousesink
  19. python程序设计江红答案_python程序设计江红上机答案
  20. 奥鹏教育微学吧JAVA答案_西交20秋《Java语言》在线作业【标准答案】

热门文章

  1. mysql proxy性能差_两种MySQL-Proxy架构的测试对比记录
  2. charles 安装 ssl_前端开发如何使用抓包工具 charles
  3. c语言编程高价是啥,有哪位高手可以帮我做几道c语言编程,有钱的呀,价格可以商量...
  4. java把字符串变代码,Java/javaScript将字符串转变成可执行的语句
  5. java公平所与非公平所_一张图读懂Java非公平锁与公平锁
  6. golag mysql_golang连接mysql操作示例增删改查
  7. golang连接postgresql too many client_MySQL和PostgreSQL压测性能对比
  8. 怎么理解java面向对象_Java 面向对象理解?
  9. 模6计数器以及模10计数器(Verilog HDL语言设计)(Modelsim仿真与ISE综合)
  10. Spartan-6的存储元件、多路复用器、快速先行进位逻辑、算术逻辑