結果

問題 No.880 Yet Another Segment Tree Problem
ユーザー e869120e869120
提出日時 2019-09-06 23:01:50
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4,954 ms / 5,000 ms
コード長 2,593 bytes
コンパイル時間 963 ms
コンパイル使用メモリ 78,344 KB
実行使用メモリ 10,368 KB
最終ジャッジ日時 2023-10-24 02:38:28
合計ジャッジ時間 34,364 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
9,968 KB
testcase_01 AC 5 ms
9,972 KB
testcase_02 AC 5 ms
9,972 KB
testcase_03 AC 5 ms
9,972 KB
testcase_04 AC 5 ms
9,972 KB
testcase_05 AC 4 ms
9,968 KB
testcase_06 AC 4 ms
9,968 KB
testcase_07 AC 5 ms
9,976 KB
testcase_08 AC 5 ms
9,976 KB
testcase_09 AC 5 ms
9,972 KB
testcase_10 AC 5 ms
9,976 KB
testcase_11 AC 1,420 ms
10,280 KB
testcase_12 AC 1,296 ms
10,300 KB
testcase_13 AC 956 ms
10,264 KB
testcase_14 AC 1,679 ms
10,348 KB
testcase_15 AC 1,766 ms
10,348 KB
testcase_16 AC 1,989 ms
10,356 KB
testcase_17 AC 4,763 ms
10,368 KB
testcase_18 AC 4,954 ms
10,368 KB
testcase_19 AC 419 ms
10,348 KB
testcase_20 AC 435 ms
10,368 KB
testcase_21 AC 429 ms
10,336 KB
testcase_22 AC 417 ms
10,356 KB
testcase_23 AC 440 ms
10,340 KB
testcase_24 AC 394 ms
10,348 KB
testcase_25 AC 405 ms
10,368 KB
testcase_26 AC 406 ms
10,336 KB
testcase_27 AC 390 ms
10,356 KB
testcase_28 AC 409 ms
10,340 KB
testcase_29 AC 1,593 ms
10,348 KB
testcase_30 AC 1,684 ms
10,348 KB
testcase_31 AC 1,899 ms
10,356 KB
testcase_32 AC 1,559 ms
10,232 KB
testcase_33 AC 300 ms
10,348 KB
testcase_34 AC 328 ms
10,352 KB
testcase_35 AC 309 ms
10,348 KB
testcase_36 AC 314 ms
10,360 KB
testcase_37 AC 306 ms
10,356 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#pragma warning (disable: 4996)

const int BACKET = 500;
int N, Q, A[1 << 18], T[1 << 18], L[1 << 18], R[1 << 18], X[1 << 18];
long long ans[1 << 18];
bool used[1 << 15];
int val[1 << 15], v_max[1 << 15]; long long v_sum[1 << 15];

/*int gcd(int a, int b) {
	if (b == 0) return a;
	return gcd(b, a % b);
}*/

void ranged(int l, int r) {
	// 分割
	vector<int> C;
	for (int i = l; i < r; i++) { C.push_back(L[i]); C.push_back(R[i]); }
	sort(C.begin(), C.end());
	C.erase(unique(C.begin(), C.end()), C.end());

	// 初期化
	for (int i = 0; i < C.size(); i++) { used[i] = false; val[i] = 0; v_max[i] = 0; v_sum[i] = 0; }
	for (int i = 0; i < C.size() - 1; i++) {
		for (int j = C[i]; j < C[i + 1]; j++) {
			v_max[i] = max(v_max[i], A[j]);
			v_sum[i] += A[j];
		}
	}

	// クエリに答える
	for (int i = l; i < r; i++) {
		int pos1 = lower_bound(C.begin(), C.end(), L[i]) - C.begin();
		int pos2 = lower_bound(C.begin(), C.end(), R[i]) - C.begin();

		// 置換
		if (T[i] == 1) {
			for (int j = pos1; j < pos2; j++) {
				used[j] = true;
				val[j] = X[i];
			}
		}
		// gcd
		if (T[i] == 2) {
			for (int j = pos1; j < pos2; j++) {
				if (used[j] == true) {
					val[j] = __gcd(val[j], X[i]);
				}
				if (used[j] == false) {
					int e = __gcd(val[j], X[i]);
					if (e == val[j]) continue;

					val[j] = e; v_max[j] = 0; v_sum[j] = 0;
					for (int k = C[j]; k < C[j + 1]; k++) {
						int ee = __gcd(e, A[k]);
						v_max[j] = max(v_max[j], ee);
						v_sum[j] += ee;
					}
				}
			}
		}
		// max を求める
		if (T[i] == 3) {
			for (int j = pos1; j < pos2; j++) {
				if (used[j] == true) ans[i] = max((int)ans[i], val[j]);
				else ans[i] = max((int)ans[i], v_max[j]);
			}
		}
		// 総和を求める
		if (T[i] == 4) {
			for (int j = pos1; j < pos2; j++) {
				if (used[j] == true) ans[i] += 1LL * (C[j + 1] - C[j]) * val[j];
				else ans[i] += v_sum[j];
			}
		}
	}

	// 更新
	for (int i = 0; i < C.size() - 1; i++) {
		for (int j = C[i]; j < C[i + 1]; j++) {
			if (used[i] == true) A[j] = val[i];
			else A[j] = __gcd(val[i], A[j]);
		}
	}
}

int main() {
	scanf("%d%d", &N, &Q);
	for (int i = 1; i <= N; i++) scanf("%d", &A[i]);
	for (int i = 1; i <= Q; i++) {
		scanf("%d%d%d", &T[i], &L[i], &R[i]); R[i]++;
		if (T[i] == 1 || T[i] == 2) scanf("%d", &X[i]);
	}
	for (int i = 1; i <= Q; i += BACKET) {
		int cl = i, cr = i + BACKET; cr = min(cr, (int)Q + 1);
		ranged(cl, cr);
	}
	for (int i = 1; i <= Q; i++) {
		if (T[i] == 3 || T[i] == 4) printf("%lld\n", ans[i]);
	}
	return 0;
}
0