[Beauty of Programming Challenge first tree]

Time limit: 4000ms
A single point of time: 2000ms
Memory Limit: 256MB

At description

A tree has N nodes. A point which is the root.

The initial point of the weights are zero.

Depth is defined as the depth of a node to its parent +1 ,. In particular, the depth is defined as a root node.

Must now support a range of the following: a subtree of node u, l and r weights between nodes depth value (the depth here is still calculated from the whole root of the tree), are a number plus delta.

Q. After completion of all operations, the weight of each node is.


In order to reduce the overhead caused great output, if the entire operation is completed, the weight of each node are answer [1..N]. Please calculate a Hash value (select the appropriate data type, avoid an overflow condition), for example, in accordance with the following manner.

Finally only need to output the Hash value on it.


MOD =1000000007; // 10^9 + 7

MAGIC= 12347;

Hash =0;

For i= 1 to N do

   Hash = (Hash * MAGIC + answer[i]) mod MOD;

EndFor


Entry

A first line integer T (1 ≤ T ≤ 5). It represents the number of data sets.

The next set of input data is T, there is no empty row between the test data.

Each set of data formats such as the following:

The first line of an integer N (N ≤ ≤ 10. 1 . 5 ), represents the total number of nodes in the tree.

Next, N - 1 lines, each number 1, a (1 ≤ a ≤ N), the order indicates the number of parent node 2..N node.

Next, an integer Q (. 1 ≤ Q ≤ 10 . 5 ), represents the total number of operations.

Next Q lines of four integers, U, L, R & lt, Delta (. 1 ≤ U ≤ N, R & lt ≤. 1 ≤ L ≤ N, -10 . 9 ≤ 10 ≤ Delta . 9 ). On behalf of a single operation.


Export

Each set of data, the first output line " Case X: ", X indicates the first several sets of data. This set of data and then take the answers Hash values.


data range


Small Data: 1 ≤ N, Q ≤ 1000

Big Data:. 1 ≤ N, Q ≤ 10 . 5


Example explained

1 point subtree 1,2,3 three nodes. 2-3 which is a depth of between 2 and point 3 points.

2 subtree point 3 has two nodes. None of the depth of a node.

So, after running at all department operations. There are only two weights of 2,3 addition of 1. That answer is 011.

Hash values ​​corresponding recalculation can.




Examples of input
1
3
1
2
2
1 2 3 1
2 1 1 1
Examples of output
Case 1: 12348
The following c ++ code that despite the adoption of the test examples, but the WR. There is to know where the problem is, kindly point it out ~
#include <iostream>
#include <cstdio>
#include <cstdlib>

int depth_n;
long mode = 1000000000+7;
int magic = 12347;

int find_parent(int n, int *parent, int depth)
{
	if (n == parent[n])
	{
		depth_n = depth;
		return n;
	}
	else return find_parent(parent[n], parent, depth+1);
}

int main()
{
	int T,t;
	scanf("%d", &T);
	for(t = 1; t <= T; t++)
	{
		int N, i;
		scanf("%d", &N);
		int *parent = new int[N+1];
		long long *ans = new long long[N+1];

		for (i = 0; i < N+1; i++)
		{
			ans[i] = 0;
		}
		parent[1] = 1;
		for (i = 2; i <= N; i++)
		{
			scanf("%d", &parent[i]);
		}
		int Q, u, l, r;
		long long delta;

		scanf("%d", &Q);
		for ( i = 0; i < Q; i++)
		{
			scanf("%d %d %d %lld", &u, &l, &r, &delta);
			if (u > N || u < 1)
			{
				continue;
			}
			if (l > r || r > N || l < 1)
			{
				continue;
			}
			int n;
			for ( n = 1; n <= N; n++)
			{
				depth_n = 1;
				if (find_parent(n, parent, depth_n) == u)
				{
					if (r >= depth_n && l <= depth_n)
					{
						ans[n] += delta;
					}
				}
			}
		}
		long long hash_n = 0;
		for ( i = 1; i <= N; i++)
		{
			hash_n = (hash_n*magic + ans[i])%mode;
		}
		printf("Case %d: %lld\n",t, hash_n);
		delete[] ans;
		delete[] parent;
	}
	system("pause");
	return 0;
}


Guess you like

Origin www.cnblogs.com/mqxnongmin/p/10963933.html