結果
| 問題 |
No.1099 Range Square Sum
|
| コンテスト | |
| ユーザー |
risujiroh
|
| 提出日時 | 2020-06-26 21:33:16 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 184 ms / 2,000 ms |
| コード長 | 2,166 bytes |
| コンパイル時間 | 2,835 ms |
| コンパイル使用メモリ | 198,124 KB |
| 最終ジャッジ日時 | 2025-01-11 10:55:42 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 30 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define DEBUG(...)
#endif
template <class T, class F> struct lazy_segtree {
const int n;
vector<T> t;
vector<F> d;
lazy_segtree(int _n = 0) : n(_n), t(2 * n), d(n) {}
T& operator[](int i) { return t[n + i]; }
void build() { for (int i = n; i--; ) t[i] = t[2 * i] * t[2 * i + 1]; }
void apply(int i, F f) {
t[i] = f(t[i]);
if (i < n) d[i] = d[i] * f;
}
void push(int i) {
apply(2 * i, d[i]), apply(2 * i + 1, d[i]);
d[i] = F();
}
void push_down(int i) {
for (int h = __lg(i), tz = __builtin_ctz(i); h > tz; --h) push(i >> h);
}
void pull_up(int i) {
while (i >>= 1) t[i] = t[2 * i] * t[2 * i + 1];
}
T fold(int l, int r) {
push_down(l += n), push_down(r += n);
T a, b;
for (; l < r; l >>= 1, r >>= 1) {
if (l & 1) a = a * t[l++];
if (r & 1) b = t[--r] * b;
}
return a * b;
}
T get(int i) { return fold(i, i + 1); }
void act(int l, int r, F f) {
push_down(l += n), push_down(r += n);
for (int i = l, j = r; i < j; i >>= 1, j >>= 1) {
if (i & 1) apply(i++, f);
if (j & 1) apply(--j, f);
}
pull_up(l >> __builtin_ctz(l)), pull_up(r >> __builtin_ctz(r));
}
void set(int i, T a) {
push_down(i += n), push_down(i + 1);
t[i] = a;
pull_up(i);
}
};
using ll = long long;
struct node {
ll s0 = 0, s1 = 0, s2 = 0;
node operator*(node b) const {
return {s0 + b.s0, s1 + b.s1, s2 + b.s2};
}
};
struct add {
ll v = 0;
add operator*(add g) const {
return {v + g.v};
}
node operator()(node x) const {
x.s2 += 2 * v * x.s1 + x.s0 * v * v;
x.s1 += x.s0 * v;
return x;
}
};
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
int n;
cin >> n;
lazy_segtree<node, add> seg(n);
for (int i = 0; i < n; ++i) {
ll a;
cin >> a;
seg[i] = {1, a, a * a};
}
seg.build();
int q;
cin >> q;
while (q--) {
int tp, l, r;
cin >> tp >> l >> r;
--l;
if (tp == 1) {
ll x;
cin >> x;
seg.act(l, r, {x});
} else {
cout << seg.fold(l, r).s2 << '\n';
}
}
}
risujiroh