hdu 3966 Aragorn's Story 树链剖分 按点

Aragorn‘s Story

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

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 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.

Source

2011
Multi-University Training Contest 13 - Host by HIT

Recommend

We have carefully selected several similar problems for
you:  3964 3965 3962 3963 3967

  1 #pragma comment(linker,"/STACK:100000000,100000000")
  2 #include<iostream>
  3 #include<stdio.h>
  4 #include<cstring>
  5 #include<cstdlib>
  6 using namespace std;
  7 typedef __int64 LL;
  8 const int maxn = 5e4+3;
  9
 10 int pos,cont;
 11 int a[maxn];
 12 int son[maxn];
 13 int head[maxn];
 14 int vis[maxn];
 15 int w[maxn];
 16 int dep[maxn];
 17 int father[maxn];
 18 int hxl[maxn];
 19 int top[maxn];
 20 struct Edge
 21 {
 22     int to;
 23     int next;
 24 }edge[maxn*2];
 25
 26 void init()
 27 {
 28     pos = cont = 0;
 29     memset(son,-1,sizeof(son));
 30     memset(head,-1,sizeof(head));
 31     memset(hxl,0,sizeof(hxl));
 32 }
 33 void addedge(int u,int v)
 34 {
 35     edge[cont].to = v;
 36     edge[cont].next = head[u];
 37     head[u] = cont;
 38     ++cont;
 39 }
 40 void dfs1(int u,int fre,int deep)
 41 {
 42     father[u] = fre;
 43     dep[u] = deep;
 44     vis[u] = 1;
 45     for(int i=head[u];i!=-1;i=edge[i].next)
 46     {
 47         int v = edge[i].to;
 48         if(v!=fre)
 49         {
 50             dfs1(v,u,deep+1);
 51             vis[u]=vis[u]+vis[v];
 52             if(son[u] == -1 || vis[v] > vis[son[u]])
 53             son[u] = v;
 54         }
 55     }
 56 }
 57 void dfs2(int u,int t)
 58 {
 59     top[u] = t;
 60     if(son[u]!=-1)
 61     {
 62         w[u]=++pos;
 63         dfs2(son[u],t);
 64     }
 65     else
 66     {
 67         w[u]=++pos;
 68         return;
 69     }
 70     for(int i=head[u];i!=-1;i=edge[i].next)
 71     {
 72         int v = edge[i].to;
 73         if(v!=son[u] && v!=father[u])
 74         dfs2(v,v);
 75     }
 76 }
 77 void add(int x,int n,int num1)
 78 {
 79     for(int i=x;i<=n;i=i+(i&(-i)))
 80     hxl[i] = hxl[i] + num1;
 81 }
 82 LL query(int x)
 83 {
 84     if(x==0)return 0;
 85     LL sum1 = 0;
 86     while(x)
 87     {
 88         sum1=sum1+hxl[x];
 89         x=x-(x&(-x));
 90     }
 91     return sum1;
 92 }
 93 void insert(int u,int v,int num1,int size1)
 94 {
 95     int topu=top[u],topv=top[v];
 96     while(topu!=topv)
 97     {
 98         if(dep[topu]<dep[topv])
 99         {
100             swap(u,v);
101             swap(topu,topv);
102         }
103         add(w[topu],pos,num1*size1);
104         add(w[u]+1,pos,-1*num1*size1);
105         u=father[topu];
106         topu = top[u];
107     }
108     if(dep[u]>dep[v]) swap(u,v);
109     add(w[u],pos,num1*size1);
110     add(w[v]+1,pos,-1*num1*size1);
111 }
112 int main()
113 {
114     int n,m,q,u,v,l,r;
115     while(scanf("%d%d%d",&n,&m,&q)>0)
116     {
117         init();
118         for(int i=1;i<=n;i++)
119         scanf("%d",&a[i]);
120         for(int i=1;i<=m;i++)
121         {
122             scanf("%d%d",&u,&v);
123             addedge(u,v);
124             addedge(v,u);
125         }
126         addedge(0,1);
127         addedge(1,0);
128         dfs1(1,0,0);
129         dfs2(1,1);
130         char str1[6];
131         while(q--)
132         {
133             scanf("%s",str1);
134             if(str1[0]==‘I‘)
135             {
136                 scanf("%d%d%d",&l,&r,&v);
137                 insert(l,r,v,1);
138             }
139             else if(str1[0]==‘D‘)
140             {
141                 scanf("%d%d%d",&l,&r,&v);
142                 insert(l,r,v,-1);
143             }
144             else if(str1[0]==‘Q‘)
145             {
146                 scanf("%d",&r);
147                 printf("%I64d\n",query(w[r])+a[r]);
148             }
149         }
150     }
151     return 0;
152 }

hdu 3966 Aragorn's Story 树链剖分 按点

时间: 2024-08-24 00:02:26

hdu 3966 Aragorn's Story 树链剖分 按点的相关文章

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 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 &

HDU 3966 Aragorn&#39;s Story 树链剖分+BIT区间修改/单点询问

Aragorn's Story 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

hdu 3966 Aragorn&#39;s Story(树链剖分+树状数组)

题目链接:hdu 3966 Aragorn's Story 题目大意:给定一个棵树,然后三种操作 Q x:查询节点x的值 I x y w:节点x到y这条路径上所有节点的值增加w D x y w:节点x到y这条路径上所有节点的值减少w 解题思路:树链剖分,用树状数组维护每个节点的值. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <cstring>

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): 2526    Accepted Submission(s): 709 Problem Description Our protagonist is the handsome human prince Aragorn comes from The Lord

hdu 3966 Aragorn&#39;s Story : 树链剖分 O(nlogn)建树 O((logn)&#178;)修改与查询

1 /** 2 problem: http://acm.hdu.edu.cn/showproblem.php?pid=3966 3 裸板 4 **/ 5 #include<stdio.h> 6 #include<stdlib.h> 7 #include<string.h> 8 #include<vector> 9 using namespace std; 10 11 const int MAXN = 500005; 12 13 template <ty

HDU 3966 Aragorn&#39;s Story (树链点权剖分,成段修改单点查询)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 树链剖分的模版,成段更新单点查询.熟悉线段树的成段更新的话就小case啦. 1 //树链剖分 边权修改 单点查询 2 #include <iostream> 3 #include <cstring> 4 #include <algorithm> 5 #include <cstdio> 6 using namespace std; 7 const int M

Hdu 3699 Aragorn&#39;s Story (树链剖分)

题目大意: 对一颗树上进行路径加减,然后询问单点的值. 思路分析: 简单的树链剖分模板题. #include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #pragma comment(linker,"/STACk:10240000,10240000") #define maxn 50005 #define lson num<<1,s

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