結果

問題 No.1705 Mode of long array
ユーザー 小野寺健小野寺健
提出日時 2021-12-17 11:48:06
言語 Go
(1.22.1)
結果
AC  
実行時間 213 ms / 3,000 ms
コード長 1,552 bytes
コンパイル時間 11,866 ms
コンパイル使用メモリ 209,616 KB
実行使用メモリ 10,040 KB
最終ジャッジ日時 2023-10-12 13:38:05
合計ジャッジ時間 18,645 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 3 ms
4,352 KB
testcase_04 AC 3 ms
4,352 KB
testcase_05 AC 3 ms
4,348 KB
testcase_06 AC 7 ms
4,352 KB
testcase_07 AC 8 ms
4,348 KB
testcase_08 AC 8 ms
4,352 KB
testcase_09 AC 7 ms
4,348 KB
testcase_10 AC 7 ms
4,352 KB
testcase_11 AC 7 ms
4,348 KB
testcase_12 AC 7 ms
4,352 KB
testcase_13 AC 97 ms
10,024 KB
testcase_14 AC 66 ms
5,548 KB
testcase_15 AC 72 ms
4,348 KB
testcase_16 AC 84 ms
4,352 KB
testcase_17 AC 61 ms
4,352 KB
testcase_18 AC 51 ms
4,348 KB
testcase_19 AC 91 ms
5,504 KB
testcase_20 AC 57 ms
5,500 KB
testcase_21 AC 78 ms
10,024 KB
testcase_22 AC 109 ms
10,020 KB
testcase_23 AC 28 ms
4,348 KB
testcase_24 AC 27 ms
4,348 KB
testcase_25 AC 29 ms
4,348 KB
testcase_26 AC 29 ms
4,352 KB
testcase_27 AC 28 ms
4,348 KB
testcase_28 AC 29 ms
4,348 KB
testcase_29 AC 29 ms
4,356 KB
testcase_30 AC 29 ms
4,352 KB
testcase_31 AC 29 ms
4,348 KB
testcase_32 AC 28 ms
4,348 KB
testcase_33 AC 70 ms
10,016 KB
testcase_34 AC 70 ms
10,012 KB
testcase_35 AC 70 ms
10,012 KB
testcase_36 AC 69 ms
10,012 KB
testcase_37 AC 70 ms
10,012 KB
testcase_38 AC 70 ms
10,016 KB
testcase_39 AC 69 ms
10,012 KB
testcase_40 AC 69 ms
10,016 KB
testcase_41 AC 70 ms
10,012 KB
testcase_42 AC 68 ms
10,012 KB
testcase_43 AC 213 ms
10,040 KB
testcase_44 AC 213 ms
10,040 KB
testcase_45 AC 209 ms
10,040 KB
testcase_46 AC 211 ms
10,040 KB
testcase_47 AC 211 ms
10,040 KB
testcase_48 AC 211 ms
10,040 KB
testcase_49 AC 111 ms
10,028 KB
testcase_50 AC 109 ms
10,028 KB
testcase_51 AC 109 ms
10,028 KB
testcase_52 AC 111 ms
10,028 KB
testcase_53 AC 107 ms
10,020 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

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() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Scan()
	sc.Scan()
	M, _ := strconv.Atoi(sc.Text())	
	var tree SegmentTree
	tree.new(M)
	for i := 0; i < M; i++ {
		sc.Scan()
		a, _ := strconv.Atoi(sc.Text())
		tree.update(i, a)
	}

	sc.Scan()
	Q, _ := strconv.Atoi(sc.Text())	
	for i := 0; i < Q; i++ {
		sc.Scan()
		t, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		x, _ := strconv.Atoi(sc.Text())
		sc.Scan()
		y, _ := strconv.Atoi(sc.Text())
		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.Printf("%d\n", tree.max())
		}
	}
}
0