結果

問題 No.1441 MErGe
ユーザー c-yanc-yan
提出日時 2021-03-28 23:34:17
言語 Go
(1.22.1)
結果
AC  
実行時間 756 ms / 1,000 ms
コード長 2,319 bytes
コンパイル時間 12,637 ms
コンパイル使用メモリ 207,624 KB
実行使用メモリ 9,912 KB
最終ジャッジ日時 2023-08-19 15:58:08
合計ジャッジ時間 25,340 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 4 ms
4,380 KB
testcase_04 AC 5 ms
4,376 KB
testcase_05 AC 13 ms
4,380 KB
testcase_06 AC 13 ms
4,376 KB
testcase_07 AC 13 ms
4,380 KB
testcase_08 AC 134 ms
5,496 KB
testcase_09 AC 129 ms
5,496 KB
testcase_10 AC 197 ms
5,508 KB
testcase_11 AC 175 ms
5,496 KB
testcase_12 AC 196 ms
5,508 KB
testcase_13 AC 614 ms
9,896 KB
testcase_14 AC 631 ms
9,892 KB
testcase_15 AC 663 ms
9,896 KB
testcase_16 AC 603 ms
9,892 KB
testcase_17 AC 592 ms
9,892 KB
testcase_18 AC 510 ms
9,912 KB
testcase_19 AC 593 ms
9,912 KB
testcase_20 AC 464 ms
9,904 KB
testcase_21 AC 394 ms
7,824 KB
testcase_22 AC 512 ms
7,836 KB
testcase_23 AC 750 ms
9,908 KB
testcase_24 AC 753 ms
9,908 KB
testcase_25 AC 751 ms
9,908 KB
testcase_26 AC 756 ms
9,912 KB
testcase_27 AC 750 ms
9,908 KB
testcase_28 AC 677 ms
9,868 KB
testcase_29 AC 682 ms
9,868 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

type rangeAddSegmentTree struct {
	size   int
	offset int
	data   []int
}

func newRangeAddSegmentTree(n int) rangeAddSegmentTree {
	var result rangeAddSegmentTree
	result.size = n
	t := 1
	for t < n {
		t *= 2
	}
	result.offset = t - 1
	result.data = make([]int, 2*t-1)
	return result
}

func (st rangeAddSegmentTree) rangeAdd(start, stop, value int) {
	l := start + st.offset
	r := stop + st.offset
	for l < r {
		if l&1 == 0 {
			st.data[l] += value
		}
		if r&1 == 0 {
			st.data[r-1] += value
		}
		l /= 2
		r = (r - 1) / 2
	}
}

func (st rangeAddSegmentTree) get(index int) int {
	i := index + st.offset
	result := st.data[i]
	for i > 0 {
		i = (i - 1) / 2
		result += st.data[i]
	}
	return result
}

func (st rangeAddSegmentTree) set(index, value int) {
	st.rangeAdd(index, index+1, value-st.get(index))
}

func (st rangeAddSegmentTree) bisectLeft(value int, lo int) int {
	return sort.Search(st.size-lo, func(i int) bool { return st.get(i+lo) >= value }) + lo
}

func main() {
	defer flush()

	N := readInt()
	Q := readInt()

	A := make([]int, N)
	for i := 0; i < N; i++ {
		A[i] = readInt()
		if i != 0 {
			A[i] += A[i-1]
		}
	}

	st := newRangeAddSegmentTree(N + 1)
	for i := 0; i < N+1; i++ {
		st.rangeAdd(i, i+1, i)
	}

	for i := 0; i < Q; i++ {
		T := readInt()
		l := readInt() - 1
		r := readInt() - 1

		x := st.bisectLeft(l, 0)
		y := st.bisectLeft(r+1, 0) - 1
		if T == 1 {
			for j := l + 1; j < r+1; j++ {
				st.rangeAdd(st.bisectLeft(j, 0), st.bisectLeft(j+1, 0), l-j)
			}
			st.rangeAdd(y+1, N+1, l-r)
		} else if T == 2 {
			if x != 0 {
				println(A[y] - A[x-1])
			} else {
				println(A[y])
			}
		}
	}
}

const (
	ioBufferSize = 1 * 1024 * 1024 // 1 MB
)

var stdinScanner = func() *bufio.Scanner {
	result := bufio.NewScanner(os.Stdin)
	result.Buffer(make([]byte, ioBufferSize), ioBufferSize)
	result.Split(bufio.ScanWords)
	return result
}()

func readString() string {
	stdinScanner.Scan()
	return stdinScanner.Text()
}

func readInt() int {
	result, err := strconv.Atoi(readString())
	if err != nil {
		panic(err)
	}
	return result
}

var stdoutWriter = bufio.NewWriter(os.Stdout)

func flush() {
	stdoutWriter.Flush()
}

func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(stdoutWriter, args...)
}
0