結果

問題 No.875 Range Mindex Query
ユーザー 👑 tatt61880tatt61880
提出日時 2021-04-11 03:31:35
言語 Kuin
(KuinC++ v.2021.9.17)
結果
AC  
実行時間 298 ms / 2,000 ms
コード長 2,181 bytes
コンパイル時間 2,737 ms
コンパイル使用メモリ 164,728 KB
実行使用メモリ 6,424 KB
最終ジャッジ日時 2023-10-14 18:25:49
合計ジャッジ時間 5,676 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 3 ms
4,348 KB
testcase_02 AC 3 ms
4,356 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 3 ms
4,352 KB
testcase_06 AC 2 ms
4,352 KB
testcase_07 AC 3 ms
4,352 KB
testcase_08 AC 2 ms
4,352 KB
testcase_09 AC 3 ms
4,352 KB
testcase_10 AC 3 ms
4,348 KB
testcase_11 AC 213 ms
5,996 KB
testcase_12 AC 176 ms
5,068 KB
testcase_13 AC 159 ms
6,304 KB
testcase_14 AC 146 ms
6,268 KB
testcase_15 AC 215 ms
6,392 KB
testcase_16 AC 273 ms
6,424 KB
testcase_17 AC 293 ms
6,248 KB
testcase_18 AC 298 ms
6,344 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

func main()
	var n: int :: cui@inputInt()
	var q: int :: cui@inputInt()
	var seg: @SegmentTree :: (#@SegmentTree).init(n)
	var a: []int :: #[n]int
	for i(0, n - 1)
		do a[i] :: cui@inputInt() - 1
		do seg.update(i, a[i] * n + i)
	end for
	
	for(1, q)
		var type: int :: cui@inputInt()
		var l: int :: cui@inputInt() - 1
		var r: int :: cui@inputInt() - 1
		if(type = 1)
			do @swap(&a[l], &a[r])
			do seg.update(l, a[l] * n + l)
			do seg.update(r, a[r] * n + r)
		else
			var idx: int :: seg.query(l, r + 1) % n
			do cui@print("\{idx + 1}\n")
		end if
	end for
end func

func swap(a: &int, b: &int)
	var t: int :: a
	do a :: b
	do b :: t
end func

class SegmentTree()
	const inf_: int :: lib@intMax
	var size: int
	var node: []int
	+func init(n: int): SegmentTree
		do me.size :: 1
		while(me.size < n)
			do me.size :* 2
		end while
		do me.node :: #[2 * me.size]int
		for i(0, 2 * me.size - 1)
			do me.node[i] :: inf_
		end for
		ret me
	end func
	+func update(idx: int, val: int)
		do idx :+ me.size - 1
		do me.node[idx] :: val
		while(idx > 0)
			do idx :: (idx - 1) / 2
			do me.node[idx] :: lib@min(me.node[2 * idx + 1], me.node[2 * idx + 2])
		end while
	end func
	+func query(a: int, b: int): int
		ret me.querySub(a, b, 0, 0, me.size)
	end func
	func querySub(a: int, b: int, k: int, l: int, r: int): int
		if(b <= l | r <= a)
			ret inf_
		end if
		if(a <= l & r <= b)
			ret me.node[k]
		end if
		var vl: int :: me.querySub(a, b, 2 * k + 1, l, (l + r) / 2)
		var vr: int :: me.querySub(a, b, 2 * k + 2, (l + r) / 2, r)
		ret lib@min(vl, vr)
	end func
	+*func toStr(): []char
		var padding: int :: 1
		while(padding <= me.size)
			do padding :* 2
		end while
		do padding :- 1
		var res: []char :: ""
		var s: int :: 0
		var e: int :: 0
		var n: int :: 1
		const digit: int :: 6
		var fmt: []char :: " \{digit}d"
		var strInf: []char :: " ".repeat(digit - 3) ~ "inf"
		while(padding > 0)
			do padding :/ 2
			for i(s, e)
				do res :~ " ".repeat((digit + 1) * padding)
				do res :~ "\{me.node[i] = inf_ ?(strInf, me.node[i].toStrFmt(fmt))}|"
			end for
			do res :~ "\n"
			do n :* 2
			do s :: e + 1
			do e :: s + n - 1
		end while
		ret res
	end func
end class
0