Codeforces276E: Little Girl and Problem on Trees, catsontrees

Source: Internet
Author: User

Codeforces276E: Little Girl and Problem on Trees, catsontrees

A little girl loves problems on trees very much. Here's one of them.

A tree is an undirected connected graph, not containing cycles. The degree of nodeXIn the tree is the number of nodesYOf the tree, such that each of them is connected with nodeXBy some edge of the tree.

Let's consider a tree that consistsNNodes. We'll consider the tree's nodes indexed from 1N. The cosidered tree has the following property: each node should t for node number 1 has the degree of at most 2.

Initially, each node of the tree contains number 0. Your task is to quickly process the requests of two types:

  • Request of form: 0V X D. In reply to the request you should addXTo all numbers that are written in the nodes that are located at the distance of at mostDFrom nodeV. The distance between two nodes is the number of edges on the shortest path between them.
  • Request of form: 1V. In reply to the request you shoshould print the current number that is written in nodeV.
Input

The first line contains integersN(2 cores ≤ CoresNLimit ≤ limit 105) andQ(1 digit ≤ DigitQLimit ≤ limit 105)-the number of tree nodes and the number of requests, correspondingly.

Each of the nextNToo many-since limit 1 lines contains two integersUIAndVI(1 digit ≤ DigitUI, Bytes,VILimit ≤ limitN,UI  =VI), That show that there is an edge between nodesUIAndVI. Each edge's description occurs in the input exactly once. It is guaranteed that the given graph is a tree that has the property that is described in the statement.

NextQLines describe the requests.

  • The request to add has the following format: 0V X D(1 digit ≤ DigitVLimit ≤ limitN, 1 limit ≤ limitXLimit ≤ limit 104, 1 limit ≤ limitDLatency <latencyN).
  • The request to print the node value has the following format: 1V(1 digit ≤ DigitVLimit ≤ limitN).

The numbers in the lines are separated by single spaces.

Output

For each request to print the node value print an integer-the reply to the request.

Sample test (s) input
3 61 21 30 3 1 20 2 3 10 1 5 21 11 21 3
Output
996
Input
6 111 22 55 41 61 30 3 1 30 3 4 50 2 1 40 1 5 50 4 6 21 11 21 31 41 51 6
Output
111711161711
 
A tree has only one vertex, and multiple links are derived from this vertex. If 0 v x d is input, x is added to all nodes within d, 1 v indicates the value of the v node to be queried.
 
Idea: when referring to other people's code, I found that tree arrays were used, but tree arrays were not used in the past. Even though tree Arrays can be used for questions, but the tree array is quite clever, so I took A look at the principle of the tree array temporarily, and finally combined the ideas of others to solve this question.
First, for a chain, when the node is in the chain, it is easy to update d down, but when it is updated to 1 node, we assume that there is still d distance not updated, if we can easily time out a single update, we can directly update the entire tree, that is, all nodes updated from 1 to d are in the same status.
Use new to dynamically allocate memory for over-memory storage
 
# Include <iostream> # include <stdio. h> # include <string. h> # include <stack> # include <queue> # include <map> # include <set> # include <vector> # include <math. h> # include <algorithm> using namespace std; # define ls 2 * I # define rs 2 * I + 1 # define up (I, x, y) for (I = x; I <= y; I ++) # define down (I, x, y) for (I = x; I> = y; I --) # define mem (a, x) memset (a, x, sizeof (a) # define w (a) while () # define LL long longconst double pi = acos (-1.0 );# Define N 100005 # define mod 19999997 const int INF = 0x3f3f3f3f; # Number of the chain where define exp 1e-8 // v points are located, number of layers, v number on the chain, and length of each chain. Int mark [N], level [N], pos [N], length [N], id, deep; vector <int> vec [N]; struct node {int * sum, n; void init (int len) {n = len; sum = new int [n + 1]; memset (sum, 0, (n + 1) * sizeof (int);} int query (int I) {int ans = 0; for (; I <= n; I + = I &-I) ans + = sum [I]; return ans;} void add (int I, int x) {for (; I> 0; i-= I &-I) sum [I] + = x;} void updata (int l, int r, int x) // update l ~ R range {add (r, x); add (L-1,-x); // redundant update minus} tree, * chain; int dfs (int step, int u, int pre) {int I, n = vec [u]. size (), v; level [u] = step + 1; // Add the mark [u] = id of the layer where 1 is located; pos [u] = step; // The label on the chain is not 1 node, so 1 up (I, 0, n-1) {v = vec [u] [I] is not added; if (v = pre) continue; return dfs (step + 1, v, u);} return step;} void init () {int I, n = vec [1]. size (), len, v; chain = new node [n]; level [1] = 1; up (I, 0, n-1) // initialize each chain {id = I; v = vec [1] [I]; len = dfs (1, v, 1); length [I] = len; deep = max (len, deep ); // find the longest chain depth chain [I]. init (len); // update the depth of each chain} tree. init (++ deep); // The depth of the entire tree} int query (int v) {int ans = 0; ans = tree. query (level [v]); if (v! = 1) {ans + = chain [mark [v]. query (pos [v]);} return ans;} void updata (int v, int x, int d) {int l, r; if (v = 1) {if (deep> = 1 + d) r = 1 + d; // compare the depth of the tree with the depth of d to determine the update depth else r = deep; tree. updata (1, r, x); return;} // For each chain, r indicates the depth of the next update, and l indicates the depth of the last update, update to 1 node first if (length [mark [v]> = pos [v] + d) r = pos [v] + d; else r = length [mark [v]; if (1 <= pos [v]-d) l = pos [v]-d; else l = 1; chain [mark [v]. updata (l, r, x); d-= level [v]-1; if (d> = 0) // There are still {if (deep> = 1 + d) r = 1 + d remaining at Node 1; // all links are updated based on node 1, the depth is r else r = deep; tree. updata (1, r, x); if (r> = 2) // for the required vertex, because the update is twice, to subtract this update {if (length [mark [v]> = R-1) r = R-1; else r = length [mark [v]; chain [mark [v]. updata (1, r,-x) ;}} int main () {int I, n, q, x, y; scanf ("% d ", & n, & q); up (I, 1, n-1) {scanf ("% d", & x, & y); vec [x]. push_back (y); vec [y]. push_back (x);} init (); int cas, v, d; w (q --) {scanf ("% d", & cas, & v ); If (! Cas) {scanf ("% d", & x, & d); updata (v, x, d);} else printf ("% d \ n ", query (v);} 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.