結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,500 KB
testcase_03 AC 4 ms
4,376 KB
testcase_04 AC 4 ms
4,376 KB
testcase_05 AC 11 ms
4,380 KB
testcase_06 AC 12 ms
4,376 KB
testcase_07 AC 11 ms
4,376 KB
testcase_08 AC 119 ms
5,500 KB
testcase_09 AC 114 ms
5,496 KB
testcase_10 AC 171 ms
5,504 KB
testcase_11 AC 157 ms
5,496 KB
testcase_12 AC 177 ms
5,504 KB
testcase_13 AC 553 ms
9,896 KB
testcase_14 AC 558 ms
9,892 KB
testcase_15 AC 610 ms
9,900 KB
testcase_16 AC 587 ms
9,896 KB
testcase_17 AC 543 ms
9,896 KB
testcase_18 AC 465 ms
9,912 KB
testcase_19 AC 528 ms
9,912 KB
testcase_20 AC 415 ms
9,900 KB
testcase_21 AC 352 ms
7,824 KB
testcase_22 AC 458 ms
7,840 KB
testcase_23 AC 679 ms
9,908 KB
testcase_24 AC 661 ms
9,908 KB
testcase_25 AC 673 ms
9,912 KB
testcase_26 AC 669 ms
9,912 KB
testcase_27 AC 670 ms
9,908 KB
testcase_28 AC 609 ms
9,872 KB
testcase_29 AC 599 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) 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