from typing import Optional, List class Heap: def __init__( self ) -> None: self.data : List[ List[ int ] ] = [] self.refs : List[ int ] = [] def boundcheck( self, obj_id: int, index: int ) -> bool: return obj_id >= 0 and \ obj_id < len( self.data ) and \ index < len( self.data[ obj_id ] ) def put( self, obj_id: int ) -> None: if obj_id <= 0 or not self.boundcheck( obj_id, 0 ): return self.refs[ obj_id ] -= 1 if self.refs[ obj_id ] == 0: for val in self.data[ obj_id ]: self.put( val ) self.data[ obj_id ] = [] def get( self, obj_id: int ) -> None: if self.boundcheck( obj_id, 0 ): self.refs[ obj_id ] += 1 def read( self, obj_id: int, index: int ) -> Optional[int]: if not self.boundcheck( obj_id, index ): return None return self.data[ obj_id ][ index ] def write( self, obj_id: int, index: int, value: int ) -> bool: if not self.boundcheck( obj_id, index ): return False self.get( value ) self.put( self.data[ obj_id ][ index ] ) self.data[ obj_id ][ index ] = value return True def count( self ) -> int: return 1 + sum( 1 if x else 0 for x in self.refs ) def make( self, size: int ) -> int: self.data.append( [ 0 for _ in range( size ) ] ) self.refs.append( 0 ) return len( self.data ) - 1 import run_tests