結果

問題 No.1890 Many Sequences Sum Queries
ユーザー ace_amuro
提出日時 2023-03-30 13:56:06
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 188 ms / 2,000 ms
コード長 789 bytes
コンパイル時間 711 ms
コンパイル使用メモリ 81,856 KB
実行使用メモリ 11,520 KB
最終ジャッジ日時 2024-09-22 01:50:15
合計ジャッジ時間 4,186 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<ctime>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<stack>
#include<map>
using namespace std;
typedef long long LL;
const int MR=1e6+10;
int n,Q,a[MR];
LL S,sa[MR],sb[MR],sc[MR];
int main(){
    cin>>n>>Q;
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=1;i<=n;i++){
        sa[i]=sa[i-1]+a[i];
        sb[i]=sb[i-1]+1ll*a[i]*(a[i]+1)/2;
    }   
    for(int i=1;i<MR;i++){
        sc[i]=sc[i-1]+i;
    }
    while(Q--) {
        cin>>S;
        if(S>sb[n]){
            printf("-1\n");
            continue;
        }
        int i=lower_bound(sb+1,sb+1+n,S)-sb;
        int j=lower_bound(sc+1,sc+MR,S-sb[i-1])-sc;
        printf("%lld\n",sa[i-1]+j);
    }
    return 0;
}
0