結果
問題 | No.2065 Sum of Min |
ユーザー |
|
提出日時 | 2022-09-02 21:35:06 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 378 ms / 2,000 ms |
コード長 | 5,467 bytes |
コンパイル時間 | 2,761 ms |
コンパイル使用メモリ | 216,020 KB |
最終ジャッジ日時 | 2025-02-07 00:50:56 |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 20 |
ソースコード
#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;}