天天看點

codeforces 38G Queue

http://www.elijahqi.win/2018/02/15/codeforces-38g-queue/

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

On a cold winter evening our hero Vasya stood in a railway queue to buy a ticket for Codeforces championship final. As it usually happens, the cashier said he was going to be away for 5 minutes and left for an hour. Then Vasya, not to get bored, started to analyze such a mechanism as a queue. The findings astonished Vasya.

Every man is characterized by two numbers: ai, which is the importance of his current task (the greater the number is, the more important the task is) and number ci, which is a picture of his conscience. Numbers ai form the permutation of numbers from 1 to n.

Let the queue consist of n - 1 people at the moment. Let’s look at the way the person who came number n behaves. First, he stands at the end of the queue and the does the following: if importance of the task ai of the man in front of him is less than an, they swap their places (it looks like this: the man number n asks the one before him: “Erm… Excuse me please but it’s very important for me… could you please let me move up the queue?”), then he again poses the question to the man in front of him and so on. But in case when ai is greater than an, moving up the queue stops. However, the man number n can perform the operation no more than cn times.

In our task let us suppose that by the moment when the man number n joins the queue, the process of swaps between n - 1 will have stopped. If the swap is possible it necessarily takes place.

Your task is to help Vasya model the described process and find the order in which the people will stand in queue when all the swaps stops.

Input

The first input line contains an integer n which is the number of people who has joined the queue (1 ≤ n ≤ 105). In the next n lines descriptions of the people are given in order of their coming — space-separated integers ai and ci (1 ≤ ai ≤ n, 0 ≤ ci ≤ n). Every description is located on s single line. All the ai’s are different.

Output

Output the permutation of numbers from 1 to n, which signifies the queue formed according to the above described rules, starting from the beginning to the end. In this succession the i-th number stands for the number of a person who will stand in line on the place number i after the swaps ends. People are numbered starting with 1 in the order in which they were given in the input. Separate numbers by a space.

Examples

Input

2

1 0

2 1

Output

2 1

Input

3

1 3

2 3

3 3

Output

3 2 1

Input

5

2 3

1 4

4 3

3 1

5 2

Output

3 1 5 4 2

昨天路途上又奔波了很久 還被拉去打球了 ..本來想偷懶的 這題直接看看題解得了 然後發現題解都看不懂了 有的題解說二分?自己yy了一下覺得不是很對啊 zhx巨佬的題解也看不太懂了 隻好半夜自己冷靜想一想怎麼搞

題意:按照題目給定的順序進人 每個人一開始站在隊伍的最末端 然後他有一個重要值a 有一個良心值c 即插隊次數(交換次數<=c 如果前面的那個人重要值小于目前這個 并且這個人良心值沒有變0 那麼他就會和前面一個人交換 直到 良心值為0或者是前一個人比他重要那麼停止交換

我的做法是 首先我先算出來我這個人最多可以和前面幾個人進行交換 然後把這段字尾在splay上轉出來 然後 同時在splay上維護一個最大值 然後直接取splay上找一下 第一個比我大的即可 如果沒有 那麼就把位置标記為我良心值能換到的最前面的地方 好直接把我這個人插進去即可

#include<cstdio>
#include<algorithm>
#define N 110000
using namespace std;
inline char gc(){
    static char now[<<],*S,*T;
    if (T==S) {T=(S=now)+fread(now,,<<,stdin);if (T==S) return EOF;}
    return *S++;
}
inline int read(){
    int x=,f=;char ch=gc();
    while(ch<'0'||ch>'9') {if (ch=='-') f=-;ch=gc();}
    while(ch<='9'&&ch>='0') x=x*+ch-'0',ch=gc();
    return x*f;
}
int fa[N],mx[N],c[N][],v[N],size[N],n,pos,cnt,root,id[N];
inline void update(int x){
    size[x]=size[c[x][]]+size[c[x][]]+;
    mx[x]=max(max(mx[c[x][]],mx[c[x][]]),v[x]);
}
inline void rotate(int x,int &tar){
    int y=fa[x],z=fa[y];
    if (y==tar) tar=x;else c[z][c[z][]==y]=x;
    int l=c[y][]==x,r=l^;
    fa[c[x][r]]=y;fa[y]=x;fa[x]=z;
    c[y][l]=c[x][r];c[x][r]=y;update(y);update(x);
}
inline void splay(int x,int &tar){
    while(x!=tar){
        int y=fa[x],z=fa[y];
        if (y!=tar){
            if(c[y][]==x^c[z][]==y) rotate(x,tar);else rotate(y,tar);
        }rotate(x,tar);
    }
}
inline int find(int x,int sz){
    int l=c[x][],r=c[x][];
    if (size[l]+==sz) return x;
    if (sz<=size[l]) return find(l,sz);else return find(r,sz-size[l]-);
}
inline void check(int x,int vv,int sz){
    if (!x) return;int l=c[x][],r=c[x][];
    if (mx[r]>vv) check(r,vv,sz+size[l]+);
    else if (v[x]>vv) pos=sz+size[l]+;
    else if (mx[l]>vv) check(l,vv,sz);
}
inline void print(int x){
    if (c[x][]) print(c[x][]);
    if (id[x]) printf("%d ",id[x]);
    if (c[x][]) print(c[x][]);
}
inline void print1(int x){
    if (c[x][]) print1(c[x][]);
    if (v[x]) printf("%d ",v[x]);
    if (c[x][]) print1(c[x][]);
}
int main(){
    freopen("cf.in","r",stdin);
    n=read();size[++cnt]=;root=cnt;
    c[root][]=++cnt;fa[cnt]=root;size[cnt]=;
    for (int i=;i<=n;++i){
        int a=read(),cc=read();
        pos=;int l=max(,i-cc);int xx=find(root,l),yy=find(root,i+);
        splay(yy,root);
        splay(xx,c[root][]);
        check(c[xx][],a,);++l;//print1(c[xx][1]);puts("as");
        if(pos) l=l+pos;xx=find(root,l-),yy=find(root,l);
        splay(yy,root);splay(xx,c[root][]);size[++cnt]=;fa[cnt]=xx;
        c[xx][]=cnt;v[cnt]=mx[cnt]=a;update(xx);update(root);id[cnt]=i;
    //  print1(root);puts("");puts("");
    }print(root);//print(root);puts("");puts("");
    return ;
}