結果
問題 | No.1099 Range Square Sum |
ユーザー | SSRS |
提出日時 | 2020-05-19 00:38:09 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 459 ms / 2,000 ms |
コード長 | 4,798 bytes |
コンパイル時間 | 1,896 ms |
コンパイル使用メモリ | 178,040 KB |
実行使用メモリ | 27,520 KB |
最終ジャッジ日時 | 2024-11-22 16:38:50 |
合計ジャッジ時間 | 7,832 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 1 ms
5,248 KB |
testcase_04 | AC | 2 ms
5,248 KB |
testcase_05 | AC | 2 ms
5,248 KB |
testcase_06 | AC | 2 ms
5,248 KB |
testcase_07 | AC | 2 ms
5,248 KB |
testcase_08 | AC | 1 ms
5,248 KB |
testcase_09 | AC | 2 ms
5,248 KB |
testcase_10 | AC | 1 ms
5,248 KB |
testcase_11 | AC | 4 ms
5,248 KB |
testcase_12 | AC | 4 ms
5,248 KB |
testcase_13 | AC | 5 ms
5,248 KB |
testcase_14 | AC | 4 ms
5,248 KB |
testcase_15 | AC | 4 ms
5,248 KB |
testcase_16 | AC | 5 ms
5,248 KB |
testcase_17 | AC | 4 ms
5,248 KB |
testcase_18 | AC | 4 ms
5,248 KB |
testcase_19 | AC | 4 ms
5,248 KB |
testcase_20 | AC | 5 ms
5,248 KB |
testcase_21 | AC | 459 ms
27,392 KB |
testcase_22 | AC | 445 ms
27,392 KB |
testcase_23 | AC | 439 ms
27,520 KB |
testcase_24 | AC | 457 ms
27,392 KB |
testcase_25 | AC | 446 ms
27,392 KB |
testcase_26 | AC | 353 ms
27,392 KB |
testcase_27 | AC | 357 ms
27,392 KB |
testcase_28 | AC | 360 ms
27,392 KB |
testcase_29 | AC | 362 ms
27,392 KB |
testcase_30 | AC | 366 ms
27,392 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; int INF = 1000000000; int MAX_N = 200000; int MAX_A = 1000000; int MIN_A = -1000000; int MAX_Q = 100000; int MAX_X = 1000000; int MIN_X = -1000000; struct lazy_segment_tree_minmax{ int N; vector<int> ST_min; vector<int> ST_max; vector<int> lazy; lazy_segment_tree_minmax(vector<long long> &A){ int n = A.size(); N = 1; while (N < n){ N *= 2; } ST_min = vector<int>(N * 2 - 1, INF); ST_max = vector<int>(N * 2 - 1, -INF); lazy = vector<int>(N * 2 - 1, 0); for (int i = 0; i < n; i++){ ST_min[N - 1 + i] = A[i]; ST_max[N - 1 + i] = A[i]; } for (int i = N - 2; i >= 0; i--){ ST_min[i] = min(ST_min[i * 2 + 1], ST_min[i * 2 + 2]); ST_max[i] = max(ST_max[i * 2 + 1], ST_max[i * 2 + 2]); } } void eval(int i){ if (i < N - 1){ lazy[i * 2 + 1] += lazy[i]; lazy[i * 2 + 2] += lazy[i]; } ST_min[i] += lazy[i]; ST_max[i] += lazy[i]; lazy[i] = 0; } void range_add(int L, int R, int x, int i, int l, int r){ eval(i); if (R <= l || r <= L){ return; } else if (L <= l && r <= R){ lazy[i] += x; eval(i); } else { int m = (l + r) / 2; range_add(L, R, x, i * 2 + 1, l, m); range_add(L, R, x, i * 2 + 2, m, r); ST_min[i] = min(ST_min[i * 2 + 1], ST_min[i * 2 + 2]); ST_max[i] = max(ST_max[i * 2 + 1], ST_max[i * 2 + 2]); } } void range_add(int L, int R, int x){ range_add(L, R, x, 0, 0, N); } int range_min(int L, int R, int i, int l, int r){ eval(i); if (R <= l || r <= L){ return INF; } else if (L <= l && r <= R){ return ST_min[i]; } else { int m = (l + r) / 2; return min(range_min(L, R, i * 2 + 1, l, m), range_min(L, R, i * 2 + 2, m, r)); } } int range_min(int L, int R){ return range_min(L, R, 0, 0, N); } int range_max(int L, int R, int i, int l, int r){ eval(i); if (R <= l || r <= L){ return -INF; } else if (L <= l && r <= R){ return ST_max[i]; } else { int m = (l + r) / 2; return max(range_max(L, R, i * 2 + 1, l, m), range_max(L, R, i * 2 + 2, m, r)); } } int range_max(int L, int R){ return range_max(L, R, 0, 0, N); } }; struct lazy_segment_tree{ int N; vector<long long> count; vector<long long> sum; vector<long long> sqsum; vector<long long> lazy; lazy_segment_tree(vector<long long> &A){ int n = A.size(); N = 1; while (N < n){ N *= 2; } count = vector<long long>(N * 2 - 1, 1); sum = vector<long long>(N * 2 - 1, 0); sqsum = vector<long long>(N * 2 - 1, 0); lazy = vector<long long>(N * 2 - 1, 0); for (int i = 0; i < n; i++){ sum[N - 1 + i] = A[i]; sqsum[N - 1 + i] = A[i] * A[i]; } for (int i = N - 2; i >= 0; i--){ count[i] = count[i * 2 + 1] + count[i * 2 + 2]; sum[i] = sum[i * 2 + 1] + sum[i * 2 + 2]; sqsum[i] = sqsum[i * 2 + 1] + sqsum[i * 2 + 2]; } } void eval(int i){ if (i < N - 1){ lazy[i * 2 + 1] += lazy[i]; lazy[i * 2 + 2] += lazy[i]; } sqsum[i] = sqsum[i] + 2 * sum[i] * lazy[i] + count[i] * lazy[i] * lazy[i]; sum[i] = sum[i] + count[i] * lazy[i]; lazy[i] = 0; } void range_add(int L, int R, int x, int i, int l, int r){ eval(i); if (R <= l || r <= L){ return; } else if (L <= l && r <= R){ lazy[i] += x; eval(i); } else { int m = (l + r) / 2; range_add(L, R, x, i * 2 + 1, l, m); range_add(L, R, x, i * 2 + 2, m, r); sum[i] = sum[i * 2 + 1] + sum[i * 2 + 2]; sqsum[i] = sqsum[i * 2 + 1] + sqsum[i * 2 + 2]; } } void range_add(int L, int R, int x){ range_add(L, R, x, 0, 0, N); } long long range_sqsum(int L, int R, int i, int l, int r){ eval(i); if (R <= l || r <= L){ return 0; } else if (L <= l && r <= R){ return sqsum[i]; } else { int m = (l + r) / 2; return range_sqsum(L, R, i * 2 + 1, l, m) + range_sqsum(L, R, i * 2 + 2, m, r); } } long long range_sqsum(int L, int R){ return range_sqsum(L, R, 0, 0, N); } }; int main(){ int N; cin >> N; assert(1 <= N); assert(N <= MAX_N); vector<long long> A(N); for (int i = 0; i < N; i++){ cin >> A[i]; assert(MIN_A <= A[i]); assert(A[i] <= MAX_A); } lazy_segment_tree_minmax a(A); lazy_segment_tree b(A); int Q; cin >> Q; assert(1 <= Q); assert(Q <= MAX_Q); int cnt = 0; for (int i = 0; i < Q; i++){ int t; cin >> t; assert(t == 1 || t == 2); if (t == 1){ int l, r, x; cin >> l >> r >> x; assert(1 <= l && l <= r && r <= N); assert(MIN_X <= x); assert(x <= MAX_X); a.range_add(l - 1, r, x); b.range_add(l - 1, r, x); assert(a.range_min(0, N) >= MIN_A); assert(a.range_max(0, N) <= MAX_A); } if (t == 2){ cnt++; int l, r; cin >> l >> r; assert(1 <= l && l <= r && r <= N); cout << b.range_sqsum(l - 1, r) << endl; } } assert(cnt >= 1); }