[HDU3966]Aragorn's Story 做题笔记

·· / ·– ·· ·-·· ·-·· / ·–· · ·-· ··· ·· ··· - / ··- -· - ·· ·-·· / ·· / ·– ·· -·
题目来源http://acm.hdu.edu.cn/showproblem.php?pid=3966

1、打懒标记的时候一定要注意懒标记是否可以累加,或者是否应该异或
2、多组测试数据。。。
3、线段树针对多组数据的初始化不要忘了

#include <cstdio>
#include <algorithm>
#include <cstring>
#define lch now<<1
#define rch now<<1|1
using namespace std;
const int N=60000;
const int inf=0x3fffffff;
int ver[N<<1],e[N<<1],head[N],nxt[N<<1];
int n,m,tot=1,cnt=0,L,R;
int dep[N],fa[N],size[N],id[N];
int son[N],top[N],w[N],C[N];
struct Tree {
    int sum,s,del;
}I[N<<2];
void add (int u,int v) {
    ver[++tot]=v;nxt[tot]=head[u];head[u]=tot;
}
void pushup (int now) {
    I[now].s=I[lch].s+I[rch].s;
    I[now].sum=I[lch].sum+I[rch].sum;
}
void pushdown (int now) {
    if (I[now].del==0) return ;
    I[lch].sum+=I[now].del*I[lch].s;
    I[rch].sum+=I[now].del*I[rch].s;
    I[lch].del+=I[now].del;//懒标记是可以累加的!
    I[rch].del+=I[now].del;//
    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;
        I[now].s=1;
        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 del) {
    if (l>R||r<L||l>r) return ;
    if (L<=l&&r<=R) {
        I[now].sum+=del*I[now].s;
        I[now].del+=del;//懒标记是可以累加的!
        return ;
    }
    int mid=(l+r)>>1;
    pushdown(now);
    if (L<=mid) change(lch,l,mid,del);
    if (R>mid) change(rch,mid+1,r,del);
    pushup(now);
}
int query (int now,int l,int r,int pos) {
    if (l==r&&l==pos) return I[now].sum;
    int mid=(l+r)>>1;
    pushdown(now);
    if (pos<=mid) return query(lch,l,mid,pos);//不要忘了return!
    if (pos>mid) return query(rch,mid+1,r,pos);
}
void dfs_1 (int u,int from) {
    size[u]=1;son[u]=0;fa[u]=from;
    for (int i=head[u];i;i=nxt[i]) {
        int v=ver[i];
        if (v==fa[u]) continue;
        dep[v]=dep[u]+1; fa[v]=u;
        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) {
    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]) {
        int v=ver[i];
        if (v==son[u]||v==fa[u]) continue;
        dfs_2(v,v);
    }
}
void Get (int x,int y,int val) {
    while (top[x]!=top[y]) {
        if (dep[top[x]]<dep[top[y]])swap(x,y);
        L=w[top[x]]; R=w[x];
        change(1,1,cnt,val);
        x=fa[top[x]];
    }
    if (dep[x]>dep[y]) swap(x,y);
    L=w[x]; R=w[y];
    change(1,1,cnt,val);
}
int main () {
    int u,v,cap,tmp;
    char str[20];
    while (scanf("%d%d%d",&n,&tmp,&m)!=EOF) {
    tot=1,cnt=0;
    memset(head,0,sizeof(head));
    memset(size,0,sizeof(size));
    memset(son,0,sizeof(son));
    memset(fa,0,sizeof(fa));
    memset(dep,0,sizeof(dep));
    for (int i=0;i<(N<<2);i++) I[i].del=0;//这里初始化一开始忘了
    for (int i=1;i<=n;i++) scanf("%d",&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,cnt);
    for (int i=1;i<=m;i++) {
        scanf("%s",str);
        if (str[0]=='I') {
            scanf("%d%d%d",&u,&v,&cap);
            Get(u,v,cap);
        }
        else if (str[0]=='D') {
            scanf("%d%d%d",&u,&v,&cap);
            Get(u,v,-cap);
        }
        else if (str[0]=='Q') {
            scanf("%d",&u);
            printf("%d\n",query(1,1,cnt,w[u]));
        }
    }
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值