結果

問題 No.1234 典型RMQ
ユーザー c-yanc-yan
提出日時 2021-02-07 04:50:54
言語 Go
(1.22.1)
結果
AC  
実行時間 154 ms / 2,000 ms
コード長 3,103 bytes
コンパイル時間 11,590 ms
コンパイル使用メモリ 218,984 KB
実行使用メモリ 13,952 KB
最終ジャッジ日時 2024-04-26 11:48:38
合計ジャッジ時間 16,698 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 131 ms
7,936 KB
testcase_07 AC 95 ms
5,376 KB
testcase_08 AC 137 ms
8,448 KB
testcase_09 AC 117 ms
5,760 KB
testcase_10 AC 129 ms
8,320 KB
testcase_11 AC 128 ms
7,936 KB
testcase_12 AC 118 ms
5,632 KB
testcase_13 AC 95 ms
5,376 KB
testcase_14 AC 113 ms
5,632 KB
testcase_15 AC 112 ms
5,504 KB
testcase_16 AC 129 ms
8,320 KB
testcase_17 AC 116 ms
5,632 KB
testcase_18 AC 83 ms
5,376 KB
testcase_19 AC 138 ms
8,448 KB
testcase_20 AC 94 ms
7,936 KB
testcase_21 AC 132 ms
7,936 KB
testcase_22 AC 152 ms
13,440 KB
testcase_23 AC 150 ms
13,440 KB
testcase_24 AC 150 ms
13,440 KB
testcase_25 AC 154 ms
13,952 KB
testcase_26 AC 150 ms
13,440 KB
testcase_27 AC 1 ms
5,376 KB
testcase_28 AC 1 ms
5,376 KB
testcase_29 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

func min(x, y int) int {
	if x < y {
		return x
	}
	return y
}

const (
	defaultValue = math.MaxInt64
	defaultLazy  = 0
)

type segmentTree struct {
	offset int
	values []int
	lazy   []int
}

func newSegmentTree(n int) segmentTree {
	var result segmentTree
	t := 1
	for t < n {
		t *= 2
	}
	result.offset = t - 1
	result.values = make([]int, 2*t-1)
	result.lazy = make([]int, 2*t-1)
	for i := 0; i < 2*t-1; i++ {
		result.values[i] = defaultValue
		result.lazy[i] = defaultLazy
	}
	return result
}

func op(x, y int) int {
	return min(x, y)
}

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

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

func (st segmentTree) propagate(segments []int) {
	for _, i := range segments {
		indexes := make([]int, 0, 20)
		for i != 0 {
			i = (i - 1) / 2
			indexes = append(indexes, i)
		}
		for len(indexes) != 0 {
			j := indexes[len(indexes)-1]
			indexes = indexes[:len(indexes)-1]
			if st.lazy[j] == defaultLazy {
				continue
			}
			st.lazy[j*2+1] += st.lazy[j]
			st.values[j*2+1] += st.lazy[j]
			st.lazy[j*2+2] += st.lazy[j]
			st.values[j*2+2] += st.lazy[j]
			st.lazy[j] = defaultLazy
		}
	}
}

func (st segmentTree) apply(start, stop int, value int) {
	segments := st.segments(start, stop)
	st.propagate(segments)
	for _, i := range segments {
		st.lazy[i] += value
		st.values[i] += value
		for i != 0 {
			i = (i - 1) / 2
			st.values[i] = op(st.values[i*2+1], st.values[i*2+2])
		}
	}
}

func (st segmentTree) query(start, stop int) int {
	segments := st.segments(start, stop)
	st.propagate(segments)
	result := defaultValue
	for _, i := range segments {
		result = op(result, st.values[i])
	}
	return result
}

func main() {
	defer flush()

	N := readInt()
	a := make([]int, N)
	for i := 0; i < N; i++ {
		a[i] = readInt()
	}
	Q := readInt()

	st := newSegmentTree(N)
	st.build(a)
	for i := 0; i < Q; i++ {
		k := readInt()
		l := readInt()
		r := readInt()
		c := readInt()
		if k == 1 {
			st.apply(l-1, r, c)
		} else if k == 2 {
			println(st.query(l-1, r))
		}
	}
}

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