差分(O(n))

一个区间的修改,很自然的想到差分的操作。数据量很小,直接模拟也能过。

package main

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

const(
    N = 110
)
var n,m int
var arr,sub [N]int
func main() {
    in := bufio.NewReader(os.Stdin)
    out := bufio.NewWriter(os.Stdout)
    defer out.Flush()
    
    Fscan(in,&n)
    for i := 1;i <= n;i++ {
        Fscan(in,&arr[i])
    }
    for i := 1;i <= n;i++ {
        sub[i] = arr[i] - arr[i - 1]
    }
    Fscan(in,&m)
    for i := 0;i < m;i++ {
        var a,b,c int
        Fscan(in,&a,&b,&c)
        sub[a] += c
        sub[b + 1] -= c
    }
    sum := 0
    for i := 1;i <= n;i++ {
        sum += sub[i]
        Fprintf(out,"%d ",sum)
    }
}