Tree
Limits: 2 sec., 512 MiB
This statement was translated automatically from Ukrainian.
Having successfully passed the External Independent Evaluation in geography, Zenyk decided to spend a pleasant evening and solve the following interesting problem.
You are given a connected graph with \(n\) vertices connected by \(n-1\) edges. The vertices of the graph are numbered with integers from 1 to \(n\) inclusive.
Initially, the number zero is written in each vertex. In addition, you are given a set of \(m\) simple paths in this graph, where each path is given as a sequence of vertices. The given paths are numbered with integers from 1 to \(m\) inclusive.
You need to process \(k\) queries of the following two types.
+\(p_i\) \(a_i\) — add \(a_i\) to the numbers written on all vertices on the \(p_i\)-th path.?\(p_i\) — find the sum of the numbers written on all vertices of the \(p_i\)-th path.
Help Zenyk solve this problem.
Input
The first line contains three integers \(n\), \(m\), \(k\) — the number of vertices, the number of paths, and the number of queries, respectively.
The next \(n-1\) lines describe the edges of the tree \(u_i\), \(v_i\).
The next \(m\) lines describe the paths, one per line. The first number \(c_i\) denotes the number of vertices on the path, and the next \(c_i\) numbers describe the vertices of the path in traversal order.
The last \(k\) lines describe the queries according to the format described in the statement.
Output
For each query of the second type, output an integer — the answer to it.
Constraints
\(1 \le n, m, k \le 2 \cdot 10^5\),
\(1 \le u_i, v_i, c_i \le n\),
\(0 \le a_i \le 10^9\),
The total size of all given paths does not exceed \(2 \cdot 10^5\).
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 7 4 7 1 2 1 3 3 4 3 5 3 6 4 7 3 1 3 5 3 6 3 4 4 7 4 3 5 2 3 1 ? 4 + 3 7 + 4 4 ? 4 ? 1 + 2 1 ? 3 | 0 15 22 34 |
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
Tree - Editorial
Let \(len\) be the total length of the paths. Split the paths into two groups:
those longer than \(B\); there are at most \(\frac{len}{B}\) of them, and we call them big;
those shorter than \(B\), which we call small.
For the \(i\)-th big path, we count how many common vertices it has with the \(j\)-th path; call this \(cnt_{ij}\). For the \(i\)-th big path, we keep the value \(add_i\) that must be added to all its vertices. We also keep the sum \(sum_i\) of all elements of this path. For each vertex \(v\), we keep its current value \(x_v\), not counting the updates of big paths.
Then the answer for the \(i\)-th big path is \(sum_i\), in \(O(1)\).
For the \(j\)-th small path, we compute the sum of the values \(x_v\) of its vertices. To get the sum on it, we also need to take into account the values added by the big paths: the \(i\)-th big path adds \(cnt_{ij} \cdot add_i\). This takes \(O(B + \frac{len}{B})\), since we go over all vertices of the path (at most \(B\)) and over every big path, of which there are at most \(\frac{len}{B}\).
Now we need to compute the values \(cnt_{ij}\) and maintain the values \(x_v\) and \(sum_i\).
For the \(i\)-th big path, we mark all its vertices; then we go over the \(j\)-th path and its vertices, and if a vertex is marked, we set \(cnt_{ij} := cnt_{ij} + 1\).
When \(+ val\) comes for the \(j\)-th path, for each \(i\)-th big path we set \(sum_i := sum_i + cnt_{ij} \cdot val\). If the \(j\)-th path is big, we also update \(add_j := add_j + val\). If it is small, we go over all its vertices \(v\) and set \(x_v := x_v + val\). This takes \(O(\frac{len}{B})\) and \(O(\frac{len}{B} + B)\) for a big and a small path, respectively.
So one query takes \(O(\frac{len}{B} + B)\), and the total complexity is \(O(k \cdot (\frac{len}{B} + B) + n \cdot B)\). If we choose \(B^2 = len\), the total complexity is \(O((n + k) \cdot \sqrt {len})\).
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|