結果

問題 No.1093 区間の和 / Sum of Range
ユーザー kibou1136kibou1136
提出日時 2021-12-15 08:50:00
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 230 ms / 2,000 ms
コード長 1,367 bytes
コンパイル時間 1,858 ms
コンパイル使用メモリ 172,724 KB
実行使用メモリ 6,732 KB
最終ジャッジ日時 2023-10-01 02:27:12
合計ジャッジ時間 9,554 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 115 ms
4,604 KB
testcase_02 AC 52 ms
4,380 KB
testcase_03 AC 52 ms
6,176 KB
testcase_04 AC 41 ms
6,136 KB
testcase_05 AC 166 ms
4,600 KB
testcase_06 AC 38 ms
4,380 KB
testcase_07 AC 94 ms
4,680 KB
testcase_08 AC 93 ms
6,200 KB
testcase_09 AC 143 ms
4,380 KB
testcase_10 AC 93 ms
6,164 KB
testcase_11 AC 140 ms
4,568 KB
testcase_12 AC 96 ms
5,080 KB
testcase_13 AC 3 ms
4,380 KB
testcase_14 AC 150 ms
4,380 KB
testcase_15 AC 43 ms
4,524 KB
testcase_16 AC 183 ms
4,860 KB
testcase_17 AC 140 ms
4,380 KB
testcase_18 AC 100 ms
4,376 KB
testcase_19 AC 12 ms
4,380 KB
testcase_20 AC 75 ms
4,376 KB
testcase_21 AC 172 ms
4,420 KB
testcase_22 AC 171 ms
4,888 KB
testcase_23 AC 116 ms
4,380 KB
testcase_24 AC 43 ms
6,004 KB
testcase_25 AC 145 ms
4,424 KB
testcase_26 AC 120 ms
5,940 KB
testcase_27 AC 162 ms
4,376 KB
testcase_28 AC 113 ms
5,944 KB
testcase_29 AC 230 ms
6,732 KB
testcase_30 AC 28 ms
4,376 KB
testcase_31 AC 43 ms
4,628 KB
testcase_32 AC 60 ms
4,580 KB
testcase_33 AC 174 ms
6,180 KB
testcase_34 AC 13 ms
4,376 KB
testcase_35 AC 169 ms
4,376 KB
testcase_36 AC 158 ms
6,204 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

vector<long long> value;
long long n, K, N;
vector<long long> S, A;

void update(int i, int x){
    i += N - 1;
    value[i] += x;
    while (i > 0){
        i = (i - 1)/2;
        value[i] = value[2*i + 1] + value[2*i + 2];
    }
}

int query(int a, int b, int k, int l, int r){
    if ( r <= a ||  b <= l ) return 0;
    if (a <= l && r <= b){
        return value[k];
    }else{
        int c1 = query(a, b, 2*k + 1, l, (l + r)/2);
        int c2 = query(a, b, 2*k + 2, (l + r)/2, r);
        return c1 + c2;
    }
}

bool isOK(int index, int key){
    if (key < S[index]) return true;
    else return false;
}

int binary_search(int key){
    int ng = -1;
    int ok = n - K + 1;
    while (abs(ok - ng) > 1){
        int mid = (ok + ng)/2;
        if(isOK(mid,key)) ok = mid;
        else ng = mid;
    }
    return ok;
}

int main(){
    cin >> n >> K;
    N = 1;
    while (N < n) N *= 2;
    A.resize(n);
    S.assign(n - K + 1, 0);

    value = vector<long long>(2*N - 1, 0);
    for (int i = 0; i < n; ++i){
        cin >> A[i];
        update(i, A[i]);
    }
    int q; cin >> q;
    for (int i = 0; i <= n - K; ++i){
        S[i] = query(i, i + K, 0, 0, N);
    }
    sort(S.begin(), S.end());
    
    for (int j = 0; j < q; ++j){
        int t; cin >> t;
        cout << binary_search(t) << endl;
    }


}
0