結果
問題 | No.1099 Range Square Sum |
ユーザー | 👑 emthrm |
提出日時 | 2020-06-26 22:08:19 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,827 bytes |
コンパイル時間 | 2,375 ms |
コンパイル使用メモリ | 214,080 KB |
実行使用メモリ | 6,948 KB |
最終ジャッジ日時 | 2024-07-04 21:21:44 |
合計ジャッジ時間 | 9,516 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | WA | - |
testcase_02 | WA | - |
testcase_03 | AC | 2 ms
6,944 KB |
testcase_04 | AC | 2 ms
6,940 KB |
testcase_05 | WA | - |
testcase_06 | AC | 2 ms
6,944 KB |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | AC | 2 ms
6,940 KB |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | WA | - |
testcase_22 | WA | - |
testcase_23 | WA | - |
testcase_24 | WA | - |
testcase_25 | WA | - |
testcase_26 | WA | - |
testcase_27 | WA | - |
testcase_28 | WA | - |
testcase_29 | WA | - |
testcase_30 | WA | - |
ソースコード
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() using ll = long long; const int INF = 0x3f3f3f3f; const ll LINF = 0x3f3f3f3f3f3f3f3fLL; const double EPS = 1e-8; const int MOD = 1000000007; // const int MOD = 998244353; const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1}; template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; } template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; } struct IOSetup { IOSetup() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(20); } } iosetup; template <typename T> struct SqrtDecomposition { int b, b_n; vector<int> left, right; vector<bool> need_to_be_eval; SqrtDecomposition(int n) : b(sqrt(n)) { b_n = (n + b - 1) / b; left.resize(b_n); right.resize(b_n); need_to_be_eval.assign(b_n, false); REP(i, b_n) { left[i] = b * i; right[i] = (i + 1 == b_n ? n : b * (i + 1)); } } void partial_update(int idx, T val); void total_update(int idx, T val); void update(int l, int r, T val) { if (r <= l) return; int l_b = l / b, r_b = (r - 1) / b; if (l_b == r_b) { FOR(i, l, r) partial_update(i, val); } else { FOR(i, l, right[l_b]) partial_update(i, val); FOR(i, l_b + 1, r_b) total_update(i, val); FOR(i, left[r_b], r) partial_update(i, val); } } void partial_query(int idx, T &val); void total_query(int idx, T &val); T query(int l, int r, T UNITY) { int l_b = l / b, r_b = (r - 1) / b; T res = UNITY; if (l_b == r_b) { FOR(i, l, r) partial_query(i, res); } else if (l < r) { FOR(i, l, right[l_b]) partial_query(i, res); FOR(i, l_b + 1, r_b) total_query(i, res); FOR(i, left[r_b], r) partial_query(i, res); } return res; } }; vector<ll> a, squ, sum, add; template <typename T> void SqrtDecomposition<T>::partial_update(int idx, T val) { int block = idx / b; if (need_to_be_eval[block]) { FOR(i, left[block], right[block]) { squ[block] -= a[i] * a[i]; a[idx] += add[block]; sum[block] += add[block]; squ[block] += a[idx] * a[idx]; } add[block] = 0; need_to_be_eval[block] = false; } squ[block] -= a[idx] * a[idx]; a[idx] += val; sum[block] += val; squ[block] += a[idx] * a[idx]; } template <typename T> void SqrtDecomposition<T>::total_update(int idx, T val) { add[idx / b] += val; need_to_be_eval[idx / b] = true; } template <typename T> void SqrtDecomposition<T>::partial_query(int idx, T &val) { int block = idx / b; if (need_to_be_eval[block]) { FOR(i, left[block], right[block]) { squ[block] -= a[i] * a[i]; a[idx] += add[block]; sum[block] += add[block]; squ[block] += a[idx] * a[idx]; } add[block] = 0; need_to_be_eval[block] = false; } val += a[idx] * a[idx]; } template <typename T> void SqrtDecomposition<T>::total_query(int idx, T &val) { val += squ[idx] + sum[idx] * add[idx] * 2 + add[idx] * add[idx] * (right[idx] - left[idx]); } int main() { int n; cin >> n; SqrtDecomposition<ll> sd(n); a.resize(n); squ.assign(sd.b_n, 0); sum.assign(sd.b_n, 0); REP(i, n) { cin >> a[i]; squ[i / sd.b] += a[i] * a[i]; sum[i / sd.b] += a[i]; } add.assign(sd.b_n, 0); int q; cin >> q; while (q--) { int query, l, r; cin >> query >> l >> r; --l; --r; if (query == 1) { int x; cin >> x; sd.update(l, r + 1, x); } else if (query == 2) { cout << sd.query(l, r + 1, 0) << '\n'; } } return 0; }