Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- M = 200003
- Z = 100003
- COUNT = 0
- class Record:
- def __init__(self, uid=-1, v=-1):
- self.uid = uid
- self.v = v
- def seek_pos(uid, table, skip=True):
- bucket = uid % M
- while table[bucket] and table[bucket].uid != uid and \
- (table[bucket].uid != -1 or skip):
- bucket = (bucket + Z) % M
- return bucket
- def put(uid, v, table):
- bucket = seek_pos(uid, table, skip=False)
- global COUNT
- if table[bucket] and COUNT:
- COUNT -= 1
- table[bucket] = Record(uid, v)
- def get(uid, table):
- bucket = seek_pos(uid, table)
- if not table[bucket]:
- return False
- return table[bucket].v
- def delete(uid, table):
- bucket = seek_pos(uid, table)
- if not table[bucket]:
- return False
- table[bucket].uid = -1
- global COUNT
- COUNT += 1
- return table[bucket].v
- def reset(table):
- hash_table = [None] * M
- for record in table:
- if record and record.uid != -1:
- put(record.uid, record.v, hash_table)
- return hash_table
- def process_commands(n):
- global COUNT
- hash_table = [None] * M
- for _ in range(n):
- args = (input() + ' 0').split()[:3]
- cmd = args[0]
- uid = int(args[1])
- v = int(args[2])
- res = {'put': lambda uid, v: put(uid, v, hash_table),
- 'get': lambda uid, _: get(uid, hash_table),
- 'delete': lambda k, _: delete(uid, hash_table)}[cmd](uid, v)
- [0 if res is None else print(None) if res is False else print(res)]
- if COUNT > M / 10:
- hash_table = reset(hash_table)
- n = int(input())
- process_commands(n)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement