結果

問題 No.885 アマリクエリ
ユーザー betrue12betrue12
提出日時 2019-09-13 21:51:57
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,033 ms / 2,000 ms
コード長 1,770 bytes
コンパイル時間 1,727 ms
コンパイル使用メモリ 177,588 KB
実行使用メモリ 13,832 KB
最終ジャッジ日時 2023-09-17 07:10:56
合計ジャッジ時間 7,325 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,033 ms
13,656 KB
testcase_01 AC 379 ms
13,708 KB
testcase_02 AC 265 ms
13,748 KB
testcase_03 AC 273 ms
13,748 KB
testcase_04 AC 274 ms
13,760 KB
testcase_05 AC 220 ms
13,748 KB
testcase_06 AC 195 ms
13,832 KB
testcase_07 AC 42 ms
4,376 KB
testcase_08 AC 180 ms
13,520 KB
testcase_09 AC 392 ms
13,744 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 4 ms
4,380 KB
testcase_14 AC 4 ms
4,376 KB
testcase_15 AC 4 ms
4,376 KB
testcase_16 AC 4 ms
4,376 KB
testcase_17 AC 4 ms
4,380 KB
testcase_18 AC 4 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 3 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

template<typename T>
struct SparseTable{
    vector<vector<T>> dat;
    vector<int> lookup;
    typedef function<T(T a, T b)> Func;
    Func f;
    T e;

    SparseTable(){}
    SparseTable(const vector<T> &v, Func f, T e){ initialize(v, f, e); }
    void initialize(const vector<T> &v, Func fin, T ein){
        f = fin;
        e = ein;
        int b = 0;
        while((1<<b) <= v.size()) b++;
        int N = 1<<b;
        dat.assign(b, vector<T>(N, e));
        for(int i=0; i<v.size(); i++) dat[0][i] = v[i];

        for(int i=1; i<b; i++){
            int d = 1<<i;
            for(int j=0; j+d<=N; j++){
                dat[i][j] = f(dat[i-1][j], dat[i-1][j+d/2]);
            }
        }
        lookup.resize(N+1, 0);
        for(int i=2; i<=N; i++){
            lookup[i] = lookup[i>>1] + 1;
        }
    }

    T between(int l, int r) {
        r++;
        int b = lookup[r-l];
        return f(dat[b][l], dat[b][r-(1<<b)]);
    }
};

int main(){
    int N, Q;
    cin >> N;
    vector<int> A(N);
    for(int i=0; i<N; i++) cin >> A[i];
    cin >> Q;
    vector<int> X(Q);
    for(int i=0; i<Q; i++) cin >> X[i];
    SparseTable<int> st(X, [](int a, int b){ return min(a, b);}, 2e9);

    vector<int64_t> imos(Q+1);
    for(int i=0; i<N; i++){
        int p = 0, a = A[i];
        while(p < Q){
            int ng = p-1, ok = Q;
            while(ok-ng>1){
                int mid = (ok+ng)/2;
                (st.between(p, mid) <= a ? ok : ng) = mid;
            }
            imos[p] += a;
            imos[ok] -= a;
            if(ok < Q) a %= X[ok];
            p = ok;
        }
    }
    for(int i=1; i<Q; i++) imos[i] += imos[i-1];
    for(int i=0; i<Q; i++) cout << imos[i] << endl;
    return 0;
}
0