結果
| 問題 |
No.647 明太子
|
| コンテスト | |
| ユーザー |
norioc
|
| 提出日時 | 2025-09-16 23:03:02 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 168 ms / 4,500 ms |
| コード長 | 2,078 bytes |
| コンパイル時間 | 318 ms |
| コンパイル使用メモリ | 82,376 KB |
| 実行使用メモリ | 80,152 KB |
| 最終ジャッジ日時 | 2025-09-16 23:03:06 |
| 合計ジャッジ時間 | 3,894 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 20 |
ソースコード
from heapq import heappush, heappop
class FenwickTree:
def __init__(self, n: int):
self.data = [0] * (n+10)
self.n = (n+10)
def add(self, p: int, x: int):
assert 0 <= p < self.n
p += 1
while p < len(self.data):
self.data[p] += x
p += p & -p
def sum(self, p: int) -> int:
"""区間 [0, p] の和"""
assert 0 <= p < self.n
p += 1
s = 0
while p > 0:
s += self.data[p]
p -= p & -p
return s
def rangesum(self, l: int, r: int) -> int:
"""区間 [l, r] の和"""
assert 0 <= l <= r < self.n
s = self.sum(r)
if l > 0:
s -= self.sum(l-1)
return s
class Compression:
def __init__(self, iterable):
self.vs = sorted(set(iterable))
self.v2i = {}
for i, val in enumerate(self.vs):
self.v2i[val] = i
def __len__(self):
return len(self.vs)
def index(self, val):
"""val のインデックスを返す"""
return self.v2i[val]
def value(self, index):
"""インデックスに対応する値を返す"""
return self.vs[index]
def map(self, iterable):
return [self.index(x) for x in iterable]
N = int(input())
ps = []
spice_levels = set()
q = []
for i in range(N):
# A円以内, 辛さB以上
A, B = map(int, input().split())
spice_levels.add(B)
heappush(q, (-A, 0, B, i))
M = int(input())
items = []
for i in range(M):
# X円, 辛さY
X, Y = map(int, input().split())
spice_levels.add(Y)
heappush(q, (-X, 1, Y, i))
comp = Compression(spice_levels)
max_spice = len(comp)
ft = FenwickTree(max_spice)
freq = [0] * M
while q:
_, t, spice, ind = heappop(q)
if t == 0: # 購入者
ft.add(comp.index(spice), 1)
else: # 明太子
s = comp.index(spice)
cnt = ft.sum(s)
freq[ind] += cnt
ma = max(freq)
if ma == 0:
print(0)
else:
for i in range(M):
if freq[i] == ma:
print(i+1)
norioc