結果

問題 No.1093 区間の和 / Sum of Range
ユーザー CELICA
提出日時 2020-08-11 15:36:22
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 38 ms / 2,000 ms
コード長 1,071 bytes
コンパイル時間 1,801 ms
コンパイル使用メモリ 174,932 KB
実行使用メモリ 6,808 KB
最終ジャッジ日時 2024-10-09 11:26:36
合計ジャッジ時間 5,774 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, k;
    cin >> n >> k;
    vector<ll> va(n);

    for (int i = 0; i < n; ++i)
        cin >> va[i];

    int q;
    cin >> q;

    vector<ll> vx(q);
    for (int i = 0; i < q; ++i)
        cin >> vx[i];

    vector<ll> vs(n - k + 1);
    for (int i = 0; i < k; ++i)
        vs[0] += va[i];

    for (int i = 1; i < n - k + 1; ++i)
        vs[i] = vs[i - 1] - va[i - 1] + va[i + k - 1];

    sort(vs.begin(), vs.end());

    vector<ll> res;
    for (int i = 0; i < q; ++i)
    {
        vector<ll>::iterator it = upper_bound(vs.begin(), vs.end(), vx[i]);
        if (it == vs.begin())
        {
            res.push_back(0);
            continue;
        }
        if (it == vs.end())
        {
            res.push_back(n - k + 1);
            continue;
        }
        ll d = distance(vs.begin(), --it) + 1;
        res.push_back(d);
    }

    for (const auto& iv : res)
        printf("%lld\n", iv);

    return 0;
}
0