POJ 2828 排队插队【线段树】题解

桃扇骨 2021-07-24 19:50 461阅读 0赞

目录

      • 1.题目
      • 2.代码

1.题目

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.
在这里插入图片描述

2.代码

  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include<iostream>
  3. #include<cstdio>
  4. #include<algorithm>
  5. #define lson l,m,rt<<1
  6. #define rson m+1,r,rt<<1|1
  7. using namespace std;
  8. const int maxn = 2e5 + 10;
  9. int sum[maxn << 2],val[maxn <<2 ];
  10. struct Node
  11. {
  12. int p;
  13. int num;
  14. }a[maxn<<2];
  15. void PushUP(int rt)
  16. {
  17. sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
  18. }
  19. void build(int l, int r, int rt)
  20. {
  21. if (l == r)
  22. {
  23. sum[rt] = 1; ///每个叶节点有一个空位
  24. return;
  25. }
  26. int m = (l + r) >> 1;
  27. build(lson);
  28. build(rson);
  29. PushUP(rt);
  30. }
  31. void query(int p,int num, int l, int r, int rt)
  32. {
  33. if (l == r)
  34. {
  35. sum[rt]--;
  36. val[l] = num;
  37. return;
  38. }
  39. int m = (l + r) >> 1;
  40. if (sum[rt << 1] >= p) query(p, num, lson);
  41. else query(p-sum[rt<<1], num, rson);
  42. PushUP(rt);
  43. }
  44. int main()
  45. {
  46. int n;
  47. while (scanf("%d", &n) != EOF)
  48. {
  49. for (int i = 1; i <= n; i++)
  50. {
  51. scanf("%d%d", &a[i].p, &a[i].num);
  52. a[i].p++;
  53. }
  54. build(1, n, 1);
  55. for (int i = n ; i >= 1; i--)
  56. {
  57. query(a[i].p, a[i].num, 1, n, 1);
  58. }
  59. for (int i = 1; i <= n; i++)
  60. {
  61. printf("%d ", val[i]);
  62. }
  63. printf("\n");
  64. }
  65. return 0;
  66. }

发表评论

表情:
评论列表 (有 0 条评论,461人围观)

还没有评论,来说两句吧...

相关阅读

    相关 POJ 2528 线段+离散化

        [POJ 2528][]      关键在于插入数据的顺序------从上往下依次插入每张海报,这样后插入的海报不可能覆盖先插入的海报,因此插入一张海报时,如果发现海

    相关 POJ2279 排队

    题意:《算法竞赛进阶指南》P265。 分析:《算法竞赛进阶指南》P265-266,除了还可以用“杨氏矩阵”和“勾长公式”进行计算。 代码: inclu