結果

問題 No.1890 Many Sequences Sum Queries
ユーザー ThetaTheta
提出日時 2024-04-16 18:05:21
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 352 ms / 2,000 ms
コード長 1,113 bytes
コンパイル時間 469 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 77,568 KB
最終ジャッジ日時 2024-04-16 18:05:26
合計ジャッジ時間 4,992 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 309 ms
76,672 KB
testcase_01 AC 340 ms
77,312 KB
testcase_02 AC 42 ms
52,480 KB
testcase_03 AC 42 ms
51,968 KB
testcase_04 AC 42 ms
52,736 KB
testcase_05 AC 42 ms
52,096 KB
testcase_06 AC 43 ms
52,480 KB
testcase_07 AC 181 ms
76,416 KB
testcase_08 AC 115 ms
76,416 KB
testcase_09 AC 120 ms
76,416 KB
testcase_10 AC 352 ms
77,568 KB
testcase_11 AC 222 ms
77,056 KB
testcase_12 AC 256 ms
76,544 KB
testcase_13 AC 262 ms
76,544 KB
testcase_14 AC 205 ms
76,544 KB
testcase_15 AC 41 ms
52,096 KB
testcase_16 AC 42 ms
52,352 KB
testcase_17 AC 42 ms
52,608 KB
testcase_18 AC 43 ms
52,096 KB
testcase_19 AC 42 ms
52,096 KB
testcase_20 AC 42 ms
52,352 KB
testcase_21 AC 42 ms
52,352 KB
testcase_22 AC 43 ms
52,352 KB
testcase_23 AC 42 ms
52,224 KB
testcase_24 AC 42 ms
52,352 KB
testcase_25 AC 42 ms
52,480 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from bisect import bisect_left, bisect_right
from itertools import accumulate
import sys


def printe(*args, end="\n", **kwargs):
    print(*args, end=end, file=sys.stderr, **kwargs)


def main():
    N, Q = map(int, input().split())
    A = list(map(int, input().split()))
    A_accum = list(accumulate(A, initial=0))
    B_partial = [a_elm * (a_elm + 1) // 2 for a_elm in A]
    B_partial_accum = list(accumulate(B_partial, initial=0))

    printe(B_partial_accum)
    printe(A_accum)

    for _ in range(Q):
        S = int(input())
        b_p_idx = bisect_left(B_partial_accum, S)
        if b_p_idx == len(B_partial_accum):
            print(-1)
            continue
        min_a_idx = 0
        max_a_idx = A[b_p_idx - 1]
        while max_a_idx - min_a_idx > 1:
            mid_a_idx = (max_a_idx + min_a_idx) // 2
            if (1 + mid_a_idx) * mid_a_idx // 2 + \
                    B_partial_accum[b_p_idx - 1] >= S:
                max_a_idx = mid_a_idx
            else:
                min_a_idx = mid_a_idx
        print(A_accum[b_p_idx - 1] + max_a_idx)


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