結果
| 問題 |
No.1234 典型RMQ
|
| コンテスト | |
| ユーザー |
c-yan
|
| 提出日時 | 2021-02-07 01:49:55 |
| 言語 | Go (1.23.4) |
| 結果 |
AC
|
| 実行時間 | 213 ms / 2,000 ms |
| コード長 | 3,559 bytes |
| コンパイル時間 | 16,295 ms |
| コンパイル使用メモリ | 219,492 KB |
| 実行使用メモリ | 8,064 KB |
| 最終ジャッジ日時 | 2024-11-09 02:38:41 |
| 合計ジャッジ時間 | 22,128 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 27 |
ソースコード
package main
import (
"bufio"
"fmt"
"os"
"strconv"
)
func min(x, y int) int {
if x < y {
return x
}
return y
}
const (
defaultValue = 1000000000000000000
defaultLazy = 0
)
type segmentTree struct {
offset int
value []int
lazy []int
}
func newSegmentTree(n int) segmentTree {
var result segmentTree
t := 1
for t < n {
t *= 2
}
result.offset = t - 1
result.value = make([]int, 2*t-1)
result.lazy = make([]int, 2*t-1)
for i := 0; i < 2*t-1; i++ {
result.value[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.value[st.offset+i] = v
}
for i := st.offset - 1; i > -1; i-- {
st.value[i] = op(st.value[i*2+1], st.value[i*2+2])
}
}
func (st segmentTree) propagateTo(index int, value int) {
st.lazy[index] += value
st.value[index] += value
}
func (st segmentTree) propagateAt(index int) {
if index != 0 {
st.propagateAt(st.getParentIndex(index))
}
if st.lazy[index] == defaultLazy {
return
}
st.propagateTo(index*2+1, st.lazy[index])
st.propagateTo(index*2+2, st.lazy[index])
st.lazy[index] = defaultLazy
}
func (st segmentTree) recalcAt(index int) {
st.value[index] = op(st.value[index*2+1], st.value[index*2+2])
if index != 0 {
st.recalcAt(st.getParentIndex(index))
}
}
func (st segmentTree) applyAt(index int, value int) {
st.lazy[index] += value
st.value[index] += value
if index != 0 {
st.recalcAt(st.getParentIndex(index))
}
}
func (st segmentTree) getParentIndex(index int) int {
if index < 1 {
panic("BUG: doesn't exists parent of 0")
}
return (index - 1) / 2
}
func (st segmentTree) propagate(start, stop int) {
l := start + st.offset
r := stop + st.offset
for l < r {
if l&1 == 0 {
if l != 0 {
st.propagateAt(st.getParentIndex(l))
}
}
if r&1 == 0 {
if r-1 != 0 {
st.propagateAt(st.getParentIndex(r - 1))
}
}
l = l / 2
r = (r - 1) / 2
}
}
func (st segmentTree) apply(start, stop int, value int) {
st.propagate(start, stop)
l := start + st.offset
r := stop + st.offset
for l < r {
if l&1 == 0 {
st.applyAt(l, value)
}
if r&1 == 0 {
st.applyAt(r-1, value)
}
l = l / 2
r = (r - 1) / 2
}
}
func (st segmentTree) query(start, stop int) int {
st.propagate(start, stop)
result := defaultValue
l := start + st.offset
r := stop + st.offset
for l < r {
if l&1 == 0 {
result = op(result, st.value[l])
}
if r&1 == 0 {
result = op(result, st.value[r-1])
}
l = l / 2
r = (r - 1) / 2
}
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...)
}
c-yan