結果
問題 | No.1099 Range Square Sum |
ユーザー | ccppjsrb |
提出日時 | 2020-06-26 23:10:07 |
言語 | Go (1.22.1) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,470 bytes |
コンパイル時間 | 16,447 ms |
コンパイル使用メモリ | 231,636 KB |
実行使用メモリ | 18,816 KB |
最終ジャッジ日時 | 2024-07-04 23:38:39 |
合計ジャッジ時間 | 19,553 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | WA | - |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | AC | 1 ms
5,376 KB |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | WA | - |
testcase_30 | WA | - |
ソースコード
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.c 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 }