結果

問題 No.1890 Many Sequences Sum Queries
ユーザー DrDrpilotDrDrpilot
提出日時 2022-06-09 13:13:16
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 309 ms / 2,000 ms
コード長 783 bytes
コンパイル時間 413 ms
コンパイル使用メモリ 81,736 KB
実行使用メモリ 76,744 KB
最終ジャッジ日時 2023-10-21 04:28:57
合計ジャッジ時間 4,262 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 277 ms
75,792 KB
testcase_01 AC 299 ms
76,004 KB
testcase_02 AC 40 ms
53,472 KB
testcase_03 AC 39 ms
53,472 KB
testcase_04 AC 39 ms
53,472 KB
testcase_05 AC 40 ms
53,472 KB
testcase_06 AC 40 ms
53,472 KB
testcase_07 AC 174 ms
76,496 KB
testcase_08 AC 108 ms
76,668 KB
testcase_09 AC 95 ms
75,852 KB
testcase_10 AC 309 ms
76,744 KB
testcase_11 AC 198 ms
76,220 KB
testcase_12 AC 246 ms
76,132 KB
testcase_13 AC 252 ms
76,096 KB
testcase_14 AC 157 ms
75,820 KB
testcase_15 AC 39 ms
53,472 KB
testcase_16 AC 40 ms
53,472 KB
testcase_17 AC 39 ms
53,472 KB
testcase_18 AC 40 ms
53,472 KB
testcase_19 AC 41 ms
53,472 KB
testcase_20 AC 40 ms
53,472 KB
testcase_21 AC 40 ms
53,472 KB
testcase_22 AC 39 ms
53,472 KB
testcase_23 AC 40 ms
53,472 KB
testcase_24 AC 40 ms
53,472 KB
testcase_25 AC 40 ms
53,472 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import bisect
from itertools import accumulate
n,q=map(int,input().split())
a=list(map(int,input().split()))
a_ind=list(accumulate(a))
b=[]
for i in a:
    b.append(i*(i+1)//2)
b=list(accumulate(b))
for _ in range(q):
    s=int(input())
    if s>b[-1]:
        print(-1)
        continue
    ind=bisect.bisect_right(b,s)
    if ind==0:
        ok=a[0];ng=0
        while ok-ng>1:
            mid=(ok+ng)//2
            if mid*(mid+1)//2>=s:
                ok=mid
            else:
                ng=mid
        print(ok)
        continue
    s-=b[ind-1]
    if s==0:
        print(a_ind[ind-1])
        continue
    ok=a[ind];ng=0
    while ok-ng>1:
        mid=(ok+ng)//2
        if mid*(mid+1)//2>=s:
            ok=mid
        else:
            ng=mid
    print(a_ind[ind-1]+ok)
0