結果
| 問題 |
No.1000 Point Add and Array Add
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-02-28 21:43:10 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 233 ms / 2,000 ms |
| コード長 | 3,752 bytes |
| コンパイル時間 | 2,457 ms |
| コンパイル使用メモリ | 209,596 KB |
| 最終ジャッジ日時 | 2025-01-09 02:40:28 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 22 |
ソースコード
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
template <typename Monoid, typename LazyMonoid, typename FunctionMonoid, typename FunctionAction, typename FunctionLazy> class LazySegmentTree {
private:
//using FunctionMonoid = function<Monoid(Monoid, Monoid)>;
//using FunctionAction = function<Monoid(Monoid, LazyMonoid)>;
//using FunctionLazy = function<LazyMonoid(LazyMonoid, LazyMonoid)>;
FunctionMonoid fm;
FunctionAction fa;
FunctionLazy fl;
int n, height;
vector<Monoid> data;
vector<LazyMonoid> lazy;
Monoid M_UNITY;
LazyMonoid L_UNITY;
void build (const int siz) {
n = 1;
height = 0;
while (n < siz) n <<= 1, ++height;
data.assign(2 * n, M_UNITY);
lazy.assign(2 * n, L_UNITY);
}
public:
LazySegmentTree (const FunctionMonoid& f, const FunctionAction& g, const FunctionLazy& h, const Monoid& m_unity, const LazyMonoid& l_unity, int siz = 1)
: fm(f), fa(g), fl(h), M_UNITY(m_unity), L_UNITY(l_unity) {
build(siz);
}
inline Monoid reflect (int k) {
return lazy[k] == L_UNITY ? data[k] : fa(data[k], lazy[k]);
}
inline void propagate (int k) {
if (lazy[k] == L_UNITY) return;
lazy[(k << 1) | 0] = fl(lazy[(k << 1) | 0], lazy[k]);
lazy[(k << 1) | 1] = fl(lazy[(k << 1) | 1], lazy[k]);
data[k] = reflect(k);
lazy[k] = L_UNITY;
}
inline void thrust (int k) {
for (int i = height; i > 0; --i) propagate(k >> i);
}
inline void recalc (int k) {
while (k >>= 1) data[k] = fm(reflect((k << 1) | 0), reflect((k << 1) | 1));
}
void modify (int left, int right, LazyMonoid val) {
thrust(left += n);
thrust(right += n - 1);
for (int l = left, r = right + 1; l < r; l >>= 1, r >>= 1) {
if (l & 1) lazy[l] = fl(lazy[l], val), l++;
if (r & 1) --r, lazy[r] = fl(lazy[r], val);
}
recalc(left);
recalc(right);
}
void modify (int idx, LazyMonoid val) {
thrust(idx += n);
data[idx] = val;
lazy[idx] = L_UNITY;
recalc(idx);
}
Monoid get (int left, int right) {
thrust(left += n);
thrust(right += n - 1);
Monoid val_l = M_UNITY, val_r = M_UNITY;
for (int l = left, r = right + 1; l < r; l >>= 1, r >>= 1) {
if (l & 1) val_l = fm(val_l, reflect(l++));
if (r & 1) val_r = fm(reflect(--r), val_r);
}
return fm(val_l, val_r);
}
};
signed main() {
ios::sync_with_stdio(false); cin.tie(0);
//c
int n, q;
cin >> n >> q;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
vector<vector<int>> query;
for (int i = 0; i < q; i++) {
char ch;
cin >> ch;
int c = (ch == 'A');
int x, y;
cin >> x >> y;
if (c == 1) x--;
else x--, y--;
query.push_back({c, x, y});
}
reverse(query.begin(), query.end());
auto f = [] (int x, int y) { return x + y; };
LazySegmentTree<int, int, decltype(f), decltype(f), decltype(f)> sgt (f, f, f, 0, 0, n + 10);
for (int i = 0; i < n; i++) sgt.modify(i, 0);
vector<vector<pair<int, int>>> add(n);
for (int i = 0; i < q; i++) {
if (query[i][0] == 0) {
int l = query[i][1];
int r = query[i][2];
sgt.modify(l, r + 1, 1);
//cerr << l << " " << r << endl;
} else {
int idx = query[i][1];
int val = query[i][2];
add[idx].emplace_back(val, sgt.get(idx, idx + 1));
}
}
// for (int i = 0; i < n; i++) {
// cerr << sgt.get(i, i + 1) << " ";
// }
// cerr << endl;
vector<long long> ans(n);
for (int i = 0; i < n; i++) {
for (auto& p : add[i]) {
ans[i] += 1LL * p.first * p.second;
}
}
for (int i = 0; i < n; i++) {
ans[i] += 1LL * a[i] * sgt.get(i, i + 1);
}
for (int i = 0; i < n; i++) {
if (i > 0) cout << " ";
cout << ans[i];
}
cout << endl;
return 0;
}