test

test

package main

import (
    "bufio"
    . "fmt"
    "os"
)

/**
题目链接: https://www.luogu.com.cn/problem/P3372
模板题
*/
func main() {
    type seg []struct {
        l, r      int
        todo, sum int64
    }

    var update func(t seg, o, l, r int, add int64)
    var query func(t seg, o, l, r int) int64
    var build func(t seg, a []int64, o, l, r int)

    do := func(t seg, o int, add int64) {
        t[o].todo += add
        t[o].sum += int64(t[o].r-t[o].l+1) * add // % mod
    }

    pushUp := func(t seg, o int) {
        t[o].sum = t[o<<1].sum + t[o<<1|1].sum
    }

    pushDown := func(t seg, o int) {
        if add := t[o].todo; add != 0 {
            do(t, o<<1, add)
            do(t, o<<1|1, add)
            t[o].todo = 0
        }
    }

    build = func(t seg, a []int64, o, l, r int) {
        t[o].l, t[o].r = l, r
        if l == r {
            t[o].sum = a[l-1]
            return
        }
        m := (l + r) >> 1
        build(t, a, o<<1, l, m)
        build(t, a, o<<1|1, m+1, r)
        pushUp(t, o)
    }

    update = func(t seg, o, l, r int, add int64) {
        if l <= t[o].l && t[o].r <= r {
            do(t, o, add)
            return
        }
        pushDown(t, o)
        m := (t[o].l + t[o].r) >> 1
        if l <= m {
            update(t, o<<1, l, r, add)
        }
        if m < r {
            update(t, o<<1|1, l, r, add)
        }
        pushUp(t, o)
    }

    query = func(t seg, o, l, r int) int64 {
        if l <= t[o].l && t[o].r <= r {
            return t[o].sum
        }
        pushDown(t, o)
        m := (t[o].l + t[o].r) >> 1
        if r <= m {
            return query(t, o<<1, l, r)
        }
        if l > m {
            return query(t, o<<1|1, l, r)
        }
        return query(t, o<<1, l, r) + query(t, o<<1|1, l, r)
    }

    r, w := bufio.NewReader(os.Stdin), bufio.NewWriter(os.Stdout)
    var n, m, op, x, y int
    Fscan(r, &n, &m)
    a := make([]int64, n)
    for i := range a {
        Fscan(r, &a[i])
    }
    t := make(seg, 4*len(a))
    build(t, a, 1, 1, len(a))
    for i := 0; i < m; i++ {
        var k int64
        Fscan(r, &op)
        if op == 1 {
            Fscan(r, &x, &y, &k)
            update(t, 1, x, y, k)
        } else {
            Fscan(r, &x, &y)
            Fprintln(w, query(t, 1, x, y))
        }
    }
    _ = w.Flush()
}

上一篇:Spring Boot 2.x基础教程:配置线程池的拒绝策略


下一篇:24_Go游戏-函数版学生管理系统