結果

問題 No.1438 Broken Drawers
ユーザー 👑 tatt61880tatt61880
提出日時 2021-03-27 12:08:00
言語 Kuin
(KuinC++ v.2021.9.17)
結果
AC  
実行時間 488 ms / 2,000 ms
コード長 3,943 bytes
コンパイル時間 3,385 ms
コンパイル使用メモリ 171,932 KB
実行使用メモリ 28,964 KB
最終ジャッジ日時 2023-10-14 17:58:40
合計ジャッジ時間 12,290 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,372 KB
testcase_02 AC 2 ms
4,356 KB
testcase_03 AC 2 ms
4,352 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 390 ms
28,744 KB
testcase_07 AC 396 ms
28,704 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,372 KB
testcase_11 AC 15 ms
4,948 KB
testcase_12 AC 143 ms
13,232 KB
testcase_13 AC 260 ms
18,864 KB
testcase_14 AC 94 ms
10,232 KB
testcase_15 AC 451 ms
26,812 KB
testcase_16 AC 425 ms
26,428 KB
testcase_17 AC 459 ms
27,320 KB
testcase_18 AC 462 ms
27,748 KB
testcase_19 AC 478 ms
28,016 KB
testcase_20 AC 468 ms
28,392 KB
testcase_21 AC 461 ms
27,752 KB
testcase_22 AC 478 ms
28,388 KB
testcase_23 AC 488 ms
28,964 KB
testcase_24 AC 485 ms
28,788 KB
testcase_25 AC 488 ms
28,928 KB
testcase_26 AC 485 ms
28,892 KB
testcase_27 AC 485 ms
28,708 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

func main()
	var n: int :: cui@inputInt()
	var a: []bool :: #[n + 1]bool
	var map: Map :: #Map
	for i(0, n - 1)
		var key: int :: cui@inputInt()
		var value: int :: i
		do map.add(key, value)
	end for
	var it: Node :: map.begin()
	var ans: int :: 0
	for(1, n)
		var value: int :: it.value
		if(!a[value + 1])
			do a[value] :: true
			do ans :+ 1
		end if
		do it :: it.next
	end for
	do cui@print("\{ans}\n")
	
	class Node()
		+var height: int
		+var key: int
		+var value: int
		+var prev: Node
		+var next: Node
		+var lst: Node
		+var rst: Node
		+func init(key: int, value: int, prev: Node, next: Node): Node
			do me.height :: 1
			do me.key :: key
			do me.value :: value
			do me.prev :: prev
			do me.next :: next
			ret me
		end func
	end class
	func height(t: Node): int
		ret t =& null ?(0, t.height)
	end func
	func bias(t: Node): int
		ret height(t.lst) - height(t.rst)
	end func
	func modHeight(t: Node)
		do t.height :: 1 + lib@max(height(t.lst), height(t.rst))
	end func
	func rotateL(v: Node): Node
		var u: Node :: v.rst
		var t: Node :: u.lst
		do u.lst :: v
		do v.rst :: t
		ret u
	end func
	func rotateR(u: Node): Node
		var v: Node :: u.lst
		var t: Node :: v.rst
		do v.rst :: u
		do u.lst :: t
		ret v
	end func
	func rotateLR(t: Node): Node
		do t.lst :: rotateL(t.lst)
		ret rotateR(t)
	end func
	func rotateRL(t: Node): Node
		do t.rst :: rotateR(t.rst)
		ret rotateL(t)
	end func
	
	; AVL Tree
	class Map()
		var root: Node
		var change: bool
		var lmax: int
		var num: int
		+func size(): int
			ret me.num
		end func
		+func begin(): Node
			var t: Node :: me.root
			while(true)
				if(t.lst =& null)
					ret t
				end if
				do t :: t.lst
			end while
		end func
		+func balanceL(t: Node): Node
			if(!me.change)
				ret t
			end if
			var h: int :: height(t)
			if(bias(t) = 2)
				if(bias(t.lst) >= 0)
					do t :: rotateR(t)
				else
					do t :: rotateLR(t)
				end if
			else
				do modHeight(t)
			end if
			do me.change :: (h <> height(t))
			ret t
		end func
		+func balanceR(t: Node): Node
			if(!me.change)
				ret t
			end if
			var h: int :: height(t)
			if(bias(t) = -2)
				if(bias(t.rst) <= 0)
					do t :: rotateL(t)
				else
					do t :: rotateRL(t)
				end if
			else
				do modHeight(t)
			end if
			do me.change :: (h <> height(t))
			ret t
		end func
		+func add(key: int, value: int)
			do me.root :: me.addSub(me.root, null, key, value)
		end func
		+func addSub(t: Node, root: Node, key: int, value: int): Node
			if(t =& null)
				var a: Node
				do me.change :: true
				if(root =& null)
					do a :: (#Node).init(key, value, null, null)
				elif(key < root.key)
					do a :: (#Node).init(key, value, root.prev, root)
					if(root.prev <>& null)
						do root.prev.next :: a
					end if
					do root.prev :: a
				elif(key > root.key)
					do a :: (#Node).init(key, value, root, root.next)
					if(root.next <>& null)
						do root.next.prev :: a
					end if
					do root.next :: a
				end if
				do me.num :+ 1
				ret a
			elif(key < t.key)
				do t.lst :: me.addSub(t.lst, t, key, value)
				ret me.balanceL(t)
			elif(key > t.key)
				do t.rst :: me.addSub(t.rst, t, key, value)
				ret me.balanceR(t)
			else
				do me.change :: false
				do t.value :: value
				do me.num :+ 1
				ret t
			end if
		end func
		+func find(key: int): Node
			var t: Node :: me.root
			while loop(t <>& null)
				if(key < t.key)
					do t :: t.lst
				elif(key > t.key)
					do t :: t.rst
				else
					break loop
				end if
			end while
			ret t
		end func
		+func lower_bound(key: int): Node
			var t: Node :: me.root
			if(t =& null)
				ret null
			end if
			while(true)
				if(key < t.key)
					if(t.lst =& null)
						ret t
					end if
					if(key > t.lst.key & t.lst.rst =& null)
						ret t
					end if
					do t :: t.lst
				elif(key > t.key)
					if(t.rst =& null)
						ret null
					end if
					do t :: t.rst
				else
					ret t
				end if
			end while
		end func
	end class
end func
0