[BZOJ4034][HAOI2015]T2 做題筆記

·· / ·– ·· ·-·· ·-·· / ·–· · ·-· ··· ·· ··· - / ··- -· - ·· ·-·· / ·· / ·– ·· -·
題目來源http://www.lydsy.com/JudgeOnline/problem.php?id=4034
[BZOJ4196][NOI2015]軟件包管理器的既視感。。
這題是樹鏈剖分+dfs序比較裸的題目,唯一需要注意的就是要開long long

#include <cstdio>
#include <algorithm>
#define lch now<<1
#define rch now<<1|1
const int N=200000;
typedef long long ll;
using namespace std;
ll C[N];
int size[N],son[N],fa[N],dep[N];
int w[N],ed[N],top[N],id[N<<2],cnt=0;
int ver[N<<1],nxt[N<<1],head[N],tot=1;
int n,m;
struct Tree {
    ll sum,del;
}I[N<<2];
void add (int u,int v) {
    ver[++tot]=v;nxt[tot]=head[u];head[u]=tot;
}
void dfs_1 (int u,int from) {
    int v;size[u]=1;son[u]=0;fa[u]=from;
    for (int i=head[u];i;i=nxt[i]) {
        v=ver[i];
        if (v==fa[u]) continue;
        dep[v]=dep[u]+1;
        dfs_1(v,u); size[u]+=size[v];
        if (!son[u]||size[son[u]]<size[v]) son[u]=v; 
    }
}
void dfs_2 (int u,int st) {
    int v;w[u]=++cnt;top[u]=st;id[cnt]=u;
    if (son[u]) dfs_2(son[u],st);
    for (int i=head[u];i;i=nxt[i]) {
        v=ver[i];
        if (v==son[u]||v==fa[u]) continue;
        dfs_2(v,v);
    }
    ed[u]=cnt;
}
inline void pushup (int now) {
    I[now].sum=I[lch].sum+I[rch].sum;
}
inline void pushdown (int now,int l,int r) {
    if (I[now].del!=0) {
        int mid=(l+r)>>1;
        I[lch].del+=I[now].del;I[rch].del+=I[now].del;
        I[lch].sum+=I[now].del*(mid-l+1); I[rch].sum+=I[now].del*(r-mid);
        I[now].del=0;
    }
}
void build (int now,int l,int r) {
    if (l==r) {
        I[now].sum=C[id[l]];
        I[now].del=0;
        return ;
    }
    int mid=(l+r)>>1;
    build(lch,l,mid); build(rch,mid+1,r);
    pushup(now);
}
void change (int now,int l,int r,int x,int y,ll del) {
    if (x<=l&&r<=y) {
        I[now].del+=del;
        I[now].sum+=del*(r-l+1);
        return ;
    }
    int mid=(l+r)>>1;
    pushdown(now,l,r);
    if (x<=mid) change(lch,l,mid,x,y,del);
    if (y>mid) change(rch,mid+1,r,x,y,del);
    pushup(now);
}
ll query (int now,int l,int r,int x,int y) {
    if (x<=l&&r<=y) return I[now].sum;
    int mid=(l+r)>>1;ll ans=0;
    pushdown(now,l,r);
    if (x<=mid) ans+=query(lch,l,mid,x,y);
    if (y>mid) ans+=query(rch,mid+1,r,x,y);
    return ans;
}
ll getans (int x,int y) {
    ll ans=0;
    while (top[x]!=top[y]) {
        ans+=query(1,1,n,w[top[x]],w[x]);
        x=fa[top[x]];
    }
    ans+=query(1,1,n,w[y],w[x]);
    return ans;
}
int main () {
    int u,v,opt,x,y;
    scanf("%d%d",&n,&m);
    for (int i=1;i<=n;i++) scanf("%lld",&C[i]);
    for (int i=1;i<n;i++) {
        scanf("%d%d",&u,&v);
        add(u,v); add(v,u);
    }
    dfs_1(1,0); dfs_2(1,1);
    build(1,1,n);
    for (int i=1;i<=m;i++) {
        scanf("%d",&opt);
        if (opt==1) {
            scanf("%d%d",&x,&y);
            change(1,1,n,w[x],w[x],y);
        }
        else if (opt==2) {
            scanf("%d%d",&x,&y);
            change(1,1,n,w[x],ed[x],y);
        }
        else if (opt==3) {
            scanf("%d",&x);
            printf("%lld\n",getans(x,1));
        }
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章