結果

問題 No.2065 Sum of Min
ユーザー NAMIDAIRONAMIDAIRO
提出日時 2022-09-02 22:25:00
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 373 ms / 2,000 ms
コード長 2,269 bytes
コンパイル時間 1,322 ms
コンパイル使用メモリ 112,588 KB
実行使用メモリ 15,384 KB
最終ジャッジ日時 2023-08-10 04:40:16
合計ジャッジ時間 10,240 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 280 ms
14,620 KB
testcase_05 AC 278 ms
14,152 KB
testcase_06 AC 315 ms
14,812 KB
testcase_07 AC 287 ms
14,596 KB
testcase_08 AC 363 ms
15,160 KB
testcase_09 AC 340 ms
14,868 KB
testcase_10 AC 313 ms
14,208 KB
testcase_11 AC 311 ms
14,008 KB
testcase_12 AC 365 ms
14,744 KB
testcase_13 AC 366 ms
14,596 KB
testcase_14 AC 358 ms
14,312 KB
testcase_15 AC 354 ms
14,400 KB
testcase_16 AC 370 ms
15,384 KB
testcase_17 AC 368 ms
14,252 KB
testcase_18 AC 367 ms
14,012 KB
testcase_19 AC 355 ms
15,064 KB
testcase_20 AC 371 ms
14,240 KB
testcase_21 AC 373 ms
14,192 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <random>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)

template<class T>
using vi = vector<T>;

template<class T>
using vii = vector<vi<T>>;

template<class T>
using viii = vector<vii<T>>;

using pii = pair<int, int>;

template<class T>
struct segtree {
    int n;
    vector<T> d;
    const T e = 0;
    segtree(int n_ = 0) {
        n = 1;
        while (n < n_) n <<= 1;
        d.resize(2 * n - 1, e);
    }

    T op(T a, T b) { return a + b; }

    void update(int i, T x) {
        int idx = i + n - 1;
        d[idx] = x;
        while (idx) {
            idx = (idx - 1) / 2;
            d[idx] = op(d[2 * idx + 1], d[2 * idx + 2]);
        }
        return;
    }

    void add(int i, T x) {
        int idx = i + n - 1;
        d[idx] += x;
        while (idx) {
            idx = (idx - 1) / 2;
            d[idx] = op(d[2 * idx + 1], d[2 * idx + 2]);
        }
        return;
    }

    T get(int l, int r) { return get_sub(l, r, 0, 0, n); }

    T get_sub(int l, int r, int i, int ok, int ng) {
        if (l <= ok && ng <= r) return d[i];
        if (r <= ok || ng <= l) return e;
        int mid = (ok + ng) / 2;
        T vl = get_sub(l, r, 2 * i + 1, ok, mid);
        T vr = get_sub(l, r, 2 * i + 2, mid, ng);
        return op(vl, vr);
    }
};

using tii = tuple<ll, int, int>;

int main()
{
    int n, q;
    cin >> n >> q;
    vi<ll> a(n), l(q), r(q), x(q);
    rep(i, n) cin >> a[i];
    rep(i, q) cin >> l[i] >> r[i] >> x[i];
    rep(i, q) l[i]--;
    
    vi<tii> d;
    rep(i, n) d.push_back({ a[i], 0, i });
    rep(i, q) d.push_back({ x[i], 1, i });
    sort(d.begin(), d.end());

    segtree<ll> seg(n + 10), segn(n + 10);

    vi<ll> ans(q);
    rep(i, n + q) {
        ll num, ax, pos;
        tie(num, ax, pos) = d[i];
        if (ax == 0) {
            seg.add(pos, num);
            segn.add(pos, 1);
        }
        else if (ax == 1) {
            ll acum = seg.get(l[pos], r[pos]);
            ll cnt = segn.get(l[pos], r[pos]);
            ans[pos] = acum + x[pos] * (r[pos] - l[pos] - cnt);
        }
    }

    for (ll res : ans) cout << res << endl;
    return 0;
}
0