結果

問題 No.1099 Range Square Sum
ユーザー e869120e869120
提出日時 2020-06-26 21:51:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,006 bytes
コンパイル時間 890 ms
コンパイル使用メモリ 88,500 KB
実行使用メモリ 22,252 KB
最終ジャッジ日時 2023-09-18 04:10:53
合計ジャッジ時間 4,214 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
22,020 KB
testcase_01 AC 5 ms
21,948 KB
testcase_02 AC 6 ms
22,028 KB
testcase_03 AC 5 ms
21,904 KB
testcase_04 AC 5 ms
22,184 KB
testcase_05 AC 6 ms
21,972 KB
testcase_06 AC 5 ms
22,032 KB
testcase_07 AC 5 ms
21,960 KB
testcase_08 AC 6 ms
22,120 KB
testcase_09 AC 6 ms
21,968 KB
testcase_10 AC 6 ms
22,184 KB
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 -
権限があれば一括ダウンロードができます

ソースコード

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];
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;
		}
	}
}

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