求大佬指点

P3372 【模板】线段树 1

rhyvme @ 2024-07-28 17:43:29

不是RE就是MLE,找不到问题人要晕了...求大佬帮忙看看问题在哪

package main

import "fmt"

func main() {
    n, q := 0, 0
    fmt.Scanf("%d %d", &n, &q)

    arr := make([]int, 100005)
    segTree := make([]int, 270000)
    lazy := make([]int, 270000)
    var buildTree func(l, r, p int)
    var update func(l, r, c, s, t, p int)
    var getSum func(l, r, s, t, p int) int
    var maintain func(cl, cr, p int)

    buildTree = func(l, r, p int) {
        if l == r {
            segTree[p] = arr[l]
            return
        }
        m := (r-l)>>1 + l
        buildTree(l, m, p<<1)
        buildTree(m+1, r, p<<1+1)
        segTree[p] = segTree[p<<1] + segTree[p<<1+1]
    }

    update = func(l, r, c, s, t, p int) {
        if l <= s && r >= t {
            segTree[p] += c * (t - s + 1)
            lazy[p] += c
            return
        }
        m := (t-s)>>1 + s
        maintain(s, t, p)
        if l <= m {
            update(l, r, c, s, m, p<<1)
        }
        if r > m {
            update(l, r, c, m+1, t, p<<1+1)
        }
        segTree[p] = segTree[p<<1] + segTree[p<<1+1]
    }

    getSum = func(l, r, s, t, p int) int {
        if l <= s && r >= t {
            return segTree[p]
        }
        m := (t-s)>>1 + s
        maintain(s, t, p)
        sum := 0
        if l <= m {
            sum = getSum(l, r, s, m, p<<1)
        }
        if r > m {
            sum += getSum(l, r, m+1, t, p<<1+1)
        }
        return sum
    }

    maintain = func(cl, cr, p int) {
        cm := (cr-cl)>>1 + cl
        if lazy[p] != 0 {
            lazy[p<<1] += lazy[p]
            lazy[p<<1+1] += lazy[p]
            segTree[p<<1] += (cm - cl + 1) * lazy[p]
            segTree[p<<1+1] += (cr - cm) * lazy[p]
            lazy[p] = 0
        }
    }

    for i := 1; i <= n; i++ {
        fmt.Scanf("%d", &arr[i])
    }

    buildTree(1, n, 1)

    for i := 0; i < q; i++ {
        op, x, y := 0, 0, 0
        fmt.Scanf("%d %d %d", &op, &x, &y)

        if op == 1 {
            c := 0
            fmt.Scanf("%d", &c)
            // [x,y] += k
            update(x, y, c, 1, n, 1)
        } else {
            // sum [x,y]
            fmt.Println(getSum(x, y, 1, n, 1))
        }
    }
}

|