結果

問題 No.2065 Sum of Min
ユーザー karinohitokarinohito
提出日時 2022-09-02 21:35:06
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 335 ms / 2,000 ms
コード長 5,467 bytes
コンパイル時間 2,753 ms
コンパイル使用メモリ 221,108 KB
実行使用メモリ 13,888 KB
最終ジャッジ日時 2023-08-10 03:29:40
合計ジャッジ時間 10,954 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 276 ms
12,564 KB
testcase_05 AC 269 ms
12,652 KB
testcase_06 AC 270 ms
12,904 KB
testcase_07 AC 277 ms
13,888 KB
testcase_08 AC 318 ms
12,888 KB
testcase_09 AC 308 ms
12,504 KB
testcase_10 AC 305 ms
12,572 KB
testcase_11 AC 305 ms
12,632 KB
testcase_12 AC 335 ms
13,760 KB
testcase_13 AC 329 ms
13,508 KB
testcase_14 AC 329 ms
12,724 KB
testcase_15 AC 328 ms
12,624 KB
testcase_16 AC 328 ms
13,500 KB
testcase_17 AC 334 ms
13,268 KB
testcase_18 AC 330 ms
12,576 KB
testcase_19 AC 331 ms
12,728 KB
testcase_20 AC 327 ms
12,584 KB
testcase_21 AC 328 ms
12,564 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <random>
using namespace std;
//#include <atcoder/all>
//using namespace atcoder;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vvvvll = vector<vvvll>;
using vvvvvll = vector<vvvvll>;
using vvvvvvll = vector<vvvvvll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
using vvvvb = vector<vvvb>;
using vd = vector<double>;
using vvd = vector<vd>;
using vvvd = vector<vvd>;
using vvvvd = vector<vvvd>;
using vvvvvd = vector<vvvvd>;
#define all(A) A.begin(),A.end()
#define ALL(A) A.begin(),A.end()
#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)
using pqr = priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>>;

template<class T>
bool chmax(T& p, T q, bool C = 1) {
    if (C == 0 && p == q) {
        return 1;
    }
    if (p < q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}

template<class T>
bool chmin(T& p, T q, bool C = 1) {
    if (C == 0 && p == q) {
        return 1;
    }
    if (p > q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}

ll gcd(ll(a), ll(b)) {
    if (b == 0)return a;
    ll c = a;
    while (a % b != 0) {
        c = a % b;
        a = b;
        b = c;
    }
    return b;
}


vector<ll> fact, factinv, inv;
ll mod = 1e9 + 7;
void prenCkModp(ll n) {
    fact.resize(n + 5);
    factinv.resize(n + 5);
    inv.resize(n + 5);
    fact.at(0) = fact.at(1) = 1;
    factinv.at(0) = factinv.at(1) = 1;
    inv.at(1) = 1;
    for (ll i = 2; i < n + 5; i++) {
        fact.at(i) = (fact.at(i - 1) * i) % mod;
        inv.at(i) = mod - (inv.at(mod % i) * (mod / i)) % mod;
        factinv.at(i) = (factinv.at(i - 1) * inv.at(i)) % mod;
    }

}
ll nCk(ll n, ll k) {
    if (n < k) return 0;
    return fact.at(n) * (factinv.at(k) * factinv.at(n - k) % mod) % mod;
}


template <class S, S(*op)(S, S), S(*e)()> struct segtree {
public:
    segtree() : segtree(0) {}
    segtree(int n) : segtree(std::vector<S>(n, e())) {}
    segtree(const std::vector<S>& v) : _n(int(v.size())) {
        log = ceil_pow2(_n);
        size = 1 << log;
        d = std::vector<S>(2 * size, e());
        for (int i = 0; i < _n; i++) d[size + i] = v[i];
        for (int i = size - 1; i >= 1; i--) {
            update(i);
        }
    }

    int ceil_pow2(int n) {
        int x = 0;
        while ((1U << x) < (unsigned int)(n)) x++;
        return x;
    }

    void set(int p, S x) {
        p += size;
        d[p] = x;
        for (int i = 1; i <= log; i++) update(p >> i);
    }

    S get(int p) { return d[p + size]; }

    S prod(int l, int r) {
        S sml = e(), smr = e();
        l += size;
        r += size;

        while (l < r) {
            if (l & 1) sml = op(sml, d[l++]);
            if (r & 1) smr = op(d[--r], smr);
            l >>= 1;
            r >>= 1;
        }
        return op(sml, smr);
    }

    S all_prod() { return d[1]; }

    template <bool (*f)(S)> int max_right(int l) {
        return max_right(l, [](S x) { return f(x); });
    }
    template <class F> int max_right(int l, F f) {
        if (l == _n) return _n;
        l += size;
        S sm = e();
        do {
            while (l % 2 == 0) l >>= 1;
            if (!f(op(sm, d[l]))) {
                while (l < size) {
                    l = (2 * l);
                    if (f(op(sm, d[l]))) {
                        sm = op(sm, d[l]);
                        l++;
                    }
                }
                return l - size;
            }
            sm = op(sm, d[l]);
            l++;
        } while ((l & -l) != l);
        return _n;
    }

    template <bool (*f)(S)> int min_left(int r) {
        return min_left(r, [](S x) { return f(x); });
    }
    template <class F> int min_left(int r, F f) {
        if (r == 0) return 0;
        r += size;
        S sm = e();
        do {
            r--;
            while (r > 1 && (r % 2)) r >>= 1;
            if (!f(op(d[r], sm))) {
                while (r < size) {
                    r = (2 * r + 1);
                    if (f(op(d[r], sm))) {
                        sm = op(d[r], sm);
                        r--;
                    }
                }
                return r + 1 - size;
            }
            sm = op(d[r], sm);
        } while ((r & -r) != r);
        return 0;
    }

private:
    int _n, size, log;
    std::vector<S> d;

    void update(int k) { d[k] = op(d[2 * k], d[2 * k + 1]); }
};


ll op(ll a, ll b) { return a + b; }
ll e() { return 0; };


int main() {
    ll N, Q;
    cin >> N >> Q;
    vector<pair<ll, ll>> A(N);
    rep(i, N) {
        cin >> A[i].first;
        A[i].second = i;
    }
    sort(all(A));
    A.push_back({ 1e18,-1 });

    segtree<ll, op, e> seg(N);
    segtree<ll, op, e> segn(N);
    vector<tuple<ll, ll, ll, ll>> D(Q);
    rep(q, Q) {
        ll L, R, X;
        cin >> L >> R >> X;
        D[q] = { X,L,R,q };
    }
    sort(all(D));

    vll AN(Q);
    ll j = 0;
    rep(q, Q) {
        ll X = get<0>(D[q]);
        ll L = get<1>(D[q]);
        ll R = get<2>(D[q]);
        ll i = get<3>(D[q]);
        while (A[j].first < X) {
            seg.set(A[j].second, A[j].first);
            segn.set(A[j].second, 1);
            j++;
        }
        ll d = seg.prod(L - 1, R);
        ll n = segn.prod(L - 1, R);
        AN[i] = d + (R - L + 1 - n) * X;
        cout << "";
    }
    rep(i, Q)cout << AN[i] << endl;

}
0