結果
| 問題 |
No.618 labo-index
|
| コンテスト | |
| ユーザー |
maspy
|
| 提出日時 | 2020-04-08 01:25:05 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,551 bytes |
| コンパイル時間 | 223 ms |
| コンパイル使用メモリ | 81,988 KB |
| 実行使用メモリ | 124,544 KB |
| 最終ジャッジ日時 | 2024-07-08 11:38:24 |
| 合計ジャッジ時間 | 8,785 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 28 WA * 7 |
ソースコード
#!/usr/bin/ python3.8
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
class BinaryIndexedTree():
def __init__(self, seq):
self.size = len(seq)
self.depth = self.size.bit_length()
self.build(seq)
def build(self, seq):
data = seq
size = self.size
for i, x in enumerate(data):
j = i + (i & (-i))
if j < size:
data[j] += data[i]
self.data = data
def __repr__(self):
return self.data.__repr__()
def get_sum(self, i):
data = self.data
s = 0
while i:
s += data[i]
i -= i & -i
return s
def add(self, i, x):
data = self.data
size = self.size
while i < size:
data[i] += x
i += i & -i
def find_kth_element(self, k):
data = self.data
size = self.size
x, sx = 0, 0
dx = 1 << (self.depth)
for i in range(self.depth - 1, -1, -1):
dx = (1 << i)
if x + dx >= size:
continue
y = x + dx
sy = sx + data[y]
if sy < k:
x, sx = y, sy
return x + 1
Q = int(readline())
m = map(int, read().split())
query = tuple(zip(m, m))
nums = []
base = 0
for t, x in query:
if t == 1:
nums.append(x - base)
elif t == 2:
continue
else:
base += x
INF = 10 ** 18
nums.append(-INF)
nums.append(INF)
nums = sorted(set(nums))
num_rank = {x: i for i, x in enumerate(nums)}
def solve(bit, nums, full, base):
if full == 0:
return 0
if nums[1] + base > full:
return 0
data = bit.data
size = bit.size
x, sx = 0, 0
for i in range(bit.depth - 1, -1, -1):
dx = (1 << i)
if x + dx >= size:
continue
y = x + dx
sy = sx + data[y]
if y + 1 < len(nums) and full - sy >= nums[y + 1] + base:
x, sx = y, sy
cand = nums[x + 1] + base
n = bit.get_sum(x + 1)
if nums[x + 1] + 1 + base <= full - n:
return full - n
else:
return cand
bit = BinaryIndexedTree([0] * (len(nums)))
base = 0
member = []
full = 0
for t, x in query:
if t == 1:
r = num_rank[x - base]
member.append(r)
bit.add(r, 1)
full += 1
elif t == 2:
r = member[x - 1]
bit.add(r, -1)
full -= 1
else:
base += x
print(solve(bit, nums, full, base))
maspy