POJ 2828 Buy Tickets

来源:互联网 发布:linux telnet命令 编辑:程序博客网 时间:2024/06/06 02:04
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
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.


<pre name="code" class="cpp">#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <cmath>#define MAXN 200010using namespace std;struct point{    int index;    int data;}p[200010];int vis[200010];struct node{    int l,r;    int sum;}a[MAXN<<2];void pushup(int index){    a[index].sum = a[index<<1].sum+a[index<<1|1].sum;}void build(int l,int r,int index){    a[index].l = l;    a[index].r = r;    if(l == r){        a[index].sum = 1;        return ;    }    int mid = (l+r)>>1;    build(l,mid,index<<1);    build(mid+1,r,index<<1|1);    pushup(index);}void updata(int pos,int index,int val){    if(a[index].l == a[index].r){        a[index].sum = 0;        vis[a[index].l] = val;        return ;    }    //判断位置    if(pos <= a[index<<1].sum){        updata(pos,index<<1,val);    }    else{        updata(pos-a[index<<1].sum,index<<1|1,val);    }    pushup(index);}int main(){    int n;    while(~scanf("%d",&n)){        for(int i = 0; i < n; i++){            scanf("%d %d",&p[i].index,&p[i].data);            p[i].index++;        }        build(1,n,1);        for(int i = n-1; i >= 0; i--){            //从后向前;因为在后面的位置是确定的            updata(p[i].index,1,p[i].data);        }        for(int i = 1; i <= n; i++){            printf("%d ",vis[i]);            /*            if(i == n)            printf("\n");            else            printf(" ");            超时            */        }        printf("\n");    }    return 0;}





0 0
原创粉丝点击