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

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

tle了。。

常数太大。。

好吧,说是用线段树。。

而且思想很拽。。

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

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

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

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

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

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

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

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

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

splay:

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cmath>
  4. #include <string>
  5. #include <iostream>
  6. #include <algorithm>
  7. using namespace std;
  8. #define rep(i, n) for(int i=0; i<(n); ++i)
  9. #define for1(i,a,n) for(int i=(a);i<=(n);++i)
  10. #define for2(i,a,n) for(int i=(a);i<(n);++i)
  11. #define for3(i,a,n) for(int i=(a);i>=(n);--i)
  12. #define for4(i,a,n) for(int i=(a);i>(n);--i)
  13. #define CC(i,a) memset(i,a,sizeof(i))
  14. #define read(a) a=getint()
  15. #define print(a) printf("%d", a)
  16. #define dbg(x) cout << #x << " = " << x << endl
  17. #define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
  18. 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; }
  19. inline const int max(const int &a, const int &b) { return a>b?a:b; }
  20. inline const int min(const int &a, const int &b) { return a<b?a:b; }
  21.  
  22. const int N=200005;
  23. int n;
  24. struct node* null;
  25. struct node {
  26. int key, s;
  27. node *ch[2], *fa;
  28. node(const int &_k=0, const int &_s=1) : key(_k), s(_s) { ch[0]=ch[1]=fa=null; }
  29. inline void setc(node* c, const bool d) { ch[d]=c; c->fa=this; }
  30. inline int d() { return fa->ch[1]==this; }
  31. inline void pushup() { s=1+ch[0]->s+ch[1]->s; }
  32. }*root;
  33. inline void rot(node* r) {
  34. node* fa=r->fa; bool d=r->d();
  35. fa->fa->setc(r, fa->d());
  36. fa->setc(r->ch[!d], d);
  37. r->setc(fa, !d);
  38. fa->pushup();
  39. if(fa==root) root=r;
  40. }
  41. inline void splay(node* r, node* fa) {
  42. while(r->fa!=fa)
  43. if(r->fa->fa==fa) rot(r);
  44. else r->d()==r->fa->d()?(rot(r->fa), rot(r)):(rot(r), rot(r));
  45. r->pushup();
  46. }
  47. inline node* select(node* r, const int &k) {
  48. if(r==null) return null;
  49. int s=r->ch[0]->s+1;
  50. if(s==k) return r;
  51. if(s>k) return select(r->ch[0], k);
  52. else return select(r->ch[1], k-s);
  53. }
  54. void getans(node* r) {
  55. if(r==null) return;
  56. getans(r->ch[0]);
  57. printf("%d ", r->key);
  58. getans(r->ch[1]);
  59. delete r;
  60. }
  61. int main() {
  62. null=new node(0, 0); null->ch[0]=null->ch[1]=null->fa=null;
  63. int pos, id;
  64. while(~scanf("%d", &n)) {
  65. root=null;
  66. rep(i, n) {
  67. read(pos); read(id);
  68. ++pos;
  69. node* c=new node(id);
  70. if(root==null) root=c;
  71. else if(pos>root->s) {
  72. splay(select(root, root->s), null);
  73. root->setc(c, 1);
  74. splay(c, null);
  75. }
  76. else {
  77. splay(select(root, pos), null);
  78. c->setc(root->ch[0], 0);
  79. c->pushup();
  80. root->setc(c, 0);
  81. splay(c, null);
  82. }
  83. }
  84. getans(root);
  85. puts("");
  86. }
  87. return 0;
  88. }

线段树:

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cmath>
  4. #include <string>
  5. #include <iostream>
  6. #include <algorithm>
  7. using namespace std;
  8. #define rep(i, n) for(int i=0; i<(n); ++i)
  9. #define for1(i,a,n) for(int i=(a);i<=(n);++i)
  10. #define for2(i,a,n) for(int i=(a);i<(n);++i)
  11. #define for3(i,a,n) for(int i=(a);i>=(n);--i)
  12. #define for4(i,a,n) for(int i=(a);i>(n);--i)
  13. #define CC(i,a) memset(i,a,sizeof(i))
  14. #define read(a) a=getint()
  15. #define print(a) printf("%d", a)
  16. #define dbg(x) cout << #x << " = " << x << endl
  17. #define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
  18. 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; }
  19. inline const int max(const int &a, const int &b) { return a>b?a:b; }
  20. inline const int min(const int &a, const int &b) { return a<b?a:b; }
  21. #define lc x<<1
  22. #define rc x<<1|1
  23. #define lson l, m, lc
  24. #define rson m+1, r, rc
  25. #define MID (l+r)>>1
  26. const int N=200005;
  27. int n, sum[N*10], a[N], b[N], ans[N];
  28. inline void pushup(const int &x) { sum[x]=sum[lc]+sum[rc]; }
  29. void build(const int &l, const int &r, const int &x) {
  30. if(l==r) { sum[x]=1; return; }
  31. int m=MID;
  32. build(lson); build(rson);
  33. pushup(x);
  34. }
  35. void update(const int &l, const int &r, const int &x, const int &id, const int &s) {
  36. if(l==r) { ans[l]=id; sum[x]=0; return; }
  37. int m=MID;
  38. if(sum[lc]>=s) update(lson, id, s);
  39. else update(rson, id, s-sum[lc]);
  40. pushup(x);
  41. }
  42. int main() {
  43. while(~scanf("%d", &n)) {
  44. build(1, N, 1);
  45. rep(i, n) { read(a[i]); read(b[i]); }
  46. for3(i, n-1, 0) update(1, N, 1, b[i], a[i]+1);
  47. for1(i, 1, n) printf("%d ", ans[i]);
  48. puts("");
  49. }
  50. return 0;
  51. }

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

  1. 4
  2. 0 77
  3. 1 51
  4. 1 33
  5. 2 69
  6. 4
  7. 0 20523
  8. 1 19243
  9. 1 3890
  10. 0 31492

Sample Output

  1. 77 33 69 51
  2. 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】2828 Buy Tickets(线段树+特殊的技巧/splay)的更多相关文章

  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. webservice远程调试开启

    在.NET 中已经默认将WEBSERVICE的远程调试功能关闭,有的时候我们需要远程调试程序的时候,就需要打开此功能我们只需在WEBSERVICE的项目的中添web.config的<system ...

  2. HTML表单元素Emil和密码

    <form action="" method="post" name="myform"><p>E-mail:< ...

  3. 用php实现遍历目录

    用php实现的遍历目录,只遍历第一层,如果制作在线文件管理器的话很管用,不同目录只加一个超链接就行了,然后给方法传递参数就行了,遍历目录的类如下: class Ergodic{ public func ...

  4. [Android Memory] App调试内存泄露之Context篇(上)

    转载自:http://www.cnblogs.com/qianxudetianxia/p/3645106.html Context作为最基本的上下文,承载着Activity,Service等最基本组件 ...

  5. 解决Unable to locate Kerberos realm

    在windows环境下 将服务器上的/etc/krb5.conf 复制到<jdk-home>/jre/lib/security

  6. opencv学习笔记(五)镜像对称

    opencv学习笔记(五)镜像对称 设图像的宽度为width,长度为height.(x,y)为变换后的坐标,(x0,y0)为原图像的坐标. 水平镜像变换: 代码实现: #include <ios ...

  7. 用scanf输入字符串

    char s[10];scanf("%s",s);改为scanf("%s",&s);也正确,为什么? 1 s与&s都表示地址,且值相等.2 s与 ...

  8. C语言 给字符数组赋值的方法

    typedef struct _tagTESTCHAR { char szTest[30];}TESTCHAR , *PTESTCHAR; int main(int argc, char* argv[ ...

  9. 移动App该如何保存用户密码(转)

    原文地址:http://blog.csdn.net/hengyunabc/article/details/34623957 移动App该如何保存用户密码? 这个实际上和桌面程序是一样的. 先看下一些软 ...

  10. 昨天用的流量有点多60M

    就是因为值班这里没有无线,然后自己又是受前几次的影响,没有收到微信,然后就看了热点,这是用的快的.