hdu 3966 (树链剖分,树状数组/线段树)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=3966
题意:

给一棵树,并给定各个点权的值,然后有3种操作:
I C1 C2 K: 把C1与C2的路径上的所有点权值加上K
D C1 C2 K:把C1与C2的路径上的所有点权值减去K
Q C:查询节点编号为C的权值

分析:

树链剖分入门题,基于点的重编号。
操作是区间增减+单点查询
用树状数组或者线段树维护操作

代码:
树状数组

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>
using namespace std;
const int N = 50010;
struct EDGE {
    int v,nex;
} edge[N<<1];
int head[N],tot;
int dep[N],p[N],fp[N],fa[N],top[N],son[N],siz[N];
int cnt;
int num[N];
void addedge(int a,int b) {
    edge[tot].v=b;
    edge[tot].nex=head[a];
    head[a]=tot++;
}
void dfs(int u) {
    siz[u]=1,son[u]=0;
    for(int i=head[u]; ~i; i=edge[i].nex) {
        int v=edge[i].v;
        if(v!=fa[u]) {
            fa[v]=u;
            dep[v]=dep[u]+1;
            dfs(v);
            if(siz[v]>siz[son[u]])son[u]=v;
            siz[u]+=siz[v];
        }
    }
}
void build(int u,int tp) {
    p[u]=++cnt;
    fp[p[u]]=u;
    top[u]=tp;
    if(son[u])build(son[u],tp);
    for(int i=head[u]; ~i; i=edge[i].nex) {
        int v=edge[i].v;
        if(v!=son[u]&&v!=fa[u])build(v,v);
    }
}
//树状数组
int arr[N];
inline int lowbit(int x){return x&(-x);}
inline int sum(int x) {
    int res=0;
    while(x)res+=arr[x],x-=lowbit(x);
    return res;
}
inline void add(int x,int n) {
    while(x<N)arr[x]+=n,x+=lowbit(x);
}
inline int update(int x,int y,int n) {
    add(x,n);
    add(y+1,-n);
}
void change(int a,int b,int x) {
    int f1=top[a],f2=top[b],tmp=0;
    while(f1!=f2) {
        if(dep[f1]<dep[f2])swap(f1,f2),swap(a,b);
        update(p[f1],p[a],x);
        a=fa[f1],f1=top[a];
    }
    // if(a==b)return;
    if(dep[a]>dep[b])swap(a,b);
    update(p[a],p[b],x);
}

int n,m,q,a,b,c;
char op[10];
int main() {
    int T;
    //freopen("f.txt","r",stdin);
    while(~scanf("%d%d%d",&n,&m,&q)) {
        memset(head,-1,sizeof(head));
        memset(arr,0,sizeof(arr));
        cnt=tot=0;
        for(int i=1; i<=n; i++)scanf("%d",&num[i]);
        for(int i=0; i<m; i++) {
            scanf("%d%d",&a,&b);
            addedge(a,b);
            addedge(b,a);
        }
        dfs(1);
        build(1,1);

        for(int i=1;i<=n;i++)update(p[i],p[i],num[i]);
        while(q--) {
            scanf("%s",op);
            if(op[0]=='Q') {
                scanf("%d",&a);
                printf("%d\n",sum(p[a]));
            } else {
                scanf("%d%d%d",&a,&b,&c);
                if(op[0]=='D')c=-c;
                change(a,b,c);
            }
        }
    }
    return 0;
}

线段树:
这题用线段树不如树状数组简单,但是学习下套路还是挺不错的

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int N = 50010;
struct EDGE {
    int v,nex;
} edge[N<<1];
int head[N],tot;
int dep[N],p[N],fp[N],fa[N],top[N],son[N],siz[N];
int cnt,sum[N<<2],col[N<<2];
int num[N];
void addedge(int a,int b) {
    edge[tot].v=b;
    edge[tot].nex=head[a];
    head[a]=tot++;
}
void dfs(int u) {
    siz[u]=1,son[u]=0;
    for(int i=head[u]; ~i; i=edge[i].nex) {
        int v=edge[i].v;
        if(v!=fa[u]) {
            fa[v]=u;
            dep[v]=dep[u]+1;
            dfs(v);
            if(siz[v]>siz[son[u]])son[u]=v;
            siz[u]+=siz[v];
        }
    }
}
void build(int u,int tp) {
    p[u]=++cnt;
    fp[p[u]]=u;
    top[u]=tp;
    if(son[u])build(son[u],tp);
    for(int i=head[u]; ~i; i=edge[i].nex) {
        int v=edge[i].v;
        if(v!=son[u]&&v!=fa[u])build(v,v);
    }
}
//线段树
//void pushUp(int rt) {
//    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
//}
void pushDown(int rt,int m) {
    if(col[rt]) {
        col[rt<<1]+=col[rt];
        col[rt<<1|1]+=col[rt];
        sum[rt<<1]+=(m-(m>>1))*col[rt];
        sum[rt<<1|1]+=(m>>1)*col[rt];
        col[rt]=0;
    }
}
void build(int l,int r,int rt) {
    col[rt]=0;
    if(l==r) {
        sum[rt]=num[fp[l]];
        return;
    }
    int m=l+r>>1;
    build(lson);
    build(rson);
   // pushUp(rt);
}
int query(int p,int l,int r,int rt) {
    if(l==r) {
        return sum[rt];
    }
    pushDown(rt,r-l+1);
    int m=l+r>>1;
    int ret=0;
    if(p<=m)ret=query(p,lson);
    else ret=query(p,rson);
//    pushUp(rt);
    return ret;
}
void update(int a,int b,int x,int l,int r,int rt) {
    if(a<=l&&r<=b) {
        col[rt]+=x;
        sum[rt]+=x*(r-l+1);
        return;
    }
    pushDown(rt,r-l+1);
    int m=l+r>>1;

    if(a<=m)update(a,b,x,lson);
    if(b>m)update(a,b,x,rson);
    //pushUp(rt);
}
void change(int a,int b,int x) {
    int f1=top[a],f2=top[b],tmp=0;
    while(f1!=f2) {
        if(dep[f1]<dep[f2])swap(f1,f2),swap(a,b);
        update(p[f1],p[a],x,1,cnt,1);
        a=fa[f1],f1=top[a];
    }
   // if(a==b)return;
    if(dep[a]>dep[b])swap(a,b);
    update(p[a],p[b],x,1,cnt,1);
}

int n,m,q,a,b,c;
char op[10];
int main() {
    int T;
   // freopen("f.txt","r",stdin);

    while(~scanf("%d%d%d",&n,&m,&q)) {
        memset(head,-1,sizeof(head));
        cnt=tot=0;
        for(int i=1; i<=n; i++)scanf("%d",&num[i]);
        for(int i=0; i<m; i++) {
            scanf("%d%d",&a,&b);
            addedge(a,b);
            addedge(b,a);
        }
        dfs(1);
        build(1,1);
        build(1,n,1);
        while(q--) {
            scanf("%s",op);
            if(op[0]=='Q') {
                scanf("%d",&a);
                printf("%d\n",query(p[a],1,n,1));
            } else {
                scanf("%d%d%d",&a,&b,&c);
                if(op[0]=='D')c=-c;
                change(a,b,c);
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值