• 【BZOJ4003】城池攻占(JLOI2015)-左偏树


    测试地址:城池攻占
    做法:本题需要用到左偏树。
    如果没有修改操作,题目中的要求很显然可以用树上合并左偏树来在O(nlogn)时间内解决。但是有了修改操作我们要怎么办呢?
    考虑左偏树的各种操作,它不会像splay一样往上或往下转,而且操作都是自下而上的,因此左偏树有类似线段树的性质,可以在上面像线段树一样处理标记。处理的方法就十分模板了,就不详细说了。这样我们就解决了这一题,时间复杂度为O(nlogn)
    以下是本人代码:

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int N=300010;
    int n,m,first[N]={0},tot=0;
    int pre[N]={0},dep[N];
    int rt[N]={0},ch[N][2]={0},dis[N]={0};
    ll val[N]={0},taga[N]={0},tagb[N];
    ll h[N],V[N],c[N];
    int ans[N]={0},stp[N];
    bool a[N];
    struct edge
    {
        int v,next;
    }e[N];
    
    void insert(int a,int b)
    {
        e[++tot].v=b;
        e[tot].next=first[a];
        first[a]=tot;
    }
    
    void dfs(int v)
    {
        dep[v]=dep[pre[v]]+1;
        for(int i=first[v];i;i=e[i].next)
            dfs(e[i].v);
    }
    
    void update(int v,ll a,ll b)
    {
        val[v]=a*val[v]+b;
        taga[v]=a*taga[v];
        tagb[v]=a*tagb[v]+b;
    }
    
    void pushdown(int v)
    {
        if (ch[v][0]) update(ch[v][0],taga[v],tagb[v]);
        if (ch[v][1]) update(ch[v][1],taga[v],tagb[v]);
        taga[v]=1ll,tagb[v]=0;
    }
    
    void pushup(int v)
    {
        if (dis[ch[v][0]]<dis[ch[v][1]])
            swap(ch[v][0],ch[v][1]);
        dis[v]=dis[ch[v][1]]+1;
    }
    
    int merge(int x,int y)
    {
        if (!x) {dis[y]=1;return y;}
        if (!y) {dis[x]=1;return x;}
        pushdown(x),pushdown(y);
        if (val[x]>val[y]) swap(x,y);
        ch[x][1]=merge(ch[x][1],y);
        pushup(x);
        return x;
    }
    
    void solve(int v)
    {
        for(int i=first[v];i;i=e[i].next)
        {
            solve(e[i].v);
            rt[v]=merge(rt[v],rt[e[i].v]);
        }
        while(rt[v]&&val[rt[v]]<h[v])
        {
            stp[rt[v]]=v;
            ans[v]++;
            pushdown(rt[v]);
            rt[v]=merge(ch[rt[v]][0],ch[rt[v]][1]);
        }
        if (!a[v]) update(rt[v],1ll,V[v]);
        else update(rt[v],V[v],0);
    }
    
    int main()
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
            scanf("%lld",&h[i]);
        for(int i=2;i<=n;i++)
        {
            scanf("%d%d%lld",&pre[i],&a[i],&V[i]);
            insert(pre[i],i);
        }
        dfs(1);
        for(int i=1;i<=m;i++)
        {
            ll s;
            scanf("%lld%d",&s,&c[i]);
            val[i]=s,taga[i]=1ll,tagb[i]=0,dis[i]=1;
            rt[c[i]]=merge(rt[c[i]],i);
        }
    
        solve(1);
        for(int i=1;i<=n;i++)
            printf("%d
    ",ans[i]);
        for(int i=1;i<=m;i++)
            printf("%d
    ",dep[c[i]]-dep[stp[i]]);
    
        return 0;
    }
  • 相关阅读:
    浅谈块元素绝对定位的margin属性
    因浏览器而异的空白节点(js清除空白节点)
    区分中英文字符的两种方法
    js中 visibility和display的区别 js中 visibility和display的区别
    观察者模式
    单例模式
    mongodb
    javascript的加载、解析、执行对浏览器渲染的影响
    EM算法原理
    YARN
  • 原文地址:https://www.cnblogs.com/Maxwei-wzj/p/9793275.html
Copyright © 2020-2023  润新知