Problem Statement
Cat Taro has a rooted tree with N nodes. Nodes are numbered from 0 to N-1, inclusive. Node with the index 0 is the root of the tree. Each edge has some positive integer value written on it.
Taro wants to process M queries. Each query consists of two nodes u and v. Queries should be processed in the following way:
- If u equals v, print -1.
- Otherwise, if the node v is in the subtree of the node u, print the maximum value written on the edges that belong to the simple path from u to v.
- Otherwise, remove the edge between u and its parent. Instead, add a new edge (with the same value) that makes u the child of v. (Note that the entire subtree rooted at u is now a part of the subtree rooted at v.) Do not print anything.
Return the sum of all values that were printed during the process.
You are given the
initialize(): curValue = startValue genNextRandom(): curValue = (curValue * 1999 + 17) modulo 1,000,003 return curValue generateTree(): for i=1 to N-1: value[i] = genNextRandom() modulo maxValue; parent[i] = max( 0, i - 1 - (genNextRandom() modulo maxHeight) ); in our tree, the parent of vertex i is parent[i] the edge between i and parent[i] has the label value[i] generateQueries(): for i=0 to M-1: queryU[i] = genNextRandom() modulo N queryV[i] = genNextRandom() modulo N if queryU[i] > queryV[i]: swap queryU[i] and queryV[i] process the query with u=queryU[i] and v=queryV[i]
To generate the input, call initialize(), then generateTree(), and finally generateQueries().
Definition
- Class:
- TaroTreeRequests
- Method:
- getNumber
- Parameters:
- int, int, int, int, int
- Returns:
- long
- Method signature:
- long getNumber(int N, int M, int startValue, int maxValue, int maxHeight)
- (be sure your method is public)
Notes
- The intended solution should work within the given time limit for an arbitrary tree and an arbitrary sequence of queries. It does not depend on any special properties of the pseudorandom generator.
Constraints
- N will be between 1 and 200,000, inclusive.
- M will be between 1 and 200,000, inclusive.
- startValue will be between 0 and 1,000,002, inclusive.
- maxValue will be between 1 and 1,000,003, inclusive.
- maxHeight will be between 1 and 1,000,003, inclusive.
Examples
4
4
47
7
2
Returns: 8
The explanation to the picture below: 0) The initial tree. 1) The first operation, with u = 0, v = 2. The maximum value is 4. 2) The second operation with u = 1, v = 3. The maximum value is 0. 3) The third operation with u = 2, v = 3. The structure of the tree is changed after this operation. 4) The fourth operation with u = 0, v = 2. The maximum value is 4.
7
10
74
7
3
Returns: 40
10
4
103
100
2
Returns: 220
10000
10000
984848
1000000
2
Returns: 6632008328
100000
100000
954711
1000000
2
Returns: 66780062524
200000
200000
7584
948984
4
Returns: 75766570928
10000
10000
87598
987937
10000
Returns: 2062655
1
2
4774
1000000
7
Returns: -2
10
20
857968
1000000
7
Returns: 5296369
50
100
12
1000
10
Returns: 23235
100
200
984198
100
1
Returns: 18491
500
1000
47
1
2
Returns: -1
1000
10
189118
100000
50
Returns: 189523
2000
200000
970120
1000000
2
Returns: 132268155610
5000
200000
747474
1000000
3
Returns: 100313573544
10000
200000
576012
1000000
7
Returns: 49886010031
10000
200000
340012
1000000
1
Returns: 199617390763
100000
200000
587
10
10
Returns: 326273
100000
200000
6857
1000000
2
Returns: 132785538345
100000
200000
2
1000000
1
Returns: 199953962709
100000
200000
689750
1000000
1000000
Returns: 1422121
100000
200000
17
6857
2000
Returns: 2615897
100000
200000
14754
1000000
20000
Returns: 233104145
200000
200000
3
10
1
Returns: 1799955
200000
200000
6975
1000
1
Returns: 199784871
200000
200000
570100
1000000
1
Returns: 199979860029
200000
200000
666535
1000000
2
Returns: 132943883378
200000
200000
4774
1
1000
Returns: -1
200000
200000
69
100
2
Returns: 13189529
200000
200000
69865
1000003
3
Returns: 100091322915
200000
200000
0
1000000
3
Returns: 100359325661
200000
200000
588586
1000000
4
Returns: 80239508718
200000
200000
333230
1000000
5
Returns: 66731492289
200000
200000
99801
888888
10
Returns: 32629937999
200000
200000
17
100000
50
Returns: 802400340
200000
200000
29
1995
100
Returns: 8081175
200000
200000
25824
1000000
500
Returns: 871629835
200000
200000
440132
1000002
2000
Returns: 339008698
200000
200000
379102
1000000
5000
Returns: 182790275
200000
200000
6
100078
20000
Returns: 17136043
200000
200000
880173
1000000
100000
Returns: 13327242
200000
200000
55
1000000
200000
Returns: 2109421
200000
200000
1715
1000000
8
Returns: 44539558848
200000
10
100000
100000
2
Returns: 699932
199999
199995
58
458488
1
Returns: 91684113757
199999
199999
47
1000000
2
Returns: 133269854716
5
200000
671
1000000
4
Returns: 75754093436