bzoj_2827_千山鸟飞绝

任意一个平衡树+hash表(map也可以)

平衡树每个节点维护:

当前子树 最大值、size   下传标记 最大值、sizemax

pushdown时候直接更新最后的ans就行了,不用记下来(我就是因为这个炸了...)

hash的话

map比较慢 hash表不用双取mod

 

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <map>
#define mem(a,b) memset(a,b,sizeof(a))
#define ll long long
#define ull unsigned long long
#define s(x) ((x)->s)
#define v(x) ((x)->v)
#define bv(x) ((x)->bv)
#define bs(x) ((x)->bs)
#define ls(x) ((x)->ch[0])
#define rs(x) ((x)->ch[1])
#define order(x) ((x)->order)
#define fix(x) ((x)->fix)
#define JI(x,y) (((ull)(x)*p+(y))*p)
using namespace std;
const int T_T=330006;
const int N=30006;
const int p=233333333;
const int mod=2333333;
inline int read()
{
    char q=getchar();int ans=0,flag=1;
    while(q<'0'||q>'9'){if(q=='-')flag=-1;q=getchar();}
    while(q>='0'&&q<='9'){ans=ans*10+q-'0';q=getchar();}
    return ans*flag;
}

int ansv[N],anss[N];
int n,cnt,m;
int w[N],pos[N];

struct treap
{
    int s,v;
    int bs,bv;
    int fix;
    int order;
    treap *ch[2];
    treap(int val,int sz,int oo)
    {
        v=val;s=sz;
        bs=0;bv=0;
        fix=rand();order=oo;
    }
}*null;

treap* newtreap(int val,int sz,int oo)
{
    treap *temp=new treap(val,sz,oo);
    ls(temp)=null;
    rs(temp)=null;
    return temp;
}

struct Treap
{
    treap *root;
    void pushup(treap *x)
    {
        if(x==null)
            return ;
        v(x)=max( max(v(ls(x)),v(rs(x))),w[order(x)] );
        s(x)=s(ls(x))+s(rs(x))+1;
    }
    void pushdown(treap *x)
    {
        if(x==null)
            return ;
        if(ls(x)!=null)
        {
            if(bv(x)) ansv[order(ls(x))]=max( ansv[order(ls(x))],bv(x) ),bv(ls(x))=max( bv(ls(x)),bv(x) );
            if(bs(x)) anss[order(ls(x))]=max( anss[order(ls(x))],bs(x) ),bs(ls(x))=max( bs(ls(x)),bs(x) );
        }
        if(rs(x)!=null)
        {
            if(bv(x)) ansv[order(rs(x))]=max( ansv[order(rs(x))],bv(x) ),bv(rs(x))=max( bv(rs(x)),bv(x) );
            if(bs(x)) anss[order(rs(x))]=max( anss[order(rs(x))],bs(x) ),bs(rs(x))=max( bs(rs(x)),bs(x) );
        }
        bv(x)=bs(x)=0;
    }
    void lturn(treap *&x)
    {
        treap *y=rs(x);
        pushdown(x);pushdown(y);
        rs(x)=ls(y);pushup(x);
        ls(y)=x;pushup(y);
        x=y;
    }
    void rturn(treap *&x)
    {
        treap *y=ls(x);
        pushdown(x);pushdown(y);
        ls(x)=rs(y);pushup(x);
        rs(y)=x;pushup(y);
        x=y;
    }
    void add(int oo,treap *&x)
    {
        if(x==null)
        {
            x=newtreap(w[oo],1,oo);
            return ;
        }
        pushdown(x);
        if(oo<order(x))
        {
            add(oo,ls(x));
            if(fix(ls(x))>fix(x))
                rturn(x);
        }
        else
        {
            add(oo,rs(x));
            if(fix(rs(x))>fix(x))
                lturn(x);
        }
        pushup(x);
    }
    void add(int oo)
    {
        if(root!=null)
        {
            ansv[oo]=max( ansv[oo],v(root) );
            anss[oo]=max( anss[oo],s(root) );

            ansv[order(root)]=max( ansv[order(root)],w[oo] );
            anss[order(root)]=max( anss[order(root)],s(root) );
            bv(root)=max( bv(root),w[oo] );
            bs(root)=max( bs(root),s(root) );
        }
        add(oo,root);
    }
    void del(int oo,treap *&x)
    {
        pushdown(x);
        if(oo==order(x))
        {
            if(ls(x)!=null&&rs(x)!=null)
            {
                if(fix(ls(x))>fix(rs(x)))
                {
                    rturn(x);
                    del(oo,rs(x));
                }
                else
                {
                    lturn(x);
                    del(oo,ls(x));
                }
            }
            else
            {
                treap *y=null;
                if(ls(x)!=null)
                    y=ls(x);
                else
                    y=rs(x);
                delete x;
                x=y;
            }
        }
        else
            if(oo<order(x))
                del(oo,ls(x));
        else
            del(oo,rs(x));
        pushup(x);
    }
    void del(int oo)
    {
        del(oo,root);
    }
    void dfs(treap *&x)
    {
        if(x==null)
            return ;
        pushdown(x);
        dfs(ls(x));
        dfs(rs(x));
    }
    void dfs()
    {
        dfs(root);
    }
}T[T_T];

void DFS()
{
    for(int i=1;i<=cnt;++i)
        T[i].dfs();
}

/*struct MAP
{
    int x,y;
    bool friend operator < (MAP a,MAP b)
    {
        if(a.x==b.x)
            return a.y<b.y;
        return a.x<b.x;
    }
};

map<MAP,int> mp;*/

struct Hash
{
    struct hash
    {
        int order,next;
        ull w;
    }a1[mod];
    int first[mod],e;
    void clear()
    {
        mem(first,-1);
        e=0;
        mem(a1,0);
    }
    int add(ull w)
    {
        int tt=w%mod;
        for(int i=first[tt];i!=-1;i=a1[i].next)
            if(a1[i].w==w)
                return a1[i].order;
        a1[e].order=++cnt;
        a1[e].w=w;
        a1[e].next=first[tt];
        first[tt]=e++;
        return cnt;
    }
}H;

void out11()
{
    printf("cnt=%d\n",cnt);
    for(int i=1;i<=n;++i)
        printf("%d ",ansv[i]);
    printf("\n");
    for(int i=1;i<=n;++i)
        printf("%d ",anss[i]);
    printf("\n");
}

int main(){

    //freopen("in.in","r",stdin);

    H.clear();

    null=new treap(0,0,0);
    ls(null)=NULL;
    rs(null)=NULL;

    for(int i=0;i<T_T;++i)
        T[i].root=null;

    n=read();
    int temp,tin1,tin2,tin3;
    for(int i=1;i<=n;++i)
    {
        tin1=read();tin2=read();tin3=read();
        w[i]=tin1;
        temp=H.add( JI(tin2,tin3) );
        pos[i]=temp;
        T[temp].add(i);
    }
    m=read();
    for(int i=1;i<=m;++i)
    {
        tin1=read();tin2=read();tin3=read();

        temp=H.add( JI(tin2,tin3) );
        T[pos[tin1]].del(tin1);
        pos[tin1]=temp;
        T[temp].add(tin1);

        //printf("i=%d\n",i);
        //DFS();
        //out11();
    }
    DFS();
    for(int i=1;i<=n;++i)
        printf("%lld\n",(ll)ansv[i]*anss[i]);
}
bzoj_2827

 

posted @ 2017-09-26 18:45  A_LEAF  阅读(140)  评论(0编辑  收藏  举报