• P2146 [NOI2015]软件包管理器


    题目大意:1、求1条链点权为1的个数,并将路径上所有点权赋0,2、求节点u子树中点权为0的个数,并把子树点权赋1。

    思路:一道裸模板,有个小技巧,我们可以在跳链时边跳边把权值赋0,这样既不会影响答案,还能少些几个函数。 而这题我调了2个多小时,在一番对拍下,我终于发现,我树链剖分的模板打错了!!!(怪不得我树剖跑那么慢。在求重儿子时,nmax不能设成全局变量!!!血一般的教训emm;

    代码如下:

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<algorithm>
    #include<cstring>
    #include<cstdlib>
    using namespace std;
    const int MaxN=200000;
    struct edge{
        int to,next;
    }e[MaxN*2];
    struct tree{
        int lb,rb,add,sum;
    }tr[MaxN*4];
    int n,q,last[MaxN],tot;
    int dis[MaxN],fa[MaxN],son[MaxN],dfn[MaxN],top[MaxN],siz[MaxN],id,namx;
    void add_edge(int x,int y){
        e[++tot].to=y;e[tot].next=last[x];last[x]=tot;
        return;
    }
    void dfs1(int x,int f){
        int nmax=-0x3f3f3f3f;//!!!2小时啊啊啊啊啊
        siz[x]=1;dis[x]=dis[f]+1;fa[x]=f;
        for(int i=last[x];i;i=e[i].next){
            int u=e[i].to;
            if(u==f) continue;
            dfs1(u,x);siz[x]+=siz[u];
            if(nmax<siz[u]) nmax=siz[u],son[x]=u;
        }
        return;
    }
    void dfs2(int x,int tp){
        dfn[x]=++id;top[x]=tp;
        if(!son[x]) return;dfs2(son[x],tp);
        for(int i=last[x];i;i=e[i].next){
            int u=e[i].to;
            if(u==fa[x]||u==son[x]) continue;//!
            dfs2(u,u);
        }
        return;
    }
    void downlag(int x){
        if(tr[x].add==-1) return;
        tr[x<<1].sum=tr[x].add*(tr[x<<1].rb-tr[x<<1].lb+1);tr[x<<1|1].sum=(tr[x<<1|1].rb-tr[x<<1|1].lb+1)*tr[x].add;
        tr[x<<1].add=tr[x<<1|1].add=tr[x].add;tr[x].add=-1;return;
    }
    void build(int now,int l,int r){
        tr[now].lb=l;tr[now].rb=r;tr[now].add=-1;
        if(l==r){tr[now].sum=1;return;}
        int mid=(l+r)>>1;
        build(now<<1,l,mid);build(now<<1|1,mid+1,r);
        tr[now].sum=tr[now<<1].sum+tr[now<<1|1].sum;
        return;
    }
    void add(int now,int l,int r,int w){
        if(tr[now].lb>=l&&tr[now].rb<=r){tr[now].sum=w*(tr[now].rb-tr[now].lb+1);tr[now].add=w;return;}
        downlag(now);
        int mid=(tr[now].lb+tr[now].rb)>>1;
        if(mid>=l) add(now<<1,l,r,w);if(mid<r) add(now<<1|1,l,r,w);
        tr[now].sum=tr[now<<1].sum+tr[now<<1|1].sum;
        return;
    }
    int query(int now,int l,int r){
        if(tr[now].lb>=l&&tr[now].rb<=r) return tr[now].sum;
        downlag(now);
        int mid=(tr[now].lb+tr[now].rb)>>1,ans=0;
        if(mid>=l) ans+=query(now<<1,l,r);if(mid<r) ans+=query(now<<1|1,l,r);
        return ans;
    }
    int wrk(int x){
        int ans=0;
        while(top[x]!=1){
            ans+=query(1,dfn[top[x]],dfn[x]);
            add(1,dfn[top[x]],dfn[x],0);
            x=fa[top[x]];
        }
        ans+=query(1,dfn[1],dfn[x]);add(1,dfn[1],dfn[x],0);
        return ans;
    }
    int main(){
        char c[10];int x,y;
        scanf("%d",&n);
        for(int i=2;i<=n;++i) scanf("%d",&x),++x,add_edge(x,i),add_edge(i,x);
        scanf("%d",&q);
        dfs1(1,0);dfs2(1,1);build(1,1,n);
        while(q--){
            scanf("%s",c);
            if(c[0]=='i'){
                scanf("%d",&x);printf("%d
    ",wrk(x+1));
            }
            if(c[0]=='u'){
                scanf("%d",&x);
                printf("%d
    ",siz[x+1]-query(1,dfn[x+1],dfn[x+1]+siz[x+1]-1));
                add(1,dfn[x+1],dfn[x+1]+siz[x+1]-1,1);
            }
        }
    }
  • 相关阅读:
    “There appears to be trouble with your network connection. Retrying”
    Nignx 处理异常操作流程
    "...do not match previously installed version; ignoring!"
    文档对象模型(DOM)
    Parsing error: Expression expected.
    让人抓狂的缩进冲突(eslint)
    C#中属性PropertyInfo的使用
    C#调用WebService服务(动态调用)
    pdf转word工具
    PASS系统应用技术手册
  • 原文地址:https://www.cnblogs.com/X-rice/p/11219140.html
Copyright © 2020-2023  润新知