結果
問題 | No.1000 Point Add and Array Add |
ユーザー |
![]() |
提出日時 | 2025-02-12 00:57:26 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
RE
|
実行時間 | - |
コード長 | 6,125 bytes |
コンパイル時間 | 3,797 ms |
コンパイル使用メモリ | 281,352 KB |
実行使用メモリ | 17,928 KB |
最終ジャッジ日時 | 2025-02-12 00:57:35 |
合計ジャッジ時間 | 7,907 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 20 RE * 2 |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; #ifdef LOCAL #include <debug.hpp> #else #define debug(...) #endif template <class T, class E, class F, class G, class H> struct LazySegmentTree { int n, len, height; vector<T> data; vector<E> lazy; const F op; const G mapp; const H comp; const T e; const E id; LazySegmentTree() = default; LazySegmentTree(int size, const F& operation, const G& mapping, const H& composition, const T& identity, const E& lazy_identity) : n(size), op(operation), mapp(mapping), comp(composition), e(identity), id(lazy_identity) { init(); } LazySegmentTree(const vector<T>& v, const F& operation, const G& mapping, const H& composition, const T& identity, const E& lazy_identity) : n(int(v.size())), op(operation), mapp(mapping), comp(composition), e(identity), id(lazy_identity) { init(), build(v); } void init() { len = 1, height = 0; while (len < n) len <<= 1, height++; data.assign(len << 1, e); lazy.assign(len << 1, id); } void build(const vector<T>& v) { for (int i = 0; i < n; i++) data[i + len] = v[i]; for (int i = len - 1; i > 0; i--) data[i] = op(data[i << 1 | 0], data[i << 1 | 1]); } T reflect(int k) { return lazy[k] == id ? data[k] : mapp(data[k], lazy[k]); } void eval(int k) { if (lazy[k] == id) return; // 子の要素があれば, 子に遅延配列の伝搬を行う lazy[k << 1 | 0] = comp(lazy[k << 1 | 0], lazy[k]); lazy[k << 1 | 1] = comp(lazy[k << 1 | 1], lazy[k]); // 自身を更新 data[k] = reflect(k); lazy[k] = id; } void recalc(int k) { while (k >>= 1) data[k] = op(reflect(k << 1 | 0), reflect(k << 1 | 1)); } void thrust(int k) { for (int i = height; i > 0; i--) eval(k >> i); } // [l, r) に val を作用させて更新 void update(int l, int r, E val) { if (l >= r) return; // 上側のノードをまず伝搬 l += len, r += len; thrust(l), thrust(r); int l0 = l, r0 = r; while (l < r) { if (l & 1) lazy[l] = comp(lazy[l], val), l++; if (r & 1) --r, lazy[r] = comp(lazy[r], val); l >>= 1, r >>= 1; } l = l0, r = r0; // 上側のノードを更新 recalc(l), recalc(r - 1); } // i 番目の値を val に更新 void set(int i, T val) { i += len; thrust(i); data[i] = val; lazy[i] = id; recalc(i); }; // [l, r) の区間取得 T prod(int l, int r) { assert(0 <= l && r <= n); if (l >= r) return e; l += len, r += len; // 上側のノードをまず伝搬 thrust(l), thrust(r - 1); T resl = e, resr = e; while (l < r) { if (l & 1) resl = op(resl, reflect(l++)); if (r & 1) resr = op(reflect(--r), resr); l >>= 1, r >>= 1; } return op(resl, resr); } // i の値を取得 T get(int i) { i += len; for (int j = height; j > 0; j--) eval(i >> j); return reflect(i); } T operator[](int i) { return get(i); } // check(A[l] * ... * A[r - 1]) = true となる最大の r template <class C> int max_right(int l, const C& check) { assert(0 <= l && l <= n && check(e)); if (l == n) return n; l += len; thrust(l); T sum = e; do { while (l % 2 == 0) l >>= 1; if (!check(op(sum, data[l]))) { while (l < len) { eval(l); l = l << 1 | 0; if (check(op(sum, data[l]))) { sum = op(sum, data[l++]); } } return l - len; } sum = op(sum, data[l++]); } while ((l & -l) != l); return n; } // check(A[l] * ... * A[r - 1]) = true となる最小の l template <class C> int min_left(int r, const C& check) { assert(0 <= r && r <= n && check(e)); if (r == 0) return 0; r += len; thrust(r - 1); T sum = e; do { r--; while (r > 1 && (r % 2)) r >>= 1; if (!check(op(data[r], sum))) { while (r < len) { eval(r); r = r << 1 | 1; if (check(op(data[r], sum))) { sum = op(data[r--], sum); } } return r + 1 - len; } sum = op(data[r], sum); } while ((r & -r) != r); return 0; } }; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); int N, Q; cin >> N >> Q; vector<ll> A(N); for (int i = 0; i < N; i++) cin >> A[i]; vector<char> C(Q); vector<ll> X(Q), Y(Q); for (int i = 0; i < Q; i++) cin >> C[i] >> X[i] >> Y[i]; // 区間加算, 区間 max 取得の遅延セグメント木 using S = ll; S e = 0; auto op = [&](S a, S b) -> S { return max(a, b); }; using F = ll; F id = 0; auto mapping = [&](S x, F f) -> S { return x + f; }; auto composition = [&](F f, F g) -> F { return f + g; }; LazySegmentTree seg(N, op, mapping, composition, e, id); vector<ll> ans(N); for (int i = Q - 1; i >= 0; i--) { if (C[i] == 'A') { X[i]--; // Y[i] の寄与は seg[X[i]] 回する ans[X[i]] += seg.get(X[i]) * Y[i]; } if (C[i] == 'B') { X[i]--, Y[i]--; // [X[i], Y[i] + 1) の寄与の回数を +1 にする seg.update(X[i], Y[i] + 1, 1); } } for (int i = 0; i < N; i++) { // A[i] の寄与は seg[i] 回する ans[i] += seg.get(i) * A[i]; } for (int i = 0; i < N; i++) { cout << ans[i] << " \n"[i == N - 1]; } }