HDU 3966 Aragorn's story

Source: Internet
Author: User
Aragorn's story

Time Limit: 10000/3000 MS (Java/others) memory limit: 32768/32768 K (Java/others) total submission (s): 2393 accepted submission (s): 641

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 input3 2 51 2 32 12 3I 1 3 5q 2D 1 2 2q 1 Q 3 sample output748 Hint1. the number of enemies may be negative.2.huge input, be careful. source 2011 multi-university training contest 13-host by hit recommendwe have carefully selected several similar problems for you: 3964 3965 3962 3963 tree array + tree link splitting ~~
# Pragma comment (linker, "/Stack: 1024000000,1024000000") # include <iostream> # include <algorithm> # include <cstring> using namespace STD; # define lson RT <1 # define rson RT <1 | 1 typedef long ll; const int n = 50005; int n, m, Q; int Tim; int num [N], size [N], top [N], son [N]; int Dep [N], tid [N], rnk [N], fa [N]; int EH [N], to [2 * n], NXT [2 * n], edge; void addedge (int u, int V) {to [edge] = V; NXT [edge] = EH [u]; EH [u] = edge ++; To [edge] = U; NXT [edge] = EH [v]; EH [v] = edge ++;} void dfs1 (int u, int father, int D) {Dep [u] = D; Fa [u] = Father; Size [u] = 1; for (INT I = EH [u]; ~ I; I = NXT [I]) {int v = to [I]; If (V! = Father) {dfs1 (v, U, D + 1); Size [u] + = size [v]; if (son [u] =-1 | size [v]> size [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 = EH [u]; ~ I; I = NXT [I]) {int v = to [I]; If (V! = Son [u] & V! = Fa [u]) dfs2 (V, V) ;}} ll C [N]; int lowbit (int x) {return X &-X ;} void add (INT POs, int key) {While (Pos <= N + 3) {C [POS] + = key; POS ++ = lowbit (POS );}} void Update (int u, int V, int Val) {int F1 = top [u], f2 = top [v]; while (F1! = F2) {If (DEP [F1] <Dep [F2]) {swap (F1, F2); swap (u, v);} Add (TID [F1], val); add (TID [u] + 1,-Val); U = Fa [F1]; F1 = top [u];} if (DEP [u]> Dep [v]) Swap (u, v); add (TID [u], Val); add (TID [v] + 1, -Val);} ll query (int pos) {ll res = 0; while (Pos> 0) {res + = C [POS]; pos-= lowbit (POS);} return res;} void Init () {memset (C, 0, sizeof (c); memset (EH,-1, sizeof (EH); memset (son,-1, sizeof (son); Tim = 1; // The Tree array coordinate starts from 1 !! Edge = 0 ;}int main (void) {char op [5]; int U, V, C; freopen ("in.txt", "r", stdin); While (~ Scanf ("% d", & N, & M, & Q) {Init (); For (INT I = 1; I <= N; ++ I) scanf ("% d", & num [I]); For (INT I = 1; I <= m; ++ I) {scanf ("% d", & U, & V); addedge (u, v);} dfs1 (1, 0, 0); dfs2 (1, 1 ); for (INT I = 1; I <= N; ++ I) {Add (TID [I], num [I]); add (TID [I] + 1, -num [I]);} while (Q --) {scanf ("% s", OP); If (OP [0] = 'q ') {scanf ("% d", & U); printf ("% LLD \ n", query (TID [u]); continue ;} scanf ("% d", & U, & V, & C); If (OP [0] = 'D') C =-C; update (u, v, c) ;}} return 0 ;}

 

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.