結果

問題 No.1012 荷物収集
ユーザー はむ吉🐹はむ吉🐹
提出日時 2020-03-20 22:51:36
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 722 ms / 2,000 ms
コード長 1,224 bytes
コンパイル時間 620 ms
コンパイル使用メモリ 10,928 KB
実行使用メモリ 38,708 KB
最終ジャッジ日時 2023-08-21 18:00:16
合計ジャッジ時間 18,000 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,232 KB
testcase_01 AC 17 ms
8,428 KB
testcase_02 AC 16 ms
8,276 KB
testcase_03 AC 17 ms
8,144 KB
testcase_04 AC 532 ms
38,624 KB
testcase_05 AC 529 ms
38,652 KB
testcase_06 AC 524 ms
38,676 KB
testcase_07 AC 534 ms
38,684 KB
testcase_08 AC 535 ms
38,656 KB
testcase_09 AC 531 ms
38,532 KB
testcase_10 AC 533 ms
38,576 KB
testcase_11 AC 534 ms
38,708 KB
testcase_12 AC 530 ms
38,636 KB
testcase_13 AC 533 ms
38,476 KB
testcase_14 AC 18 ms
8,220 KB
testcase_15 AC 19 ms
8,512 KB
testcase_16 AC 19 ms
8,420 KB
testcase_17 AC 19 ms
8,428 KB
testcase_18 AC 19 ms
8,508 KB
testcase_19 AC 18 ms
8,364 KB
testcase_20 AC 18 ms
8,316 KB
testcase_21 AC 19 ms
8,552 KB
testcase_22 AC 19 ms
8,548 KB
testcase_23 AC 19 ms
8,224 KB
testcase_24 AC 460 ms
24,612 KB
testcase_25 AC 627 ms
32,552 KB
testcase_26 AC 387 ms
27,884 KB
testcase_27 AC 92 ms
12,988 KB
testcase_28 AC 641 ms
34,676 KB
testcase_29 AC 463 ms
33,612 KB
testcase_30 AC 683 ms
37,612 KB
testcase_31 AC 171 ms
15,600 KB
testcase_32 AC 185 ms
15,268 KB
testcase_33 AC 266 ms
17,112 KB
testcase_34 AC 272 ms
18,232 KB
testcase_35 AC 550 ms
32,292 KB
testcase_36 AC 220 ms
17,700 KB
testcase_37 AC 425 ms
32,128 KB
testcase_38 AC 480 ms
26,648 KB
testcase_39 AC 222 ms
18,620 KB
testcase_40 AC 237 ms
18,004 KB
testcase_41 AC 722 ms
36,348 KB
testcase_42 AC 300 ms
20,064 KB
testcase_43 AC 451 ms
28,680 KB
testcase_44 AC 16 ms
8,272 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env python3

import bisect
import itertools


class CumulativeSum(object):

    def __init__(self, sequence):
        self.cumulative_sum = [0]
        self.cumulative_sum.extend(itertools.accumulate(sequence))

    def partial_sum(self, first, last):
        return self.cumulative_sum[last + 1] - self.cumulative_sum[first]


def process_queries(xs0, ws0, ys):
    pairs = sorted(zip(xs0, ws0))
    xs = [x for x, _ in pairs]
    ws = [w for _, w in pairs]
    n = len(xs)
    w_cs = CumulativeSum(ws)
    xw_cs = CumulativeSum(x * w for x, w in zip(xs, ws))
    for y in ys:
        pos = bisect.bisect(xs, y)
        left_sum = y * w_cs.partial_sum(0, pos - 1) - \
            xw_cs.partial_sum(0, pos - 1)
        right_sum = xw_cs.partial_sum(
            pos, n - 1) - y * w_cs.partial_sum(pos, n - 1)
        all_sum = left_sum + right_sum
        yield all_sum


def main():
    n, q = (int(z) for z in input().split())
    xs = []
    ws = []
    for _ in range(n):
        x, w = (int(z) for z in input().split())
        xs.append(x)
        ws.append(w)
    ys = [int(z) for z in input().split()]
    for res in process_queries(xs, ws, ys):
        print(res)


if __name__ == "__main__":
    main()
0