結果

問題 No.1441 MErGe
ユーザー c-yanc-yan
提出日時 2021-03-28 23:35:55
言語 Go
(1.22.1)
結果
AC  
実行時間 835 ms / 1,000 ms
コード長 2,288 bytes
コンパイル時間 10,708 ms
コンパイル使用メモリ 235,944 KB
実行使用メモリ 9,672 KB
最終ジャッジ日時 2024-05-06 23:01:35
合計ジャッジ時間 25,206 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 4 ms
6,944 KB
testcase_04 AC 4 ms
6,944 KB
testcase_05 AC 12 ms
6,944 KB
testcase_06 AC 13 ms
6,940 KB
testcase_07 AC 13 ms
6,940 KB
testcase_08 AC 144 ms
6,940 KB
testcase_09 AC 144 ms
6,940 KB
testcase_10 AC 192 ms
6,940 KB
testcase_11 AC 173 ms
6,944 KB
testcase_12 AC 224 ms
6,940 KB
testcase_13 AC 741 ms
9,672 KB
testcase_14 AC 608 ms
9,668 KB
testcase_15 AC 635 ms
9,668 KB
testcase_16 AC 582 ms
9,668 KB
testcase_17 AC 593 ms
9,668 KB
testcase_18 AC 498 ms
9,668 KB
testcase_19 AC 563 ms
9,672 KB
testcase_20 AC 437 ms
9,668 KB
testcase_21 AC 375 ms
7,624 KB
testcase_22 AC 527 ms
7,624 KB
testcase_23 AC 815 ms
9,672 KB
testcase_24 AC 835 ms
9,672 KB
testcase_25 AC 761 ms
9,668 KB
testcase_26 AC 736 ms
9,668 KB
testcase_27 AC 746 ms
9,668 KB
testcase_28 AC 640 ms
9,668 KB
testcase_29 AC 643 ms
9,668 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) int {
	return sort.Search(st.size, func(i int) bool { return st.get(i) >= value })
}

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)
		y := st.bisectLeft(r+1) - 1
		if T == 1 {
			for j := l + 1; j < r+1; j++ {
				st.rangeAdd(st.bisectLeft(j), st.bisectLeft(j+1), 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