Codeforces 383c. Propagating tree [tree array, DFS]

Source: Internet
Author: User
There is a tree. There are two kinds of operations for this tree: 1: represented as (1 X Val), add Val on the node numbered X, add-Val to each son of node X, and add-(-Val) to each son until there is no son. 2: indicates (2 x) Querying the value on the x node. Practice: Since each modification operation does not modify a value but many values, we can abstract this question into a question about interval modification and point query. So how to abstract it? It is clear that although each operation has been reduced Each addition or subtraction operation is performed on the same part.(That is, if you add the same number of nodes at a time, the next operation must add or subtract the same number, then we need to modify those nodes of the same number, which must be non-consecutive, so we cannot use the segment tree or the interval of the tree array. What should I do? Let's consider if we can re-sort the nodes on the tree so that each modified value can be in a continuous interval. For example, there is a tree as follows:


The first column on the right of the tree indicates the depth, and the second column indicates that the nodes of these layers with the same value (0 or 1) are added or subtracted at the same time. For example, when a node on the second layer and a node on the fourth layer are modified, all operations must be performed by adding or decreasing nodes. It is impossible for some nodes to be added or some nodes to be subtracted.
So how can we re-number the value to be modified for each operation into a continuous interval? For example, we should re-number the following array: 1 2 3 4 5678910 // new array subscript 1 4 5 6 7289310 // re-numbered
In this way, we change the value to be modified continuously, for example, changing the operation to (1 Val ), then we need to add the Val node in the new array with the range of [] and the Val-less node in [6, 10]; if the modification operation is (1 2 Val) so we need to add the Val node in the new array with the interval [6, 8], and the Val-minus node in [2, 3]; then how can we number it like this? We first store the values in the second column on the right of each node in the d [] array, starting from the root node (1), the hop layer DFS, add the child nodes that traverse to the new array one by one in the order of DFS. After the traversal, perform the same operation on each son of the root node. (For details, refer to the Code)
After we get this array, we also need to pre-process the modification operation on a certain vertex. We need to add a value in that interval, where the value is impaired. DFS is also used. The attributes of each node can be determined by the son. For details, see the code and comments: code:
# Include <iostream> # include <cstdio> # include <vector> # include <algorithm> # define n 200020 using namespace STD; struct EE // storage of the intervals in which the struct {int X1, Y1, X2, Y2;} e [N]; vector <int> G [N]; int N, m, a [n], d [N], C [N], BEF [N], Index = 1; void dfs1 (int x, int FA, int deep) // process the d [] Array {d [x] = deep; For (INT I = 0; I <G [X]. size (); I ++) if (G [x] [I]! = FA) dfs1 (G [x] [I], X, 1-deep);} void dfs2 (INT X, int FA, int deep) // obtain the new array {If (d [x] = Deep) BEF [x] = index ++; // aft [index ++] = X; // BEF [I] Where I is the ID of the original node, and BEF [I] is the subscript of I in the new array for (INT I = 0; I <G [X]. size (); I ++) if (G [x] [I]! = FA) dfs2 (G [x] [I], X, deep);} void dfs3 (INT X, int FA) // pre-process the attributes of each vertex {for (INT I = 0; I <G [X]. size (); I ++) if (G [x] [I]! = FA) dfs3 (G [x] [I], x); int ma1 = BEF [X], mi2 = N, Ma2 = 0; For (INT I = 0; I <G [X]. size (); I ++) {If (G [x] [I] = FA) continue; int cur = G [x] [I]; mi2 = min (mi2, E [cur]. x1); Ma2 = max (Ma2, E [cur]. y1); ma1 = max (ma1, E [cur]. y2);} e [X]. x1 = BEF [X], E [X]. y1 = ma1, E [X]. x2 = mi2, E [X]. y2 = Ma2; // [X1, Y1] indicates the interval in which value-added operations are required, and [X2, y2] indicates the interval in which value-added operations are required, can be determined by the son} int getnum (int x) // the interval of the tree array is modified below. Click the query function ~ {Int RNT = 0; For (INT I = x; I <= N; I ++ = (I & (-I) {RNT ++ = C [I];} return RNT;} void add (int I, int A) {While (I> = 1) {C [I] + =; i-= (I & (-I) ;}} int main () {scanf ("% d", & N, & M ); for (INT I = 1; I <= N; I ++) scanf ("% d", A + I); For (INT I = 0; I <n-1; I ++) {int A, B; scanf ("% d", & A, & B); G [A]. push_back (B), g [B]. push_back (a);} dfs1 (, 1); // calculate the d [] array dfs2 (, 1 ); // process the root node for (INT I = 0; I <G [1]. size (); I ++) dfs2 (G [1] [I], 1, 0); // each child of the Root Node Sub-processing dfs3 (); // pre-processing while (M --) {int ty; scanf ("% d", & ty); If (TY = 1) {int X, Y; scanf ("% d", & X, & Y); int L1 = E [X]. x1, R1 = E [X]. y1, L2 = E [X]. x2, R2 = E [X]. y2; add (R1, Y), add (l1-1,-y); If (R2! = 0) add (R2,-y), add (l2-1, Y); // perform the subtraction operation if not the root node} else {int X; scanf ("% d", & X); cout <getnum (BEF [x]) + A [x] <Endl ;}} return 0 ;}


Codeforces 383c. Propagating tree [tree array, DFS]

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.