POJ 2828 Buy Tickets (线段树 || 树状数组)

题目大意

一些小朋友在排队,每次来一个人,第i个人会插到第x个人的后面。权值为y。保证x∈[0,i-1]。

按照最后的队伍顺序,依次输出每个人的权值。

解题分析

好气吖。本来是在做splay练习,然后发现这道题用splay写T掉了,可能是我的splay常数太大了吧。要不要考虑去学一下自顶向下建树的splay,据说会快一点。

可以倒着考虑问题。如果倒着安排小朋友的队伍的话,就不用考虑插队的问题了。如果第i个人插到了第x个人的后面。

用线段树写的话,记录一下空格的数量,每次找个第x+1个空格的位置插入值。

用树状数组写的话,记录一下空格的数量,每次二分找出第x+1空格的位置插入值。

参考程序

Splay T掉了= =

#include <cstdio>
using namespace std;

class Splay_tree
{
private:
    struct node
    {
        int val,sz;
        node *l,*r,*f;
        node(int _val=-1,int _sz=1,node*_f=NULL,node*_l=NULL,node*_r=NULL):
        val(_val),sz(_sz),f(_f),l(_l),r(_r){}
    };
    node *rt;
    void del(node *x)
    {
        if (!x) return;
        del(x->l); del(x->r);
        delete x;
    }
    void pushup(node *x)
    {
        x->sz=1;
        if (x->l) x->sz += x->l->sz;
        if (x->r) x->sz += x->r->sz;    
    }
    void left(node *x,node *&rt)
    {
        node *y=x->f,*z=y->f;
        if (y==rt) rt=x; else if (y==z->l) z->l=x; else z->r=x;
        if (x->l) x->l->f=y; y->f=x; x->f=z;
        y->r=x->l; x->l=y;
        pushup(y); pushup(x);       
    }
    void right(node *x,node *&rt)
    {
        node *y=x->f,*z=y->f;
        if (y==rt) rt=x; else if (y==z->l) z->l=x; else z->r=x;
        if (x->r) x->r->f=y; y->f=x; x->f=z;
        y->l=x->r; x->r=y;
        pushup(y); pushup(x);       
    }
    void splay(node *x,node *&rt)
    {
        while (x!=rt)
        {
            node *y=x->f,*z=y->f;
            if (y==rt) if (x==y->l) right(x,rt); else left(x,rt);
            else if (y==z->l) if (x==y->l) {right(y,rt);right(x,rt);} else {left(x,rt);right(x,rt);}
                 else if (x==y->r) {left(y,rt);left(x,rt);} else {right(x,rt);left(x,rt);}
        }
    }
    void find(int rk,node *&rt)
    {
        node *x=rt;
        while ((x->l?x->l->sz+1:1)!=rk)
        {
            if (rk<=(x->l?x->l->sz:0)) x=x->l; else
            {
                rk-=(x->l?x->l->sz+1:1);
                x=x->r;
            }
        }
        splay(x,rt);
    }
    void visit(node *rt)
    {
        if (rt->l) visit(rt->l);
        if (~rt->val) printf("%d ",rt->val);
        if (rt->r) visit(rt->r);
    }
public:
    Splay_tree()
    {
        node *x=new node;
        node *y=new node;
        x->r=y; y->f=x; x->sz=2; 
        rt=x;
    }
    ~Splay_tree(){del(rt);}
    void insert(int rk,int val)
    {
        find(rk,rt);
        find(1,rt->r);
        rt->r->l=new node(val,1,rt->r);
        pushup(rt->r); pushup(rt);
    }
    void print(){visit(rt);printf("\n");}
};
int main()
{
    int n;
    while (~scanf("%d",&n))
    {
        Splay_tree T;
        for (int i=1;i<=n;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            T.insert(x+1,y);
        }
        T.print();
    }
}

线段树

#include <cstdio>
#include <iostream> 
using namespace std;
const int N=200008;
class Segment_tree
{
public:
    struct node
    {
        int val,space;  
    }a[N*4];
    void pushup(int rt)
    {
        a[rt].space=a[rt<<1].space+a[rt<<1|1].space;
    }
    void build(int l,int r,int rt)
    {
        a[rt].val=a[rt].space=0;
        if (l==r) 
        {
            a[rt].val=0; 
            a[rt].space=1;
            return;
        }
        int m=l+r>>1;
        build(l,m,rt<<1);
        build(m+1,r,rt<<1|1);
        pushup(rt);
    }
    void update(int k,int val,int l,int r,int rt)
    {
        if (l==r)
        {
            a[rt].space=0;
            a[rt].val=val;
            return;
        }
        int m=l+r>>1;
        if (k<=a[rt<<1].space) update(k,val,l,m,rt<<1);
        else update(k-a[rt<<1].space,val,m+1,r,rt<<1|1);
        pushup(rt);
    }
    void query(int l,int r,int rt)
    {
        if (l==r) 
        {
            printf("%d ",a[rt].val);
            return;
        }
        int m=l+r>>1;
        query(l,m,rt<<1);
        query(m+1,r,rt<<1|1);
    }
}T;
int x[N],y[N];
int main()
{
    int n;
    while (~scanf("%d",&n))
    {
        T.build(1,n,1);
        for (int i=1;i<=n;i++) scanf("%d%d",&x[i],&y[i]);
        for (int i=n;i>=1;i--) T.update(x[i]+1,y[i],1,n,1);
        T.query(1,n,1); printf("\n");
    }
}

树状数组

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int N=200008;
class Binary_index_tree
{
public:
    int a[N];
    void init(){memset(a,0,sizeof(a));}
    void add(int x,int y)
    {
        for (int i=x;i<N;i+=i & (-i)) a[i]+=y;
    }
    int sigma(int x)
    {
        int res=0;
        for (int i=x;i;i-=i & (-i)) res+=a[i];
        return res;
    }
}T;
int x[N],y[N],ans[N];
int main()
{
    int n;
    while (~scanf("%d",&n))
    {
        for (int i=1;i<=n;i++) scanf("%d%d",&x[i],&y[i]);
        T.init();
        for (int i=1;i<=n;i++) T.add(i,1);
        for (int i=n;i>=1;i--) 
        {
            int l=1,r=n,res=-1;
            while (l<=r)
            {
                int m=l+r>>1;
                int num=T.sigma(m);
                if (num==x[i]+1) res=m;
                if (num<x[i]+1) l=m+1; else r=m-1;
            }
            ans[res]=y[i];
            T.add(res,-1);
        }
        for (int i=1;i<=n;i++) printf("%d ",ans[i]); printf("\n");
    }
}

转载于:https://www.cnblogs.com/rpSebastian/p/6810046.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值