HDU 3966 Aragorn's story (tree link division point number)

Source: Internet
Author: User
Aragorn's story Time Limit: 10000/3000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 2526 accepted submission (s): 709


Problem descriptionour 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 special camps real-time.
Inputmultiple 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.
Outputfor 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
748Hint1.The number of enemies may be negative.2.Huge input, be careful.  
 
Source2011 multi-university training contest 13-host by hit
Recommendwe have carefully selected several similar problems for you: 3964 3965 3962 3963 question: give you a tree. You can perform three operations on the tree. 1. Add a value to all vertices in the path U-> V on the tree. 2 ............. Subtract a value. 3. query the value of V for a node on the tree. Idea: Create a bare tree chain. For details, see the code:
#include<algorithm>#include<iostream>#include<string.h>#include<stdio.h>using namespace std;const int INF=0x3f3f3f3f;const int maxn=50010;typedef long long ll;#define lson L,mid,ls#define rson mid+1,R,rsint sz[maxn],dep[maxn],fa[maxn],son[maxn],id[maxn],top[maxn];int add[maxn<<2],val[maxn],arr[maxn],cnt,ptr;struct node{    int v;    node *next;} ed[maxn<<1],*head[maxn];void adde(int u,int v){    ed[cnt].v=v;    ed[cnt].next=head[u];    head[u]=&ed[cnt++];}void build(int L,int R,int rt){    add[rt]=0;    if(L==R)    {        add[rt]=val[L];        return;    }    int ls=rt<<1,rs=ls|1,mid=(L+R)>>1;    build(lson);    build(rson);}void update(int L,int R,int rt,int l,int r,int d){    if(l<=L&&R<=r)    {        add[rt]+=d;        return;    }    int ls=rt<<1,rs=ls|1,mid=(L+R)>>1;    if(l<=mid)        update(lson,l,r,d);    if(r>mid)        update(rson,l,r,d);}int qu(int L,int R,int rt,int p){    if(L==R)        return add[rt];    int ls=rt<<1,rs=ls|1,mid=(L+R)>>1;    if(p<=mid)        return qu(lson,p)+add[rt];    else        return qu(rson,p)+add[rt];}void dfs(int u){    int v,ms=0;    sz[u]=1,son[u]=-1;    for(node *p=head[u];p!=NULL;p=p->next)    {        v=p->v;        if(v==fa[u])            continue;        dep[v]=dep[u]+1;        fa[v]=u;        dfs(v);        if(sz[v]>ms)            ms=sz[v],son[u]=v;        sz[u]+=sz[v];    }}void getid(int u,int ft){    id[u]=++ptr,top[u]=ft;    val[ptr]=arr[u];    if(son[u]!=-1)        getid(son[u],top[u]);    for(node *p=head[u];p!=NULL;p=p->next)    {        if(p->v==son[u]||p->v==fa[u])            continue;        getid(p->v,p->v);    }}void init(int rt){    fa[rt]=-1;    dep[rt]=ptr=0;    dfs(rt);    getid(rt,rt);}void uppath(int u,int v,int d){    int f1=top[u],f2=top[v];    while(f1!=f2)    {        if(dep[f1]<dep[f2])            swap(f1,f2),swap(u,v);        update(1,ptr,1,id[f1],id[u],d);        u=fa[f1],f1=top[u];    }    if(dep[u]>dep[v])        swap(u,v);    update(1,ptr,1,id[u],id[v],d);}int main(){    int n,m,p,i,u,v,w,rt;    char cmd[100];    while(~scanf("%d%d%d",&n,&m,&p))    {        for(i=1;i<=n;i++)            scanf("%d",&arr[i]);        cnt=0;        memset(head,0,sizeof head);        for(i=1;i<n;i++)        {            scanf("%d%d",&u,&v);            adde(u,v);            adde(v,u);        }        rt=(n+1)/2;        init(rt);        build(1,ptr,1);        while(p--)        {            scanf("%s%d",cmd,&u);            if(cmd[0]=='Q')                printf("%d\n",qu(1,ptr,1,id[u]));            else            {                scanf("%d%d",&v,&w);                if(cmd[0]=='D')                    w=-w;                uppath(u,v,w);            }        }    }    return 0;}


HDU 3966 Aragorn's story (tree link division point number)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.