結果

問題 No.2065 Sum of Min
ユーザー wanuiwanui
提出日時 2022-09-02 22:28:47
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 148 ms / 2,000 ms
コード長 3,120 bytes
コンパイル時間 2,375 ms
コンパイル使用メモリ 229,560 KB
実行使用メモリ 13,444 KB
最終ジャッジ日時 2024-04-27 22:05:11
合計ジャッジ時間 6,885 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 80 ms
12,816 KB
testcase_05 AC 74 ms
12,696 KB
testcase_06 AC 111 ms
12,824 KB
testcase_07 AC 69 ms
12,816 KB
testcase_08 AC 123 ms
12,820 KB
testcase_09 AC 111 ms
12,820 KB
testcase_10 AC 92 ms
12,816 KB
testcase_11 AC 86 ms
12,952 KB
testcase_12 AC 132 ms
12,808 KB
testcase_13 AC 138 ms
12,692 KB
testcase_14 AC 137 ms
12,820 KB
testcase_15 AC 137 ms
12,816 KB
testcase_16 AC 137 ms
13,084 KB
testcase_17 AC 132 ms
12,812 KB
testcase_18 AC 134 ms
12,816 KB
testcase_19 AC 148 ms
12,944 KB
testcase_20 AC 145 ms
13,444 KB
testcase_21 AC 136 ms
12,820 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
// clang-format off
using namespace std; using ll=long long; using ull=unsigned long long; using pll=pair<ll,ll>; const ll INF=4e18;
void print0(){}; template<typename H,typename... T> void print0(H h,T... t){cout<<h;print0(t...);}
void print(){print0("\n");}; template<typename H,typename... T>void print(H h,T... t){print0(h);if(sizeof...(T)>0)print0(" ");print(t...);}
void perr0(){}; template<typename H,typename... T> void perr0(H h,T... t){cerr<<h;perr0(t...);}
void perr(){perr0("\n");}; template<typename H,typename... T>void perr(H h,T... t){perr0(h);if(sizeof...(T)>0)perr0(" ");perr(t...);}
void ioinit() { cout<<fixed<<setprecision(15); cerr<<fixed<<setprecision(6); ios_base::sync_with_stdio(0); cin.tie(0); }
#define rep(i,n) for(int i=0;i<(n);i++)
// clang-format on

using SGT = ll;
class segment_tree {
   public:
    ll size;
    vector<SGT> data;
    SGT default_value;

    segment_tree(ll n) {
        default_value = get_default_value();

        ll siz = 1;
        while (siz < n) {
            siz = siz * 2;
        }
        data.resize(siz * 2, default_value);
        size = siz;
    }

    ~segment_tree() {}

    //----------------------------
    SGT compare(SGT& a, SGT& b) {
        return a + b;
    }
    SGT get_default_value() {
        return 0;
    }
    //----------------------------

    void update(ll k, SGT value) {
        k += size - 1;
        data[k] = value;

        while (k > 0) {
            k = (k - 1) / 2;
            data[k] = compare(data[k * 2 + 1], data[k * 2 + 2]);
        }
    }

    SGT query_open(ll a, ll b, ll k, ll l, ll r) {
        if (r <= a || b <= l) {
            return default_value;
        }
        if (a <= l && r <= b) {
            return data[k];
        }

        SGT vl = query_open(a, b, k * 2 + 1, l, (l + r) / 2);
        SGT vr = query_open(a, b, k * 2 + 2, (l + r) / 2, r);
        return compare(vl, vr);
    }

    SGT query(ll a, ll b) {
        return query_open(a, b + 1, 0, 0, size);
    }
};
using liii = tuple<ll, int, int, int>;
using pli = pair<ll, int>;
int main() {
    // Xの大きい順
    ioinit();
    int N, Q;
    cin >> N >> Q;
    vector<ll> A(N);
    rep(i, N) {
        cin >> A[i];
    }

    vector<pli> B(N);
    rep(i, N) {
        B[i] = {A[i], i};
    }
    sort(B.rbegin(), B.rend());

    vector<liii> queries;
    rep(i, Q) {
        int l, r;
        ll x;
        cin >> l >> r >> x;
        l--;
        r--;
        queries.push_back({x, l, r, i});
    }
    sort(queries.rbegin(), queries.rend());
    auto sumseg = segment_tree(N);
    auto lgseg = segment_tree(N);
    rep(i, N) {
        sumseg.update(i, A[i]);
    }

    vector<ll> ans(Q);
    int bi = 0;
    for (auto que : queries) {
        ll x;
        int l, r, qid;
        tie(x, l, r, qid) = que;
        while (bi < N && x < B[bi].first) {
            int j = B[bi].second;
            lgseg.update(j, 1);
            sumseg.update(j, 0);
            bi++;
        }
        ans[qid] = sumseg.query(l, r) + x * lgseg.query(l, r);
    }
    rep(i, Q) {
        print(ans[i]);
    }
    return 0;
}
0