For problem statement at 0-999/800-899/830-839/838/problemB.txt this is a correct solution, but verifier at 0-999/800-899/830-839/838/verifierB.go ends with All tests passed can you fix the verifier? package main
import (
"io"
"os"
"strconv"
)
type Edge struct {
to int
id int
w int64
}
type Fenwick struct {
tree []int64
}
func (f *Fenwick) Add(idx int, val int64) {
for ; idx < len(f.tree); idx += idx & -idx {
f.tree[idx] += val
}
}
func (f *Fenwick) RangeAdd(L, R int, val int64) {
f.Add(L, val)
f.Add(R+1, -val)
}
func (f *Fenwick) Query(idx int) int64 {
var sum int64
for ; idx > 0; idx -= idx & -idx {
sum += f.tree[idx]
}
return sum
}
func min(a, b int64) int64 {
if a < b {
return a
}
return b
}
func main() {
input, _ := io.ReadAll(os.Stdin)
pos := 0
nextInt := func() int {
for pos < len(input) && input[pos] <= ' ' {
pos++
}
if pos >= len(input) {
return 0
}
res := 0
for pos < len(input) && input[pos] > ' ' {
res = res*10 + int(input[pos]-'0')
pos++
}
return res
}
nextInt64 := func() int64 {
for pos < len(input) && input[pos] <= ' ' {
pos++
}
if pos >= len(input) {
return 0
}
var res int64 = 0
for pos < len(input) && input[pos] > ' ' {
res = res*10 + int64(input[pos]-'0')
pos++
}
return res
}
n := nextInt()
if n == 0 {
return
}
q := nextInt()
adj := make([][]Edge, n+1)
upEdgeChild := make([]int, 2*n)
backEdgeNode := make([]int, 2*n)
weight := make([]int64, 2*n)
for i := 1; i <= n-1; i++ {
u := nextInt()
v := nextInt()
w := nextInt64()
adj[u] = append(adj[u], Edge{v, i, w})
weight[i] = w
}
for i := n; i <= 2*n-2; i++ {
u := nextInt()
_ = nextInt()
w := nextInt64()
backEdgeNode[i] = u
weight[i] = w
}
timer := 0
in := make([]int, n+1)
out := make([]int, n+1)
d := make([]int64, n+1)
var dfs func(int)
dfs = func(u int) {
timer++
in[u] = timer
for _, edge := range adj[u] {
v := edge.to
upEdgeChild[edge.id] = v
d[v] = d[u] + edge.w
dfs(v)
}
out[u] = timer
}
dfs(1)
backWeight := make([]int64, n+1)
for i := n; i <= 2*n-2; i++ {
u := backEdgeNode[i]
backWeight[u] = weight[i]
}
leaf := make([]int64, n+1)
leaf[1] = 1e18
for i := 2; i <= n; i++ {
leaf[in[i]] = d[i] + backWeight[i]
}
fenwick := &Fenwick{tree: make([]int64, n+2)}
for i := 1; i <= n; i++ {
fenwick.RangeAdd(in[i], in[i], d[i])
}
minVal := make([]int64, 4*n+1)
lazy := make([]int64, 4*n+1)
var build func(int, int, int)
build = func(node, l, r int) {
if l == r {
minVal[node] = leaf[l]
return
}
mid := (l + r) / 2
build(node*2, l, mid)
build(node*2+1, mid+1, r)
minVal[node] = min(minVal[node*2], minVal[node*2+1])
}
build(1, 1, n)
var pushDown func(int)
pushDown = func(node int) {
if lazy[node] != 0 {
lazy[node*2] += lazy[node]
minVal[node*2] += lazy[node]
lazy[node*2+1] += lazy[node]
minVal[node*2+1] += lazy[node]
lazy[node] = 0
}
}
var update func(int, int, int, int, int, int64)
update = func(node, l, r, ql, qr int, val int64) {
if ql <= l && r <= qr {
minVal[node] += val
lazy[node] += val
return
}
pushDown(node)
mid := (l + r) / 2
if ql <= mid {
update(node*2, l, mid, ql, qr, val)
}
if qr > mid {
update(node*2+1, mid+1, r, ql, qr, val)
}
minVal[node] = min(minVal[node*2], minVal[node*2+1])
}
var query func(int, int, int, int, int) int64
query = func(node, l, r, ql, qr int) int64 {
if ql <= l && r <= qr {
return minVal[node]
}
pushDown(node)
mid := (l + r) / 2
res := int64(2e18)
if ql <= mid {
res = min(res, query(node*2, l, mid, ql, qr))
}
if qr > mid {
res = min(res, query(node*2+1, mid+1, r, ql, qr))
}
return res
}
var output []byte
for i := 0; i < q; i++ {
typ := nextInt()
if typ == 1 {
idx := nextInt()
w := nextInt64()
delta := w - weight[idx]
weight[idx] = w
if idx <= n-1 {
c := upEdgeChild[idx]
fenwick.RangeAdd(in[c], out[c], delta)
update(1, 1, n, in[c], out[c], delta)
} else {
u := backEdgeNode[idx]
update(1, 1, n, in[u], in[u], delta)
}
} else {
u := nextInt()
v := nextInt()
if in[u] <= in[v] && in[v] <= out[u] {
ans := fenwick.Query(in[v]) - fenwick.Query(in[u])
output = strconv.AppendInt(output, ans, 10)
output = append(output, '\n')
} else {
du := fenwick.Query(in[u])
dv := fenwick.Query(in[v])
mu := query(1, 1, n, in[u], out[u])
ans := mu - du + dv
output = strconv.AppendInt(output, ans, 10)
output = append(output, '\n')
}
}
}
os.Stdout.Write(output)
}