結果
問題 | No.1099 Range Square Sum |
ユーザー | 👑 emthrm |
提出日時 | 2020-06-26 22:22:37 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 4,039 bytes |
コンパイル時間 | 2,299 ms |
コンパイル使用メモリ | 215,136 KB |
実行使用メモリ | 13,756 KB |
最終ジャッジ日時 | 2024-07-04 22:05:38 |
合計ジャッジ時間 | 7,822 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | WA | - |
testcase_01 | WA | - |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
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 | OLE | - |
testcase_22 | -- | - |
testcase_23 | -- | - |
testcase_24 | -- | - |
testcase_25 | -- | - |
testcase_26 | -- | - |
testcase_27 | -- | - |
testcase_28 | -- | - |
testcase_29 | -- | - |
testcase_30 | -- | - |
ソースコード
#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[i] += add[block]; sum[block] += add[block]; squ[block] += a[i] * a[i]; } 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] += val; need_to_be_eval[idx] = 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[i] += add[block]; sum[block] += add[block]; squ[block] += a[i] * a[i]; } 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'; } REP(i, n) cout << a[i] << " \n"[i + 1 == n]; REP(i, sd.b_n) cout << squ[i] << " \n"[i + 1 == sd.b_n]; REP(i, sd.b_n) cout << sum[i] << " \n"[i + 1 == sd.b_n]; REP(i, sd.b_n) cout << add[i] << " \n"[i + 1 == sd.b_n]; } return 0; }