Closed zronghui closed 10 months ago
link of the problem: P3384 【模板】重链剖分/树链剖分 - 洛谷 | 计算机科学教育新生态 https://www.luogu.com.cn/problem/P3384
Thank you for reaching out with the issue.
The library seems to be correct, and it appears there may be an issue in your code.
For interval addition and interval sum retrieval, each node of the SegmentTree should maintain the interval sum and interval length.
Here is an example of the implementation:
from Library_py.Graph.HLD.HLD import HLD
from Library_py.Graph.HLD.HLDLazySegmentTree import HLDLazySegmentTree
n, m, r, p = map(int, input().split())
r -= 1
a = list(map(int, input().split()))
tree = [[] for _ in range(n)]
for _ in range(n-1):
u, v = map(int, input().split())
u -= 1
v -= 1
tree[u].append(v)
tree[v].append(u)
hld = HLD(tree, r)
class Data():
def __init__(self, val, size):
self.val = val
self.size = size
def op(s: Data, t: Data):
return Data(s.val+t.val, s.size+t.size)
def mapping(f: int, s: Data):
return Data(s.val+f*s.size, s.size)
def composition(f: int, g: int):
return f + g
e = Data(0, 0)
id = 0
a = [Data(v, 1) for v in a]
hldst = HLDLazySegmentTree(
hld, n_or_a=a, op=op, mapping=mapping, composition=composition, e=e, id=id
)
for _ in range(m):
t = list(map(int, input().split()))
if t[0] == 1:
x, y, z = t[1:]
x, y = x - 1, y - 1
hldst.path_apply(x, y, z)
elif t[0] == 2:
x, y = t[1:]
x, y = x - 1, y - 1
print(hldst.path_prod(x, y).val)
elif t[0] == 3:
x, z = t[1:]
x = x - 1
hldst.subtree_apply(x, z)
elif t[0] == 4:
x = t[1] - 1
print(hldst.subtree_prod(x).val)
Sample Input:
8 10 6 623232
56 838 680 614 846 408 890 829
1 2
2 3
3 4
1 5
4 7
4 8
7 6
3 4 859
1 4 2 189
2 1 7
1 1 2 159
1 6 7 553
3 5 649
1 3 2 672
4 6
4 8
4 1
Sample Output:
7081
14299
1688
3428
Please check it.
Thanks a lot. I'm such an idiot. I forgot to consider the detail of interval length. There is no problem with the code being correct. By the way, using a class instead of a tuple causes more memory consumption. The time and space constraints on this test were strict, with 2 test cases timed out. I'm going to ignore it and do other problems of the same type.
WA in problem:
[Template] Heavy-Light Decomposition
Problem Description
Given a tree with $N$ nodes (connected and acyclic), where each node contains a value, you need to support the following operations:
1 x y z
: Add $z$ to the values of all nodes on the shortest path from node $x$ to node $y$ in the tree.2 x y
: Calculate the sum of values of all nodes on the shortest path from node $x$ to node $y$ in the tree.3 x z
: Add $z$ to the values of all nodes in the subtree rooted at node $x$.4 x
: Calculate the sum of values of all nodes in the subtree rooted at node $x$.Input Format
The first line contains four positive integers $N$, $M$, $R$, and $P$, representing the number of nodes, the number of operations, the index of the root node, and the modulo value (i.e., all output results should be modulo $P$).
The second line contains $N$ non-negative integers, representing the initial values of each node.
The next $N-1$ lines each contain two integers $x$ and $y$, indicating an edge between node $x$ and node $y$ (guaranteed to be a connected tree without cycles).
The next $M$ lines each contain a series of positive integers, representing an operation.
Output Format
Output multiple lines, each representing the result of operation $2$ or operation $4$ (modulo $P$).
Example #1
Input
Output
Note
[Data Constraints]
For $30\%$ of the data: $1 \leq N \leq 10$, $1 \leq M \leq 10$.
For $70\%$ of the data: $1 \leq N \leq {10}^3$, $1 \leq M \leq {10}^3$.
For $100\%$ of the data: $1 \leq N \leq {10}^5$, $1 \leq M \leq {10}^5$, $1 \leq R \leq N$, $1 \leq P \leq 2^{31}-1$.
[Example Explanation]
The tree structure is as follows:
![]()The operations are as follows:![]()Hence, the expected output is $2$ and $21$ in order.
my code
it can pass samples, but wa when submit. input
expected output
my output