package main import ( "bufio" "fmt" "os" "strconv" ) func configure(scanner *bufio.Scanner) { scanner.Split(bufio.ScanWords) scanner.Buffer(make([]byte, 1000005), 1000005) } func getNextString(scanner *bufio.Scanner) string { scanned := scanner.Scan() if !scanned { panic("scan failed") } return scanner.Text() } func getNextInt(scanner *bufio.Scanner) int { i, _ := strconv.Atoi(getNextString(scanner)) return i } func getNextInt64(scanner *bufio.Scanner) int64 { i, _ := strconv.ParseInt(getNextString(scanner), 10, 64) return i } func getNextFloat64(scanner *bufio.Scanner) float64 { i, _ := strconv.ParseFloat(getNextString(scanner), 64) return i } func main() { fp := os.Stdin wfp := os.Stdout extra := 0 if os.Getenv("I") == "IronMan" { fp, _ = os.Open(os.Getenv("END_GAME")) extra = 100 } scanner := bufio.NewScanner(fp) configure(scanner) writer := bufio.NewWriter(wfp) defer func() { r := recover() if r != nil { fmt.Fprintln(writer, r) } writer.Flush() }() solve(scanner, writer) for i := 0; i < extra; i++ { fmt.Fprintln(writer, "-----------------------------------") solve(scanner, writer) } } func solve(scanner *bufio.Scanner, writer *bufio.Writer) { n := getNextInt(scanner) seg := newSegment(n) for i := 0; i < n; i++ { for h := 0; h < len(seg); h++ { seg[h][i>>uint(h)].c++ } } for i := 0; i < n; i++ { a := getNextInt64(scanner) seg.update(i, i+1, a, seg.add) } q := getNextInt(scanner) for i := 0; i < q; i++ { op := getNextInt(scanner) l := getNextInt(scanner) - 1 r := getNextInt(scanner) switch op { case 1: x := getNextInt64(scanner) seg.update(l, r, x, seg.add) case 2: ans := seg.query(l, r, 0, seg.add) fmt.Fprintln(writer, ans) } } } type lazy struct { v, w, s, c int64 p bool } func (l *lazy) hold(w int64) { l.w = w l.p = true } func (l *lazy) assign() { l.v += l.s*l.w*2 + l.w*l.w*l.c l.s += l.w l.w = 0 l.p = false } func (l *lazy) eval() int64 { if l.p { return l.v + l.s*l.w*2 + l.w*l.w*l.c } return l.v } type segment [][]lazy func newSegment(n int) segment { seg := make(segment, 0) for n > 1 { seg = append(seg, make([]lazy, n)) n = (n + 1) >> 1 } seg = append(seg, make([]lazy, 1)) return seg } func (seg segment) push(h, i int, f func(x, y int64) int64) { for h = h + 1; h < len(seg); h++ { i >>= 1 if i<<1+1 < len(seg[h-1]) { seg[h][i].v = f(seg[h-1][i<<1].eval(), seg[h-1][i<<1+1].eval()) continue } seg[h][i].v = seg[h-1][i<<1].eval() } } func (seg segment) pull(h, i int) { if h >= len(seg) { return } seg.pull(h+1, i>>1) if seg[h][i].p { if h > 0 { seg[h-1][i<<1].hold(seg[h][i].w) if i<<1+1 < len(seg[h-1]) { seg[h-1][i<<1+1].hold(seg[h][i].w) } } seg[h][i].assign() } } func (seg segment) update(l, r int, v int64, f func(x, y int64) int64) { for h := 0; h < len(seg) && l < r; h++ { if l&1 == 1 { seg.pull(h, l) seg[h][l].hold(v) seg.push(h, l, f) l++ } l >>= 1 if r&1 == 1 { seg.pull(h, r-1) seg[h][r-1].hold(v) seg.push(h, r-1, f) } r >>= 1 } } func (seg segment) query(l, r int, s int64, f func(x, y int64) int64) int64 { for h := 0; h < len(seg) && l < r; h++ { if l&1 == 1 { seg.pull(h, l) s = f(s, seg[h][l].eval()) l++ } l >>= 1 if r&1 == 1 { seg.pull(h, r-1) s = f(s, seg[h][r-1].eval()) } r >>= 1 } return s } func (seg segment) add(x, y int64) int64 { return x + y }