POJ 2828Buy Tickets(线段树的单点维护)

Buy Tickets

Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 20462   Accepted: 10096

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

解题思路

一个不错的排队问题,可通过从后面开始排队来进行巧妙地排队解决,假设后面的人都已经站在正确的位置上了,那么到那个人站的时候,现在的位置上已经都是后面的那些人了,只要数pos个空格,那那个人站的位置能确定了。确定之后就可以求下一个了,所以这个前提和结论都成立了。所以我们只要从后面人站起,数pos个空格站上去就行了。 具体实现看代码吧.

PS:

G++  TLE, C++ AC.

AC代码:

#include<cstring>
#include<string>
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
const int maxsize = 200002;
int cnt,n,sum[maxsize*4],ans[maxsize*4];
struct node
{
    int l,r;
} tree[maxsize*4];
void build(int l,int r,int root)
{
    if(l==r)
    {
        sum[root]=1;
        return ;
    }
    int mid=(l+r)/2;
    build(lson);
    build(rson);
    sum[root]=sum[root*2]+sum[root*2+1];
}
void Update(int pos,int b,int l,int r,int root)
{
    if(l==r)
    {
        if(sum[root])
        {
            sum[root]=0;
            ans[root]=b;
        }
        return ;
    }
    int mid=(l+r)/2;
    if(pos<=sum[root*2])  Update(pos,b,lson);  //前面的话就霸占
    else    Update(pos-sum[root*2],b,rson); //后面的话就往后滚
    sum[root]=sum[root*2]+sum[root*2+1];
}
void Printf(int l,int r,int root)
{
    if(l==r)
    {
        cnt++;
        printf("%d%c",ans[root],cnt==n?‘\n‘:‘ ‘);
        return ;
    }
    int mid=(l+r)/2;
    Printf(lson);
    Printf(rson);
}
int main()
{
    ios::sync_with_stdio(false);
    while(cin>>n&&n)  //(cin>>n,n)就TLE,无语
    {
        cnt=0;
        build(1,n,1);
        for(int i=1; i<=n; i++)
        {
            scanf("%d %d",&tree[i].l,&tree[i].r);
            tree[i].l++;
        }
        ///pos记得加1
        for(int i=n; i>=1; i--)   Update(tree[i].l,tree[i].r,1,n,1);
        Printf(1,n,1);
    }
    return 0;
}

 

时间: 2024-11-05 10:00:30

POJ 2828Buy Tickets(线段树的单点维护)的相关文章

[POJ2828]Buy Tickets(线段树,单点更新,二分,逆序)

题目链接:http://poj.org/problem?id=2828 由于最后一个人的位置一定是不会变的,所以我们倒着做,先插入最后一个人. 我们每次处理的时候,由于已经知道了这个人的位置k,这个位置表明,在他之前一定有k个空位,于是将他插在第k+1个位置上.我们可以在线段树上直接二分,根据这个位置,假如这个位置在左子树上,那么一直向左走,否则要减掉左子树剩下的位置后再向右边走,同时更新沿途非叶节点的空闲位置数量(减一),手工画一下图就知道了. 1 /* 2 ━━━━━┒ギリギリ♂ eye!

POJ训练计划2828_Buy Tickets(线段树/单点更新)

解题报告 题意: 插队完的顺序. 思路: 倒着处理数据,第i个人占据第j=pos[i]+1个的空位. 线段树维护区间空位信息. #include <iostream> #include <cstdio> #include <cstring> using namespace std; struct node { int x,v; } num[201000]; int sum[1000000],ans[201000]; void cbtree(int rt,int l,in

poj-----(2828)Buy Tickets(线段树单点更新)

Buy Tickets Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 12930   Accepted: 6412 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 wa

POJ 2828 Buy Tickets (线段树)

题目大意: 排队有人插队,每一次都插到第 i 个人的后面. 最后输出顺序. 思路分析: 你会发现,如果反向处理的话,你就知道这个人是第几个了. 那么问题一下子就简化了. 就是在线段树上找第几个空位置就行了. #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #include <vector> #define lson num<<1

POJ 2828 Buy Tickets 线段树解法

此题应用线段树的方法非常巧妙.没做过真的难想得出是这么想的. 是一个逆向思维的运用. 其实一看到这道题目我就想到要运用逆向思维的了,但是就是没那么容易想通的. 思路: 1 要从后面往前更新线段树 2 线段树记录的是当前区间还剩下多少个记录空间 3 因为后面的插入会使得前面的插入往后退,那么前面插入的下标如果大于前面可以插入的空间,就需要往后退了. 好难理解的操作.仔细观察一下下面update这个函数吧. 二分地去查找适合的插入位置,把插入操作时间效率提高到 O(lgn),如果使用一般方法插入操作

POJ训练计划2299_Ultra-QuickSort(线段树/单点更新)

解题报告 题意: 求逆序数. 思路: 线段树离散化处理. #include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #define LL long long using namespace std; LL sum[2001000],num[501000],_hash[501000]; void push_up(int rt) { sum[rt]=sum[rt*2

POJ 3264-Balanced Lineup(线段树:单点更新+区间查询)

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34522   Accepted: 16224 Case Time Limit: 2000MS Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer Joh

Buy Tickets POJ - 2828 思维+线段树

Buy Tickets POJ - 2828 思维+线段树 题意 是说有n个人买票,但是呢这n个人都会去插队,问最后的队列是什么情况.插队的输入是两个数,第一个是前面有多少人,第二个是这个人的编号,最后输出编号就好了. 解题思路 这个题要倒着处理,因为最后一个人插队完成后,别人就不能影响他了.他前面有n个人,那么他就是n+1号位置上,这样来的话,我们只需要知道那个位置,他前面有n个人就行.默认每个位置都没有人. 详细看代码. 代码实现 #include<cstdio> #include<

POJ 2828Buy Tickets

POJ 2828 题目大意是说有n个插入操作,每次把B插入到位置A,原来A以后的全部往后移动1,球最后的序列 tree里保存的应该是这整个区间还有多扫个位置可以插入数据,那么线段树里从后往前扫描依次插入数据 比如现在吧B插入到A位置,如果整个区间左侧还有<A个位置可以插入数据,那么就只能将其放到整个区间的右侧,递归下去就可以了 1 void update(int k, int L, int R, int x) 2 { 3 if(L == R) { pre[k] = r; ans[L] = val