bzoj4034: [HAOI2015]树上操作(树链剖分+线段树)

来源:互联网 发布:实验楼 知乎 编辑:程序博客网 时间:2024/05/24 05:33

题目传送门
水经验。

解法:
就写个树剖呗。
然后用线段树维护一下呗。
维护整一段的和。
打个懒标记。

代码实现:

#include<cstdio>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>#include<cmath>#include<queue>using namespace std;typedef long long ll;struct node {    int x,y,next;}a[210000];int len,last[110000];void ins(int x,int y) {    len++;    a[len].x=x;a[len].y=y;    a[len].next=last[x];last[x]=len;}int fa[110000],son[110000],tot[110000],dep[110000];void pre_tree_node(int x) {    tot[x]=1;son[x]=0;    for(int k=last[x];k;k=a[k].next) {        int y=a[k].y;        if(y!=fa[x]) {            dep[y]=dep[x]+1;            fa[y]=x;            pre_tree_node(y);            if(tot[y]>tot[son[x]])                son[x]=y;            tot[x]+=tot[y];        }    }}int ys[110000],z,top[110000];void pre_tree_edge(int x,int tp) {    ys[x]=++z;top[x]=tp;    if(son[x]!=0)        pre_tree_edge(son[x],tp);    for(int k=last[x];k;k=a[k].next) {        int y=a[k].y;        if(y!=son[x]&&y!=fa[x])            pre_tree_edge(y,y);    }}struct trnode {    int l,r,lc,rc;ll c,lazy;}tr[210000];int trlen;void update(int x) {    int lc=tr[x].lc,rc=tr[x].rc;    tr[lc].lazy+=tr[x].lazy,tr[lc].c+=(tr[lc].r-tr[lc].l+1)*tr[x].lazy;    tr[rc].lazy+=tr[x].lazy,tr[rc].c+=(tr[rc].r-tr[rc].l+1)*tr[x].lazy;    tr[x].lazy=0;}void bt(int l,int r) {    trlen++;int now=trlen;    tr[now].l=l;tr[now].r=r;    tr[now].lc=tr[now].rc=-1;    tr[now].c=0;tr[now].lazy=0;    if(l<r) {        int mid=(l+r)/2;        tr[now].lc=trlen+1;bt(l,mid);        tr[now].rc=trlen+1;bt(mid+1,r);    }}void change(int now,int l,int r,ll k) {    if(tr[now].l==l&&tr[now].r==r) {        tr[now].c+=(r-l+1)*k;tr[now].lazy+=k;return ;    }    int lc=tr[now].lc,rc=tr[now].rc,mid=(tr[now].l+tr[now].r)/2;        if(tr[now].lazy!=0)        update(now);    if(r<=mid)        change(lc,l,r,k);    else if(l>mid)        change(rc,l,r,k);    else {        change(lc,l,mid,k);change(rc,mid+1,r,k);    }    tr[now].c=tr[lc].c+tr[rc].c;}ll find_sum(int now,int l,int r) {    if(tr[now].l==l&&tr[now].r==r)        return tr[now].c;    int lc=tr[now].lc,rc=tr[now].rc,mid=(tr[now].l+tr[now].r)/2;    if(tr[now].lazy!=0)        update(now);    if(r<=mid)        return find_sum(lc,l,r);    else if(l>mid)        return find_sum(rc,l,r);    else        return find_sum(lc,l,mid)+find_sum(rc,mid+1,r);}ll solve(int x) {    int tx=top[x];ll ans=0;    while(tx!=1) {        ans+=find_sum(1,ys[tx],ys[x]);        x=fa[tx];tx=top[x];    }    return ans+find_sum(1,ys[1],ys[x]);}ll s[110000];int main() {    int n,m;scanf("%d%d",&n,&m);    for(int i=1;i<=n;i++)        scanf("%lld",&s[i]);    len=0;memset(last,0,sizeof(last));    for(int i=1;i<n;i++) {        int x,y;scanf("%d%d",&x,&y);        ins(x,y);ins(y,x);    }    fa[1]=0;dep[1]=0;pre_tree_node(1);    z=0;pre_tree_edge(1,1);    trlen=0;bt(1,z);    for(int i=1;i<=n;i++)        change(1,ys[i],ys[i],s[i]);    for(int i=1;i<=m;i++) {        int t,x;ll y;scanf("%d%d",&t,&x);        if(t==1) {            scanf("%lld",&y);            change(1,ys[x],ys[x],y);        }        else if(t==2) {            scanf("%lld",&y);            change(1,ys[x],ys[x]+tot[x]-1,y);        }        else            printf("%lld\n",solve(x));    }    return 0;}
原创粉丝点击