zoukankan      html  css  js  c++  java
  • poj2828(Buy Tickets)线段树

    Time Limit: 4000MS   Memory Limit: 65536K
    Total Submissions: 11157   Accepted: 5450

    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 ≤ iN). 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

    思路:从后往前考虑,先确定最后一个人的位置,再倒数第二个,...,即可

     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <algorithm>
     4 #include <iomanip>
     5 #include <set>
     6 #include <map>
     7 #include <vector>
     8 #include <queue>
     9 #define N 200005
    10 using namespace std;
    11 int segtree[N << 2];
    12 int pos[N], val[N], res[N];
    13 void build(int l, int r, int p)
    14 {
    15     segtree[p] = r - l + 1;//l到r区间总共位置 
    16     if (l < r)
    17     {
    18         int mid = (l + r) >> 1, pp = p << 1;
    19         build(l, mid, pp);
    20         build(mid + 1, r, pp + 1);
    21     }
    22 }
    23 int query(int l, int r, int p, int v)
    24 {
    25     if (l == r)//找到所在位置 
    26     {
    27         segtree[p] = 0;
    28         return l;
    29     }
    30     else
    31     {
    32         int mid = (l + r) >> 1, pp = p << 1, ans;
    33         if (segtree[pp] >= v)//位置在左边 
    34             ans = query(l, mid, pp, v);
    35         else
    36             ans = query(mid + 1, r, pp + 1, v - segtree[pp]);//位置在右边,注意v-左边位置个数 
    37         segtree[p] = segtree[pp] + segtree[pp + 1];//向上更新 
    38         return ans;
    39     }
    40 } 
    41 int main()
    42 {
    43     int n, i;
    44     while (~scanf("%d", &n))
    45     {
    46         for (i = 1; i <= n; i++)
    47             scanf("%d%d", &pos[i], &val[i]);
    48         build(1, n, 1);
    49         for (i = n; i >= 1; i--)
    50         {
    51             res[query(1, n, 1, pos[i] + 1)] = val[i];
    52         }
    53         printf("%d", res[1]);
    54         for (i = 2; i <= n; i++)
    55             printf(" %d", res[i]);
    56         putchar('
    ');
    57     }
    58     return 0;
    59 }
  • 相关阅读:
    iOS Provision 要点记录
    (FIFO)有名管道在无亲缘进程间的通信
    uuid Makefile share
    浅谈C语言中的联合体
    消息队列 进程通信
    onvif makefile without share
    共享内存 进程通信
    共享内存(非map) 进程通信
    消息队列 两个进程
    linux 进程通信
  • 原文地址:https://www.cnblogs.com/jecyhw/p/3448216.html
Copyright © 2011-2022 走看看