結果
問題 | No.2065 Sum of Min |
ユーザー | k1suxu |
提出日時 | 2022-09-02 23:03:00 |
言語 | C++23 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 652 ms / 2,000 ms |
コード長 | 10,124 bytes |
コンパイル時間 | 3,415 ms |
コンパイル使用メモリ | 263,660 KB |
実行使用メモリ | 49,788 KB |
最終ジャッジ日時 | 2024-11-16 05:41:28 |
合計ジャッジ時間 | 14,118 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 2 ms
5,248 KB |
testcase_04 | AC | 298 ms
49,660 KB |
testcase_05 | AC | 330 ms
49,656 KB |
testcase_06 | AC | 251 ms
49,528 KB |
testcase_07 | AC | 202 ms
49,656 KB |
testcase_08 | AC | 264 ms
49,656 KB |
testcase_09 | AC | 310 ms
49,524 KB |
testcase_10 | AC | 355 ms
49,784 KB |
testcase_11 | AC | 344 ms
49,708 KB |
testcase_12 | AC | 624 ms
49,656 KB |
testcase_13 | AC | 652 ms
49,656 KB |
testcase_14 | AC | 647 ms
49,652 KB |
testcase_15 | AC | 612 ms
49,648 KB |
testcase_16 | AC | 617 ms
49,720 KB |
testcase_17 | AC | 629 ms
49,788 KB |
testcase_18 | AC | 622 ms
49,660 KB |
testcase_19 | AC | 641 ms
49,528 KB |
testcase_20 | AC | 639 ms
49,656 KB |
testcase_21 | AC | 614 ms
49,652 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for(int i = 0; i < (int)n; i++) #define FOR(n) for(int i = 0; i < (int)n; i++) #define repi(i,a,b) for(int i = (int)a; i < (int)b; i++) #define pb push_back #define m0(x) memset(x,0,sizeof(x)) #define fill(x,y) memset(x,y,sizeof(x)) #define bg begin() #define ed end() #define all(x) x.bg,x.ed //#define mp make_pair #define vi vector<int> #define vvi vector<vi> #define vll vector<ll> #define vvll vector<vll> #define vs vector<string> #define vvs vector<vs> #define vc vector<char> #define vvc vector<vc> #define pii pair<int,int> #define pllll pair<ll,ll> #define vpii vector<pair<int,int>> #define vpllll vector<pair<ll,ll>> #define vpis vector<pair<int,string>> #define vplls vector<pair<ll, string>> #define vpsi vector<pair<string, int>> #define vpsll vector<pair<string, ll>> template<typename T> void chmax(T &a, const T &b) {a = (a > b? a : b);} template<typename T> void chmin(T &a, const T &b) {a = (a < b? a : b);} using ll = long long; using ld = long double; using ull = unsigned long long; const ll INF = numeric_limits<long long>::max() / 2; const ld pi = 3.1415926535897932384626433832795028; const ll mod = 1e9 + 7; int dx[] = {-1, 0, 1, 0, -1, -1, 1, 1}; int dy[] = {0, -1, 0, 1, -1, 1, -1, 1}; #define int long long struct SegmentTree { //functions and values int n, n0; vector<int> max_v, smax_v, max_c; vector<int> min_v, smin_v, min_c; vector<int> sum; vector<int> len, ladd, lval; SegmentTree(int n, bool init = false, vector<int> a = {}) : n(n) { n0 = 1; while(n0 < n) n0 <<= 1; max_v.resize(4 * n0); smax_v.resize(4 * n0); max_c.resize(4 * n0); min_v.resize(4 * n0); smin_v.resize(4 * n0); min_c.resize(4 * n0); sum.resize(4 * n0); len.resize(4 * n0); ladd.resize(4 * n0); lval.resize(4 * n0); for(int i=0; i<2*n0; ++i) ladd[i] = 0, lval[i] = INF; len[0] = n0; for(int i=0; i<n0-1; ++i) len[2*i+1] = len[2*i+2] = (len[i] >> 1); for(int i=0; i<n; ++i) { max_v[n0-1+i] = min_v[n0-1+i] = sum[n0-1+i] = (init ? a[i] : 0); smax_v[n0-1+i] = -INF; smin_v[n0-1+i] = INF; max_c[n0-1+i] = min_c[n0-1+i] = 1; } for(int i=n; i<n0; ++i) { max_v[n0-1+i] = smax_v[n0-1+i] = -INF; min_v[n0-1+i] = smin_v[n0-1+i] = INF; max_c[n0-1+i] = min_c[n0-1+i] = 0; } for(int i=n0-2; i>=0; i--) { update(i); } } void update_node_max(int k, ll x) { sum[k] += (x - max_v[k]) * max_c[k]; if(max_v[k] == min_v[k]) { max_v[k] = min_v[k] = x; } else if(max_v[k] == smin_v[k]) { max_v[k] = smin_v[k] = x; } else { max_v[k] = x; } if(lval[k] != INF && x < lval[k]) { lval[k] = x; } } void update_node_min(int k, ll x) { sum[k] += (x - min_v[k]) * min_c[k]; if(max_v[k] == min_v[k]) { max_v[k] = min_v[k] = x; } else if(smax_v[k] == min_v[k]) { min_v[k] = smax_v[k] = x; } else { min_v[k] = x; } if(lval[k] != INF && lval[k] < x) { lval[k] = x; } } void push(int k) { if(n0-1 <= k) return; if(lval[k] != INF) { updateall(2*k+1, lval[k]); updateall(2*k+2, lval[k]); lval[k] = INF; return; } if(ladd[k] != 0) { addall(2*k+1, ladd[k]); addall(2*k+2, ladd[k]); ladd[k] = 0; } if(max_v[k] < max_v[2*k+1]) { update_node_max(2*k+1, max_v[k]); } if(min_v[2*k+1] < min_v[k]) { update_node_min(2*k+1, min_v[k]); } if(max_v[k] < max_v[2*k+2]) { update_node_max(2*k+2, max_v[k]); } if(min_v[2*k+2] < min_v[k]) { update_node_min(2*k+2, min_v[k]); } } void update(int k) { sum[k] = sum[2*k+1] + sum[2*k+2]; if(max_v[2*k+1] < max_v[2*k+2]) { max_v[k] = max_v[2*k+2]; max_c[k] = max_c[2*k+2]; smax_v[k] = max(max_v[2*k+1], smax_v[2*k+2]); } else if(max_v[2*k+1] > max_v[2*k+2]) { max_v[k] = max_v[2*k+1]; max_c[k] = max_c[2*k+1]; smax_v[k] = max(smax_v[2*k+1], max_v[2*k+2]); } else { max_v[k] = max_v[2*k+1]; max_c[k] = max_c[2*k+1] + max_c[2*k+2]; smax_v[k] = max(smax_v[2*k+1], smax_v[2*k+2]); } if(min_v[2*k+1] < min_v[2*k+2]) { min_v[k] = min_v[2*k+1]; min_c[k] = min_c[2*k+1]; smin_v[k] = min(smin_v[2*k+1], min_v[2*k+2]); } else if(min_v[2*k+1] > min_v[2*k+2]) { min_v[k] = min_v[2*k+2]; min_c[k] = min_c[2*k+2]; smin_v[k] = min(min_v[2*k+1], smin_v[2*k+2]); } else { min_v[k] = min_v[2*k+1]; min_c[k] = min_c[2*k+1] + min_c[2*k+2]; smin_v[k] = min(smin_v[2*k+1], smin_v[2*k+2]); } } void _update_min(ll x, int a, int b, int k, int l, int r) { if(b <= l || r <= a || max_v[k] <= x) { return; } if(a <= l && r <= b && smax_v[k] < x) { update_node_max(k, x); return; } push(k); _update_min(x, a, b, 2*k+1, l, (l+r)/2); _update_min(x, a, b, 2*k+2, (l+r)/2, r); update(k); } void _update_max(ll x, int a, int b, int k, int l, int r) { if(b <= l || r <= a || x <= min_v[k]) { return; } if(a <= l && r <= b && x < smin_v[k]) { update_node_min(k, x); return; } push(k); _update_max(x, a, b, 2*k+1, l, (l+r)/2); _update_max(x, a, b, 2*k+2, (l+r)/2, r); update(k); } void addall(int k, ll x) { max_v[k] += x; if(smax_v[k] != -INF) smax_v[k] += x; min_v[k] += x; if(smin_v[k] != INF) smin_v[k] += x; sum[k] += len[k] * x; if(lval[k] != INF) { lval[k] += x; } else { ladd[k] += x; } } void updateall(int k, ll x) { max_v[k] = x; smax_v[k] = -INF; min_v[k] = x; smin_v[k] = INF; max_c[k] = min_c[k] = len[k]; sum[k] = x * len[k]; lval[k] = x; ladd[k] = 0; } void _add_val(ll x, int a, int b, int k, int l, int r) { if(b <= l || r <= a) { return; } if(a <= l && r <= b) { addall(k, x); return; } push(k); _add_val(x, a, b, 2*k+1, l, (l+r)/2); _add_val(x, a, b, 2*k+2, (l+r)/2, r); update(k); } void _update_val(ll x, int a, int b, int k, int l, int r) { if(b <= l || r <= a) { return; } if(a <= l && r <= b) { updateall(k, x); return; } push(k); _update_val(x, a, b, 2*k+1, l, (l+r)/2); _update_val(x, a, b, 2*k+2, (l+r)/2, r); update(k); } ll _query_max(int a, int b, int k, int l, int r) { if(b <= l || r <= a) { return -INF; } if(a <= l && r <= b) { return max_v[k]; } push(k); ll lv = _query_max(a, b, 2*k+1, l, (l+r)/2); ll rv = _query_max(a, b, 2*k+2, (l+r)/2, r); return max(lv, rv); } ll _query_min(int a, int b, int k, int l, int r) { if(b <= l || r <= a) { return INF; } if(a <= l && r <= b) { return min_v[k]; } push(k); ll lv = _query_min(a, b, 2*k+1, l, (l+r)/2); ll rv = _query_min(a, b, 2*k+2, (l+r)/2, r); return min(lv, rv); } ll _query_sum(int a, int b, int k, int l, int r) { if(b <= l || r <= a) { return 0; } if(a <= l && r <= b) { return sum[k]; } push(k); ll lv = _query_sum(a, b, 2*k+1, l, (l+r)/2); ll rv = _query_sum(a, b, 2*k+2, (l+r)/2, r); return lv + rv; } // range minimize query void update_min(int a, int b, ll x) { _update_min(x, a, b, 0, 0, n0); } // range maximize query void update_max(int a, int b, ll x) { _update_max(x, a, b, 0, 0, n0); } // range add query void add_val(int a, int b, ll x) { _add_val(x, a, b, 0, 0, n0); } // range update query void update_val(int a, int b, ll x) { _update_val(x, a, b, 0, 0, n0); } // range minimum query ll query_max(int a, int b) { return _query_max(a, b, 0, 0, n0); } // range maximum query ll query_min(int a, int b) { return _query_min(a, b, 0, 0, n0); } // range sum query ll query_sum(int a, int b) { return _query_sum(a, b, 0, 0, n0); } vector<ll> get_all() { vector<ll> ret(n); for(int i = 0; i < n; i++) { ret[i] = query_max(i, i + 1); } return ret; } inline int operator[](int i) { return query_max(i, i + 1); } void print() { for(int i = 0; i < n; i++) { cout << (*this)[i]; if(i != n - 1) cout << ","; } cout << endl; } }; void solve() { int n, q; cin >> n >> q; vi a(n); FOR(n) cin >> a[i]; vector<array<int, 4>> query; FOR(q) { int l, r, x; cin >> l >> r >> x; --l; query.push_back({l, r, x, i}); } sort(all(query), [&](array<int, 4> a, array<int, 4> b) { return a[2] > b[2]; }); vi ans(q); SegmentTree seg(n, true, a); for(auto e : query) { seg.update_min(e[0], e[1], e[2]); ans[e[3]] = seg.query_sum(e[0], e[1]); } for(auto e : ans) cout << e << endl; } signed main() { cin.tie(nullptr); ios::sync_with_stdio(false); solve(); return 0; }