您的位置:首页 > 产品设计 > UI/UE

Hdu 4836 The Query on the Tree

2014-06-04 16:47 344 查看


The Query on the Tree

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 129 Accepted Submission(s): 63



Problem Description

  度度熊最近沉迷在和树有关的游戏了,他一直认为树是最神奇的数据结构。一天他遇到这样一个问题:

  有一棵树,树的每个点有点权,每次有三种操作:

  1. Query x 表示查询以x为根的子树的权值和。

  2. Change x y 表示把x点的权值改为y(0<=y<=100)。

  3. Root x 表示把x变为根。

  现在度度熊想请更聪明的你帮助解决这个问题。



Input

  第一行为数据组数T(1 <= T <= 100)

  每组数据第一行为N(1<= N <= 10000),表示树的节点数。

  后面N-1行每行有两个数x,y ,表示x,y之间有一条边 1<=x,y<=N。初始时树是以1号节点为根节点。

  之后的一行为N个数表示这N个点的点权(点权的范围是0到100)。

  然后为整数Q(Q<=1000)为操作次数。

  之后的Q行为描述中的三种操作。



Output

  对于第k组输入数据,第一行输出Case #k 接下来对于每个”Query x”操作,输出以x为根的子数和。



Sample Input

2
5
1 2
1 3
3 4
3 5
1 2 3 4 5
5
Query 1
Change 3 10
Query 1
Root 4
Query 3
8
1 2
1 3
3 4 
4 5
5 6
5 7
4 8
1 2 3 4 5 6 7 8
5
Query 1
Query 3
Root 5
Query 3
Query 1




Sample Output

Case #1:
15
22
18
Case #2:
36 
33
6
3

对于树上的每一个点,dfs出一个连续的区间,两端分别是dfs到这个点的编号,和dfs完他的子树,最后一个点的编号,这样修改可以用BIT或SegmentTree进行求这个区间的操作。
至于查询操作,假如当前的root不在X的子树里,那么答案就是直接查询X的dfs区间和,如果root在X的子树里,那就是需要把root所在的子树减去,并且加上一开始不在X的dfs区间的和,也就是所有数的和减去A的dfs区间和,A是X的儿子以及root的祖先。当然如果X就是root......

#include <stdio.h>
#include <vector>
#include <string>
#include <queue>
#include <string.h>
#include <algorithm>
using namespace std;

#define repe(i,now) for(int i=head[now];i!=-1;i=edge[i].next)
#define rep(i,s,t) for(int i=s;i<t;i++)
#define clr(a,v) memset(a,v,sizeof a)
#define L t<<1
#define R t<<1|1
#define lowbit(x) (x&(-x))

const int N=10005;
struct Edge{
    int v,next;
}edge[N*2];
int head
,e;
int t,n,a,b,v
,to
,rTo
,last
,fa
[20],dep
,cnt,q,x,y,all,r;
char op[10];
int c
;

inline void add(int x,int v){
    while(x<=n){
        c[x]+=v;x+=lowbit(x);
    }
}

inline int sum(int x){
    int a=0;
    while(x>0){
        a+=c[x];x-=lowbit(x);
    }
    return a;
}

inline void addEdge(int u,int v){
    edge[e].v=v;
    edge[e].next=head[u];
    head[u]=e++;
}

inline void dfs(int now,int pre){
    to[now]=(++cnt);rTo[cnt]=now;
    fa[now][0]=pre;
    dep[now]=dep[pre]+1;
    rep(i,1,20) fa[now][i]=fa[fa[now][i-1]][i-1];
    repe(i,now){
        int nxt=edge[i].v;
        if(nxt!=pre){
            dfs(nxt,now);
        }
    }
    last[now]=cnt;
}

inline int lca(int a,int b){
    if(dep[a]>dep[b]) swap(a,b);
    int ha=dep[a],hb=dep[b];
    int ta=a,tb=b;
    int det=hb-ha;
    rep(i,0,20){
        if(det&(1<<i)){
            tb=fa[tb][i];
        }
    }
    if(ta==tb) return ta;
    for(int i=19;i>=0;i--){
        if(fa[ta][i]==fa[tb][i]) continue;
        ta=fa[ta][i];
        tb=fa[tb][i];
    }
    return fa[ta][0];
}

inline void Init(){
    scanf("%d",&n);
    clr(head,-1),e=all=cnt=0;
    r=1;
    rep(i,1,n){
        scanf("%d%d",&a,&b);
        addEdge(a,b);
        addEdge(b,a);
    }
    rep(i,1,n+1) scanf("%d",&v[i]),all+=v[i];
    fa[1][0]=1;
    dep[0]=0;
    dfs(1,0);
}

inline int find(int x,int y){
    int det=dep[y]-dep[x]-1;
    rep(i,0,20){
        if(det&(1<<i)){
            y=fa[y][i];
        }
    }
    return y;
}
inline void Work(){
    clr(c,0);
    rep(i,1,n+1) add(to[i],v[i]);
    scanf("%d",&q);
    rep(i,0,q){
        scanf("%s",op);
        if(op[0]=='Q'){
            scanf("%d",&x);
            a=lca(x,r);
            if(x==r){
                printf("%d\n",all);
            }
            else if(a!=x){
                printf("%d\n",sum(last[x])-sum(to[x]-1));
            }
            else{
               b=find(x,r);
               printf("%d\n",all-sum(last[b])+sum(to[b]-1));
            }
        }
        else if(op[0]=='R'){
            scanf("%d",&x);r=x;
        }
        else{
            scanf("%d%d",&x,&y);
            add(to[x],y-v[x]);
            all+=y-v[x];
            v[x]=y;
        }
    }
}

int main(){
    scanf("%d",&t);
    rep(ca,1,t+1){
        printf("Case #%d:\n",ca);
        Init();
        Work();
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: