結果

問題 No.2065 Sum of Min
ユーザー NAMIDAIRONAMIDAIRO
提出日時 2022-09-02 22:25:00
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 340 ms / 2,000 ms
コード長 2,269 bytes
コンパイル時間 1,036 ms
コンパイル使用メモリ 113,828 KB
実行使用メモリ 14,984 KB
最終ジャッジ日時 2024-04-27 22:00:59
合計ジャッジ時間 8,535 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,948 KB
testcase_03 AC 2 ms
6,940 KB
testcase_04 AC 246 ms
14,444 KB
testcase_05 AC 244 ms
14,248 KB
testcase_06 AC 280 ms
14,384 KB
testcase_07 AC 259 ms
14,360 KB
testcase_08 AC 324 ms
14,184 KB
testcase_09 AC 299 ms
14,352 KB
testcase_10 AC 285 ms
14,984 KB
testcase_11 AC 273 ms
14,348 KB
testcase_12 AC 320 ms
14,308 KB
testcase_13 AC 325 ms
14,532 KB
testcase_14 AC 322 ms
14,368 KB
testcase_15 AC 324 ms
14,348 KB
testcase_16 AC 325 ms
14,512 KB
testcase_17 AC 340 ms
14,508 KB
testcase_18 AC 340 ms
14,452 KB
testcase_19 AC 334 ms
14,492 KB
testcase_20 AC 326 ms
14,420 KB
testcase_21 AC 318 ms
14,516 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