結果

問題 No.1441 MErGe
ユーザー c-yanc-yan
提出日時 2021-03-28 13:41:46
言語 Go
(1.22.1)
結果
TLE  
実行時間 -
コード長 2,370 bytes
コンパイル時間 16,223 ms
コンパイル使用メモリ 212,520 KB
実行使用メモリ 13,744 KB
最終ジャッジ日時 2023-08-19 15:33:23
合計ジャッジ時間 27,684 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,328 KB
testcase_01 AC 2 ms
4,356 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,364 KB
testcase_04 AC 2 ms
4,360 KB
testcase_05 AC 9 ms
4,360 KB
testcase_06 AC 9 ms
4,360 KB
testcase_07 AC 8 ms
4,360 KB
testcase_08 AC 101 ms
4,516 KB
testcase_09 AC 99 ms
4,472 KB
testcase_10 AC 165 ms
4,684 KB
testcase_11 AC 172 ms
4,400 KB
testcase_12 AC 151 ms
4,792 KB
testcase_13 AC 453 ms
8,516 KB
testcase_14 AC 485 ms
9,620 KB
testcase_15 TLE -
testcase_16 AC 429 ms
9,608 KB
testcase_17 AC 430 ms
8,392 KB
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

type segmentTree struct {
	data   []int
	offset int
}

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

func (st segmentTree) updateAll(a []int) {
	for i, v := range a {
		st.data[st.offset+i] = v
	}
	for i := st.offset - 1; i > -1; i-- {
		st.data[i] = st.data[i*2+1] + st.data[i*2+2]
	}
}

func (st segmentTree) add(index, value int) {
	i := st.offset + index
	st.data[i] += value
	for i >= 1 {
		i = (i - 1) / 2
		st.data[i] = st.data[i*2+1] + st.data[i*2+2]
	}
}

func (st segmentTree) update(index, value int) {
	i := st.offset + index
	st.data[i] = value
	for i >= 1 {
		i = (i - 1) / 2
		st.data[i] = st.data[i*2+1] + st.data[i*2+2]
	}
}

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

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 := newSegmentTree(N)
	for i := 0; i < Q; i++ {
		T := readInt()
		l := readInt() - 1
		r := readInt() - 1

		a := 0
		b := l
		for {
			c := st.query(a, b)
			if c == 0 {
				break
			}
			a = b
			b += c
		}
		x := b

		if T == 1 {
			st.add(x, r-l)
		} else if T == 2 {
			a := 0
			b := r
			for {
				c := st.query(a, b+1)
				if c == 0 {
					break
				}
				a = b + 1
				b += c
			}
			y := b
			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