結果

問題 No.1705 Mode of long array
コンテスト
ユーザー 小野寺健
提出日時 2021-12-17 11:00:01
言語 Go
(1.26.1)
コンパイル:
env GOCACHE=/tmp go build _filename_
実行:
./Main
結果
AC  
実行時間 1,795 ms / 3,000 ms
コード長 1,293 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 11,163 ms
コンパイル使用メモリ 279,620 KB
実行使用メモリ 14,336 KB
最終ジャッジ日時 2026-04-04 12:22:24
合計ジャッジ時間 66,428 ms
ジャッジサーバーID
(参考情報)
judge4_0 / judge5_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 51
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

package main

import "fmt"

type KeyValue struct {
	key int
	value int
}

type SegmentTree struct {
	n int
	dat []*KeyValue
}

func (this *SegmentTree) new(n int) {
	this.n = 1
	for this.n < n {
		this.n *= 2
	}
	this.dat = make([]*KeyValue, 2*this.n-1)
	for i := 0; i < 2*this.n-1; i++ {
		this.dat[i] = &KeyValue{-1, -1}
	}
}

func (this *SegmentTree) update(k int, a int) {
	i := k
	k += this.n - 1
	this.dat[k].key = i
	this.dat[k].value = a
	for k > 0 {
		k = (k - 1) / 2
		this.dat[k] = Max(this.dat[k * 2 + 1], this.dat[k * 2 + 2])
	}
}

func (this SegmentTree) max() int {
	return this.dat[0].key + 1
}

func (this SegmentTree) get(i int) int {
	return this.dat[i + this.n - 1].value
}

func Max(a *KeyValue, b *KeyValue) *KeyValue{
	if a.value > b.value {
		return a
	} else if a.value < b.value {
		return b
	} else if a.key >= b.key {
		return a
	} else {
		return b
	}
}

func main() {
	var N, M int
	fmt.Scan(&N, &M)
	var tree SegmentTree
	tree.new(M)
	for i := 0; i < M; i++ {
		var a int
		fmt.Scan(&a)
		tree.update(i, a)
	}

	var Q int
	fmt.Scan(&Q)
	for i := 0; i < Q; i++ {
		var t, x, y int
		fmt.Scan(&t, &x, &y)
		if t == 1 {
			tree.update(x-1, tree.get(x-1) + y)
		} else if t == 2 {
			tree.update(x-1, tree.get(x-1) - y)
		} else {
			fmt.Println(tree.max())
		}
	}
}
0