結果
| 問題 | No.1347 HS Railway |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-03-30 02:36:36 |
| 言語 | PyPy3 (7.3.17) |
| 結果 |
AC
|
| 実行時間 | 419 ms / 2,000 ms |
| コード長 | 3,668 bytes |
| 記録 | |
| コンパイル時間 | 401 ms |
| コンパイル使用メモリ | 85,272 KB |
| 実行使用メモリ | 170,000 KB |
| 最終ジャッジ日時 | 2026-03-30 02:36:52 |
| 合計ジャッジ時間 | 13,311 ms |
|
ジャッジサーバーID (参考情報) |
judge3_0 / judge2_1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 50 |
ソースコード
## https://yukicoder.me/problems/no/1347
class BinaryIndexTree:
"""
フェニック木(BinaryIndexTree)の基本的な機能を実装したクラス
"""
def __init__(self, size):
self.size = size
self.array = [0] * (size + 1)
def add(self, x, a):
index = x
while index <= self.size:
self.array[index] += a
index += index & (-index)
def sum(self, x):
index = x
ans = 0
while index > 0:
ans += self.array[index]
index -= index & (-index)
return ans
def least_upper_bound(self, value):
if self.sum(self.size) < value:
return -1
elif value <= 0:
return 0
m = 1
while m < self.size:
m *= 2
k = 0
k_sum = 0
while m > 0:
k0 = k + m
if k0 < self.size:
if k_sum + self.array[k0] < value:
k_sum += self.array[k0]
k += m
m //= 2
if k < self.size:
return k + 1
else:
return -1
def main():
N = int(input())
rail_types = []
for _ in range(5):
values = tuple(map(int, input().split()))
l = values[0]
r = values[1]
stations = values[2:]
rail_types.append((l - 1, r - 1, stations))
M = int(input())
bs = []
for _ in range(M):
b, s = map(int, input().split())
bs.append((b, s))
# 各わける
bs_types = [[] for _ in range(5)]
for b, s in bs:
bs_types[b - 1].append(s)
answer = 0
for i in range(5):
for j in range(i + 1, 5):
l_i, r_i, stations_i = rail_types[i]
l_j, r_j, stations_j = rail_types[j]
# とおる駅の区間が交わらなければ交わらない
if r_j < l_i or r_i < l_j:
continue
base_l = max(l_i, l_j)
base_r = min(r_i, r_j)
i_weights = 0
j_weights = 0
coef_l_i = 0
coef_l_j = 0
coef_r_i = 0
coef_r_j = 0
for k in range(N):
if l_i < k <= r_i:
i_weights += stations_i[k - l_i - 1]
if l_j < k <= r_j:
j_weights += stations_j[k - l_j - 1]
if k == base_l:
coef_l_i = i_weights
coef_l_j = j_weights
if k == base_r:
coef_r_i = i_weights
coef_r_j = j_weights
# jについて回したい
a_set= set()
for s in bs_types[j]:
x = s + coef_l_j - coef_l_i
y = s + coef_r_j - coef_r_i
a_set.add(x)
a_set.add(y)
for s in bs_types[i]:
a_set.add(s)
a_list = list(a_set)
a_list.sort()
a_map = {}
for k, a in enumerate(a_list):
a_map[a] = k + 1
lower_bit = BinaryIndexTree(len(a_map))
for s in bs_types[i]:
lower_bit.add(a_map[s], 1)
ans = 0
for s in bs_types[j]:
x = s + coef_l_j - coef_l_i
y = s + coef_r_j - coef_r_i
ans += lower_bit.sum(a_map[min(x, y)] - 1)
ans += lower_bit.sum(lower_bit.size) - lower_bit.sum(a_map[max(x, y)])
answer += (len(bs_types[i]) * len(bs_types[j])) - ans
print(answer)
if __name__ == "__main__":
main()