(第一个树链剖分!!!!)hdu 3966

A - Aragorn‘s Story

Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Submit Status Practice HDU 3966

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 5
1 2 3
2 1
2 3
I 1 3 5
Q 2
D 1 2 2
Q 1
Q 3

Sample Output

7
4
8

Hint

 1.The number of enemies may be negative. 2.Huge input, be careful. 
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
#define N 50010
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
int sum[N<<2],cal[N<<2];
vector<int> e[N];
int n,m,p;
int tim;
char s[5];
int num[N],siz[N],top[N],son[N];
int deep[N],tid[N],rnk[N],fa[N];
void init()
{
    memset(son,-1,sizeof(son));
    tim=0;
    for(int i=0;i<=n;i++)
        e[i].clear();
}
void dfs1(int u,int father,int d)
{
    deep[u]=d;
    fa[u]=father;
    siz[u]=1;
    for(int i=0;i<e[u].size();i++)
    {
        int v=e[u][i];
        if(v!=father)
        {
            dfs1(v,u,d+1);
            siz[u]+=siz[v];
            if(son[u]==-1||siz[v]>siz[son[u]])
                son[u]=v;
        }
    }
}
void dfs2(int u,int tp)
{
    top[u]=tp;
    tid[u]=++tim;
    rnk[tid[u]]=u;
    if(son[u]==-1) return ;
    dfs2(son[u],tp);
    for(int i=0;i<e[u].size();i++)
    {
        int v=e[u][i];
        if(v!=son[u]&&v!=fa[u])
        {
            dfs2(v,v);
        }
    }
}
void pushdown(int rt,int m)
{
    if(cal[rt])
    {
        cal[rt<<1]+=cal[rt];
        cal[rt<<1|1]+=cal[rt];
        sum[rt<<1]+=(m-(m>>1))*cal[rt];
        sum[rt<<1|1]+=(m>>1)*cal[rt];
        cal[rt]=0;
    }
}
void build(int l,int r,int rt)
{
    cal[rt]=0;
    if(l==r)
    {
        sum[rt]=num[rnk[l]];
        return ;
    }
    int mid=(l+r)>>1;
    build(lson);
    build(rson);
}
void update(int L,int R,int c,int l,int r,int rt)
{
    if(L<=l&&r<=R)
    {
        cal[rt]+=c;
        sum[rt]+=(r-l+1)*c;
        return ;
    }
    pushdown(rt,r-l+1);
    int mid=(l+r)>>1;
    if(L<=mid) update(L,R,c,lson);
    if(R>mid) update(L,R,c,rson);
}
int query(int pos,int l,int r,int rt)
{
    if(l==r)
    {
        return sum[rt];
    }
    pushdown(rt,r-l+1);
    int mid=(l+r)>>1,ret=0;
    if(pos<=mid) ret=query(pos,lson);
    else ret=query(pos,rson);
    return ret;
}
void change(int x,int y,int val)
{
    while(top[x]!=top[y])
    {
        if(deep[top[x]]<deep[top[y]]) swap(x,y);
        update(tid[top[x]],tid[x],val,1,n,1);
        x=fa[top[x]];
    }
    if(deep[x]>deep[y]) swap(x,y);
    update(tid[x],tid[y],val,1,n,1);
}
int main()
{
    int a,b,c;
    while(scanf("%d%d%d",&n,&m,&p)!=EOF)
    {
        init();
        for(int i=1;i<=n;i++)
            scanf("%d",&num[i]);
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d",&a,&b);
            e[a].push_back(b);
            e[b].push_back(a);
        }
        dfs1(1,0,1);
        dfs2(1,1);
        build(1,n,1);
        while(p--)
        {
            scanf("%s",s);
            if(s[0]==‘Q‘)
            {
                scanf("%d",&a);
                printf("%d\n",query(tid[a],1,n,1));
            }
            else
            {
                scanf("%d%d%d",&a,&b,&c);
                if(s[0]==‘D‘) c=-c;
                change(a,b,c);
            }
        }
    }
    return 0;
}

  

时间: 2025-01-06 11:40:34

(第一个树链剖分!!!!)hdu 3966的相关文章

树链剖分 [HDU 3966] Aragorn&#39;s Story

Aragorn's Story Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3544    Accepted Submission(s): 995 Problem Description Our protagonist is the handsome human prince Aragorn comes from The Lord

树链剖分 [HDU 5029] Relief grain

Relief grain Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others) Total Submission(s): 1254    Accepted Submission(s): 299 Problem Description The soil is cracking up because of the drought and the rabbit kingdom is

树链剖分 [HDU 5044] Tree

Tree Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 2038    Accepted Submission(s): 391 Problem Description You are given a tree (an acyclic undirected connected graph) with N nodes. The tree

HDU - 3966 Aragorn&#39;s Story(树链剖分入门+线段树)

HDU - 3966 Aragorn's Story Time Limit: 3000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status Description Our protagonist is the handsome human prince Aragorn comes from The Lord of the Rings. One day Aragorn finds a lot of ene

Hdu 3966 Aragorn&#39;s Story (树链剖分 + 线段树区间更新)

题目链接: Hdu 3966 Aragorn's Story 题目描述: 给出一个树,每个节点都有一个权值,有三种操作: 1:( I, i, j, x ) 从i到j的路径上经过的节点全部都加上x: 2:( D, i, j, x ) 从i到j的路径上经过的节点全部都减去x: 3:(Q, x) 查询节点x的权值为多少? 解题思路: 可以用树链剖分对节点进行hash,然后用线段树维护(修改,查询),数据范围比较大,要对线段树进行区间更新 1 #include <cstdio> 2 #include

HDU 2966 Aragorn&#39;s Story 树链剖分第一题 基础题

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 c

HDU 3966 Aragorn&#39;s Story(树链剖分 模板题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 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, th

HDU 3966 Aragorn&amp;#39;s Story(树链剖分)

HDU Aragorn's Story 题目链接 树抛入门裸题,这题是区间改动单点查询,于是套树状数组就OK了 代码: #include <cstdio> #include <cstring> #include <vector> #include <algorithm> using namespace std; const int N = 50005; inline int lowbit(int x) {return x&(-x);} int dep

HDU 3966 Aragorn&#39;s Story 树链剖分

Link: http://acm.hdu.edu.cn/showproblem.php?pid=3966 这题注意要手动扩栈. 这题我交g++无限RE,即使手动扩栈了,但交C++就过了. 1 #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include <cstdio> 3 #include <cstring> 4 #include <iostream> 5 #include &