結果

問題 No.1099 Range Square Sum
ユーザー e869120e869120
提出日時 2020-06-26 21:53:02
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 236 ms / 2,000 ms
コード長 2,274 bytes
コンパイル時間 995 ms
コンパイル使用メモリ 88,944 KB
実行使用メモリ 24,232 KB
最終ジャッジ日時 2023-09-18 04:15:40
合計ジャッジ時間 5,385 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
23,952 KB
testcase_01 AC 5 ms
23,988 KB
testcase_02 AC 6 ms
23,952 KB
testcase_03 AC 6 ms
24,076 KB
testcase_04 AC 6 ms
24,164 KB
testcase_05 AC 5 ms
24,016 KB
testcase_06 AC 5 ms
23,948 KB
testcase_07 AC 6 ms
23,984 KB
testcase_08 AC 6 ms
24,016 KB
testcase_09 AC 6 ms
24,228 KB
testcase_10 AC 6 ms
23,952 KB
testcase_11 AC 6 ms
24,024 KB
testcase_12 AC 7 ms
24,020 KB
testcase_13 AC 6 ms
24,020 KB
testcase_14 AC 6 ms
23,980 KB
testcase_15 AC 7 ms
23,960 KB
testcase_16 AC 6 ms
24,020 KB
testcase_17 AC 6 ms
24,084 KB
testcase_18 AC 7 ms
23,968 KB
testcase_19 AC 6 ms
23,948 KB
testcase_20 AC 7 ms
23,916 KB
testcase_21 AC 232 ms
23,968 KB
testcase_22 AC 231 ms
23,968 KB
testcase_23 AC 233 ms
24,112 KB
testcase_24 AC 231 ms
23,956 KB
testcase_25 AC 235 ms
24,028 KB
testcase_26 AC 235 ms
23,952 KB
testcase_27 AC 233 ms
24,164 KB
testcase_28 AC 235 ms
24,232 KB
testcase_29 AC 236 ms
23,996 KB
testcase_30 AC 233 ms
23,912 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <map>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
#pragma warning (disable: 4996)

const int BACKET = 500;
long long N, A[1 << 18], B[1 << 18];
long long Q, X[1 << 18], Y[1 << 18], Z[1 << 18], W[1 << 18];
long long Answer[1 << 18];

// 平方分割
vector<int> vec;
long long V[1 << 18];
long long P1[1 << 18], P2[1 << 18], P3[1 << 18];

void solve(int cl, int cr) {
	// 初期化
	vec.clear();
	for (int i = 0; i <= BACKET * 2 + 100; i++) {
		V[i] = 0; P1[i] = 0; P2[i] = 0; P3[i] = 0;
	}

	// ソート
	vec.clear();
	vec.push_back(1); vec.push_back(N + 1);
	for (int i = cl; i <= cr; i++) vec.push_back(Y[i]);
	for (int i = cl; i <= cr; i++) vec.push_back(Z[i] + 1);
	sort(vec.begin(), vec.end());
	vec.erase(unique(vec.begin(), vec.end()), vec.end());

	// 前準備
	for (int i = 0; i < vec.size() - 1; i++) {
		for (int j = vec[i]; j < vec[i + 1]; j++) {
			P1[i] += 1LL;
			P2[i] += 2LL * A[j];
			P3[i] += A[j] * A[j];
		}
	}

	// 平方分割
	for (int i = cl; i <= cr; i++) {
		int pos1 = lower_bound(vec.begin(), vec.end(), (int)Y[i]) - vec.begin();
		int pos2 = lower_bound(vec.begin(), vec.end(), (int)Z[i] + 1) - vec.begin();
		if (X[i] == 1) {
			for (int j = pos1; j < pos2; j++) V[j] += W[i];
		}
		if (X[i] == 2) {
			long long ans = 0;
			for (int j = pos1; j < pos2; j++) ans += V[j] * V[j] * P1[j] + V[j] * P2[j] + P3[j];
			Answer[i] = ans;
		}
	}

	// 上乗せ
	for (int i = 1; i <= N; i++) B[i] = A[i] - A[i - 1];
	for (int i = cl; i <= cr; i++) {
		if (X[i] == 1) { B[Y[i]] += W[i]; B[Z[i] + 1] -= W[i]; }
	}
	for (int i = 1; i <= N; i++) B[i] += B[i - 1];
	for (int i = 1; i <= N; i++) A[i] = B[i];
}

int main() {
	// 入力
	scanf("%lld", &N);
	for (int i = 1; i <= N; i++) scanf("%lld", &A[i]);
	scanf("%lld", &Q);
	for (int i = 1; i <= Q; i++) {
		scanf("%lld", &X[i]);
		if (X[i] == 1) scanf("%lld%lld%lld", &Y[i], &Z[i], &W[i]);
		if (X[i] == 2) scanf("%lld%lld", &Y[i], &Z[i]);
	}

	// 平方分割
	for (int i = 1; i <= Q; i += BACKET) {
		int cl = i, cr = i + BACKET - 1; cr = min(cr, (int)Q);
		solve(cl, cr);
	}

	// 出力
	for (int i = 1; i <= Q; i++) {
		if (X[i] == 2) printf("%lld\n", Answer[i]);
	}
	return 0;
}
0