結果
問題 | No.1099 Range Square Sum |
ユーザー | SSRS |
提出日時 | 2020-05-17 13:28:35 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 482 ms / 2,000 ms |
コード長 | 5,426 bytes |
コンパイル時間 | 2,002 ms |
コンパイル使用メモリ | 179,468 KB |
実行使用メモリ | 25,472 KB |
最終ジャッジ日時 | 2024-11-22 16:37:52 |
合計ジャッジ時間 | 7,845 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
5,248 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 2 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 | 2 ms
5,248 KB |
testcase_09 | AC | 2 ms
5,248 KB |
testcase_10 | AC | 2 ms
5,248 KB |
testcase_11 | AC | 5 ms
5,248 KB |
testcase_12 | AC | 5 ms
5,248 KB |
testcase_13 | AC | 4 ms
5,248 KB |
testcase_14 | AC | 4 ms
5,248 KB |
testcase_15 | AC | 4 ms
5,248 KB |
testcase_16 | AC | 4 ms
5,248 KB |
testcase_17 | AC | 5 ms
5,248 KB |
testcase_18 | AC | 5 ms
5,248 KB |
testcase_19 | AC | 5 ms
5,248 KB |
testcase_20 | AC | 4 ms
5,248 KB |
testcase_21 | AC | 475 ms
25,344 KB |
testcase_22 | AC | 471 ms
25,344 KB |
testcase_23 | AC | 482 ms
25,472 KB |
testcase_24 | AC | 470 ms
25,344 KB |
testcase_25 | AC | 479 ms
25,344 KB |
testcase_26 | AC | 390 ms
25,344 KB |
testcase_27 | AC | 391 ms
25,344 KB |
testcase_28 | AC | 389 ms
25,344 KB |
testcase_29 | AC | 394 ms
25,472 KB |
testcase_30 | AC | 390 ms
25,344 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 range_add_range_min{ int N; vector<int> ST; vector<int> lazy; range_add_range_min(vector<long long> &A){ int n = A.size(); N = 1; while (N < n){ N *= 2; } ST = vector<int>(N * 2 - 1, INF); lazy = vector<int>(N * 2 - 1, 0); for (int i = 0; i < n; i++){ ST[N - 1 + i] = A[i]; } for (int i = N - 2; i >= 0; i--){ ST[i] = min(ST[i * 2 + 1], ST[i * 2 + 2]); } } void eval(int i){ if (i < N - 1){ lazy[i * 2 + 1] += lazy[i]; lazy[i * 2 + 2] += lazy[i]; } ST[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[i] = min(ST[i * 2 + 1], ST[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[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); } }; struct range_add_range_max{ int N; vector<int> ST; vector<int> lazy; range_add_range_max(vector<long long> &A){ int n = A.size(); N = 1; while (N < n){ N *= 2; } ST = vector<int>(N * 2 - 1, -INF); lazy = vector<int>(N * 2 - 1, 0); for (int i = 0; i < n; i++){ ST[N - 1 + i] = A[i]; } for (int i = N - 2; i >= 0; i--){ ST[i] = max(ST[i * 2 + 1], ST[i * 2 + 2]); } } void eval(int i){ if (i < N - 1){ lazy[i * 2 + 1] += lazy[i]; lazy[i * 2 + 2] += lazy[i]; } ST[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[i] = max(ST[i * 2 + 1], ST[i * 2 + 2]); } } void range_add(int L, int R, int x){ range_add(L, R, x, 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[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 range_add_range_sqsum{ int N; vector<long long> ST1; //sum vector<long long> ST2; //sqsum vector<long long> lazy; range_add_range_sqsum(vector<long long> &A){ int n = A.size(); N = 1; while (N < n){ N *= 2; } ST1 = vector<long long>(N * 2 - 1, 0); ST2 = vector<long long>(N * 2 - 1, 0); lazy = vector<long long>(N * 2 - 1, 0); for (int i = 0; i < n; i++){ ST1[N - 1 + i] = A[i]; ST2[N - 1 + i] = A[i] * A[i]; } for (int i = N - 2; i >= 0; i--){ ST1[i] = ST1[i * 2 + 1] + ST1[i * 2 + 2]; ST2[i] = ST2[i * 2 + 1] + ST2[i * 2 + 2]; } } void eval(int i, int l, int r){ if (i < N - 1){ lazy[i * 2 + 1] += lazy[i]; lazy[i * 2 + 2] += lazy[i]; } ST2[i] = ST2[i] + lazy[i] * ST1[i] * 2 + lazy[i] * lazy[i] * (r - l); ST1[i] = ST1[i] + lazy[i] * (r - l); lazy[i] = 0; } void range_add(int L, int R, int x, int i, int l, int r){ eval(i, l, r); if (R <= l || r <= L){ return; } else if (L <= l && r <= R){ lazy[i] += x; eval(i, l, r); } 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); ST1[i] = ST1[i * 2 + 1] + ST1[i * 2 + 2]; ST2[i] = ST2[i * 2 + 1] + ST2[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, l, r); if (R <= l || r <= L){ return 0; } else if (L <= l && r <= R){ return ST2[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); } range_add_range_sqsum a(A); range_add_range_min b(A); range_add_range_max c(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); c.range_add(l - 1, r, x); assert(b.range_min(0, N) >= MIN_A); assert(c.range_max(0, N) <= MAX_A); } if (t == 2){ cnt++; int l, r; cin >> l >> r; assert(1 <= l && l <= r && r <= N); cout << a.range_sqsum(l - 1, r) << endl; } } assert(cnt >= 1); }