HDU

来源:互联网 发布:李明老师的linux视频 编辑:程序博客网 时间:2024/06/16 02:57

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3966

Aragorn's Story

Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12273    Accepted Submission(s): 3279


Problem Description
Our protagonist is the handsome human prince Aragorn comes from The Lord of the Rings. One day Aragorn finds a lot of enemies who want to invade his kingdom. As Aragorn knows, the enemy has N camps out of his kingdom and M edges connect them. It is guaranteed that for any two camps, there is one and only one path connect them. At first Aragorn know the number of enemies in every camp. But the enemy is cunning , they will increase or decrease the number of soldiers in camps. Every time the enemy change the number of soldiers, they will set two camps C1 and C2. Then, for C1, C2 and all camps on the path from C1 to C2, they will increase or decrease K soldiers to these camps. Now Aragorn wants to know the number of soldiers in some particular camps real-time.
 

Input
Multiple test cases, process to the end of input.

For each case, The first line contains three integers N, M, P which means there will be N(1 ≤ N ≤ 50000) camps, M(M = N-1) edges and P(1 ≤ P ≤ 100000) operations. The number of camps starts from 1.

The next line contains N integers A1, A2, ...AN(0 ≤ Ai ≤ 1000), means at first in camp-i has Ai enemies.

The next M lines contains two integers u and v for each, denotes that there is an edge connects camp-u and camp-v.

The next P lines will start with a capital letter 'I', 'D' or 'Q' for each line.

'I', followed by three integers C1, C2 and K( 0≤K≤1000), which means for camp C1, C2 and all camps on the path from C1 to C2, increase K soldiers to these camps.

'D', followed by three integers C1, C2 and K( 0≤K≤1000), which means for camp C1, C2 and all camps on the path from C1 to C2, decrease K soldiers to these camps.

'Q', followed by one integer C, which is a query and means Aragorn wants to know the number of enemies in camp C at that time.
 

Output
For each query, you need to output the actually number of enemies in the specified camp.
 

Sample Input
3 2 51 2 32 12 3I 1 3 5Q 2D 1 2 2Q 1 Q 3
 

Sample Output
748
Hint
1.The number of enemies may be negative.2.Huge input, be careful.
 

Source
2011 Multi-University Training Contest 13 - Host by HIT

题目大意:有一个n个结点的树形图,每个结点都有一个权值,然后有三种操作:

1、查询节点u的权值

2、将结点u->v这条路径上的所有节点的权值都加w

3、将结点u->v这条路径上的所有节点的权值都减w

题解:典型的树剖题,如果知道树剖的知识点再加上线段树延迟更新就能迎刃而解了,可是就是这个题让我TLE了两天,最终的问题是因为数组不够大,- ^ - 关于树剖的知识上一篇博客已经给出链接。

#include <bits/stdc++.h>using namespace std;typedef long long LL;const int MAXN=5e5+100;struct node{    int u,v;    int nex;}eage[MAXN];int head[MAXN],cnt,idx;int siz[MAXN],son[MAXN],fa[MAXN],deep[MAXN];int top[MAXN],id[MAXN],aa[MAXN],val[MAXN];int A[MAXN<<2],value;void ADD(int u,int v){    eage[cnt].u=u;    eage[cnt].v=v;    eage[cnt].nex=head[u];    head[u]=cnt++;}void build(int u,int l,int r){    A[u]=0;    if(l==r){        A[u]=val[l];        return ;    }    int mid=(l+r)>>1;    build(u<<1,l,mid);    build(u<<1|1,mid+1,r);}void dfs1(int u,int f,int dep){    deep[u]=dep;    siz[u]=1;    son[u]=0;    fa[u]=f;    for(int i=head[u];i!=-1;i=eage[i].nex){        int v=eage[i].v;        if(v==f)continue;        dfs1(v,u,dep+1);        siz[u]+=siz[v];        if(siz[son[u]]<siz[v])son[u]=v;    }}void dfs2(int u,int tp){    top[u]=tp;    id[u]=++idx;    if(son[u])dfs2(son[u],tp);    for(int i=head[u];i!=-1;i=eage[i].nex){        int v=eage[i].v;        if(v==fa[u]||v==son[u])continue;        dfs2(v,v);    }}void  query(int u,int l,int r,int pos){    if(l==r&&l==pos){        value=A[u];        return ;    }    if(A[u]){        A[u<<1]+=A[u];        A[u<<1|1]+=A[u];        A[u]=0;    }    int mid=(l+r)>>1;    if(pos<=mid)       query(u<<1,l,mid,pos);    else query(u<<1|1,mid+1,r,pos);}void change(int u,int l,int r,int L,int R,int x){    if(L<=l&&r<=R){        A[u]+=x;        return ;    }    if(A[u]){        A[u<<1]+=A[u];        A[u<<1|1]+=A[u];        A[u]=0;    }    int mid=(l+r)>>1;    if(L<=mid)        change(u<<1,l,mid,L,R,x);    if(R>mid)        change(u<<1|1,mid+1,r,L,R,x);}void work(int u,int v,int c){    while(top[u]!=top[v]){        if(deep[top[u]]<deep[top[v]]){            swap(u,v);        }        change(1,1,idx,id[top[u]],id[u],c);        u=fa[top[u]];    }    if(deep[u]>deep[v])swap(u,v);    change(1,1,idx,id[u],id[v],c);}void solve(int p){    char s[10];    int a,b,c;    while(p--){        scanf("%s",s);        if(s[0]=='Q'){            scanf("%d",&c);            query(1,1,idx,id[c]);            printf("%d\n",value);        }        else{            scanf("%d%d%d",&a,&b,&c);            if(s[0]=='I')                work(a,b,c);            else if(s[0]=='D')                work(a,b,-c);        }    }}void init(int n){    for(int i=1;i<=n;i++)        scanf("%d",&aa[i]);    cnt=0;    int a,b;    memset(head,-1,sizeof(head));    for(int i=1;i<n;i++){        scanf("%d%d",&a,&b);        ADD(a,b);        ADD(b,a);    }    idx=0;    dfs1(1,0,1);    dfs2(1,1);    for(int i=1;i<=n;i++){        val[ id[i] ]=aa[i];    }    build(1,1,idx);}int main(){    int n,m,p;    while(~scanf("%d%d%d",&n,&m,&p)){        init(n);        solve(p);    }    return 0;}


原创粉丝点击