POJ 2828 (線段樹)

Buy Tickets
Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 6634   Accepted: 3186

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.


開始的時候一直糾結這個,雖開始就知道是線段樹但一直找不到下手點。。。。最後看題解才發現可以逆着來。。。


#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>

#define Mid(a) ((a)>>1)
#define R(a) (((a)<<1)+1)
#define L(a) ((a)<<1)
#define maxn 200010
#define clear(x) memset(x,0,sizeof(x))


struct Tree {
  int l,r,num,id;
} T[maxn*3];

int len,ans[maxn],a[maxn],b[maxn];

void Build(int l,int r,int n)
{
  int m = Mid(l+r);
  T[n].l = l;
  T[n].r = r;
  T[n].num = r - l + 1;
  T[n].id = 0;
  if (T[n].r == T[n].l) return ;
  Build(l,m,L(n));
  Build(m+1,r,R(n));
}

void del(int k,int n)
{
  if (T[n].l <= k && T[n].r >= k) T[n].num--;
  if (T[n].l == T[n].r) return ;
  int m = Mid(T[n].l + T[n].r);
  if (k <= m) del(k,L(n));
  else del(k,R(n));
}

void find(int k,int n,int id)
{
  T[n].num--;
  if (T[n].r == T[n].l) {
    T[n].id = id;
    //del(T[n].r,1);
    return ;
    }

  //int rm_l = T[L(n)].r - T[L(n)].l + 1 - T[L(n)].num;//已經佔了幾個人
  //printf("%d %d %d %d\n",k,T[L(n)].num,T[n].l,T[n].r);
  if (T[L(n)].num < k) find(k - T[L(n)].num,R(n),id);
    else find(k,L(n),id);
}

void vis(int n)
{
  //printf("%d %d %d %d %d\n",n,T[n].l,T[n].r,T[n].num,T[n].id);
  if (T[n].l == T[n].r) {ans[++len] = T[n].id;return ;}
  if ((T[L(n)].r - T[L(n)].l + 1 - T[L(n)].num) > 0) vis(L(n));
  if ((T[R(n)].r - T[R(n)].l + 1 - T[R(n)].num) > 0) vis(R(n));
}

int work()
{
  int n,i,j,temp;
  while (scanf("%d",&n) == EOF) return 0;
  len = 0;

  clear(T);
  clear(ans);

  Build(1,maxn,1);
  for(i = 1;i <= n;i++)
    scanf("%d%d",&a[i],&b[i]);
  for(i = n;i >= 1;i--)
    find(a[i]+1,1,b[i]);
  vis(1);
  for(i = 1;i < len;i++) printf("%d ",ans[i]);
  printf("%d\n",ans[len]);
  return 1;
}

int main()
{
  //freopen("xx.out","w",stdout);
  while (work()) ;
  return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章