結果

問題 No.1890 Many Sequences Sum Queries
ユーザー hiragnhiragn
提出日時 2022-12-03 22:07:49
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,226 ms / 2,000 ms
コード長 776 bytes
コンパイル時間 130 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 10,880 KB
最終ジャッジ日時 2024-04-19 08:43:08
合計ジャッジ時間 8,376 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,226 ms
10,752 KB
testcase_01 AC 748 ms
10,752 KB
testcase_02 AC 29 ms
10,624 KB
testcase_03 AC 30 ms
10,752 KB
testcase_04 AC 30 ms
10,880 KB
testcase_05 AC 30 ms
10,752 KB
testcase_06 AC 30 ms
10,880 KB
testcase_07 AC 394 ms
10,752 KB
testcase_08 AC 95 ms
10,624 KB
testcase_09 AC 119 ms
10,752 KB
testcase_10 AC 704 ms
10,880 KB
testcase_11 AC 348 ms
10,752 KB
testcase_12 AC 714 ms
10,752 KB
testcase_13 AC 736 ms
10,752 KB
testcase_14 AC 400 ms
10,880 KB
testcase_15 AC 30 ms
10,752 KB
testcase_16 AC 30 ms
10,752 KB
testcase_17 AC 29 ms
10,752 KB
testcase_18 AC 30 ms
10,752 KB
testcase_19 AC 30 ms
10,752 KB
testcase_20 AC 29 ms
10,752 KB
testcase_21 AC 30 ms
10,752 KB
testcase_22 AC 30 ms
10,752 KB
testcase_23 AC 29 ms
10,880 KB
testcase_24 AC 30 ms
10,880 KB
testcase_25 AC 30 ms
10,624 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from itertools import accumulate
from bisect import bisect_left


def main():
    n, q = map(int, input().split())
    a = [0] + list(map(int, input().split()))
    aa = [ai * (ai + 1) // 2 for ai in a]
    acc = list(accumulate(aa))
    for _ in range(q):
        s = int(input())
        if s > acc[-1]:
            print(-1)
            continue

        k = bisect_left(acc, s)
        if s == acc[k]:
            print(sum(a[:k + 1]))
            continue

        left = 0
        right = a[k] + 1
        while right - left > 1:
            mid = (right + left) // 2
            if acc[k - 1] + mid * (mid + 1) // 2 >= s:
                right = mid
            else:
                left = mid
        print(sum(a[:k]) + right)


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