結果

問題 No.879 Range Mod 2 Query
ユーザー e869120e869120
提出日時 2019-09-06 22:30:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 476 ms / 3,000 ms
コード長 2,573 bytes
コンパイル時間 742 ms
コンパイル使用メモリ 78,264 KB
実行使用メモリ 22,756 KB
最終ジャッジ日時 2023-09-07 03:33:37
合計ジャッジ時間 6,152 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
22,040 KB
testcase_01 AC 6 ms
21,928 KB
testcase_02 AC 6 ms
21,912 KB
testcase_03 AC 6 ms
21,920 KB
testcase_04 AC 6 ms
22,096 KB
testcase_05 AC 6 ms
21,920 KB
testcase_06 AC 6 ms
21,984 KB
testcase_07 AC 6 ms
22,084 KB
testcase_08 AC 6 ms
21,896 KB
testcase_09 AC 6 ms
21,916 KB
testcase_10 AC 6 ms
21,948 KB
testcase_11 AC 405 ms
22,756 KB
testcase_12 AC 231 ms
22,352 KB
testcase_13 AC 293 ms
22,660 KB
testcase_14 AC 294 ms
22,400 KB
testcase_15 AC 253 ms
22,468 KB
testcase_16 AC 448 ms
22,624 KB
testcase_17 AC 476 ms
22,656 KB
testcase_18 AC 476 ms
22,684 KB
testcase_19 AC 286 ms
22,688 KB
testcase_20 AC 289 ms
22,736 KB
testcase_21 AC 305 ms
22,716 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

const int BACKET = 300;
long long N, Q, A[1 << 18], T[1 << 18], L[1 << 18], R[1 << 18], X[1 << 18], ans[1 << 18];
long long S[1 << 18], Z1[1 << 18], Z2[1 << 18], ZADD[1 << 18], used[1 << 18];

void ranged(int l, int r) {
	vector<long long> 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++) { S[i] = 0; Z1[i] = 0; Z2[i] = 0; ZADD[i] = 0; used[i] = 0; }
	for (int i = 0; i < C.size() - 1; i++) {
		for (int j = C[i]; j < C[i + 1]; j++) { S[i] += A[j]; if (A[j] % 2 == 0) Z1[i]++; else Z2[i]++; }
	}

	for (int i = l; i < r; i++) {
		if (T[i] == 2) {
			int pos1 = lower_bound(C.begin(), C.end(), L[i]) - C.begin();
			int pos2 = lower_bound(C.begin(), C.end(), R[i]) - C.begin();
			for (int j = pos1; j < pos2; j++) { ZADD[j] += X[i]; }
		}
		if (T[i] == 1) {
			int pos1 = lower_bound(C.begin(), C.end(), L[i]) - C.begin();
			int pos2 = lower_bound(C.begin(), C.end(), R[i]) - C.begin();
			for (int j = pos1; j < pos2; j++) {
				if (used[j] == 0) { if (ZADD[j] % 2 == 0) used[j] = 1; else used[j] = 2; }
				else if (used[j] == 1) { if (ZADD[j] % 2 == 0) used[j] = 1; else used[j] = 2; }
				else if (used[j] == 2) { if (ZADD[j] % 2 == 0) used[j] = 2; else used[j] = 1; }
				if (ZADD[j] % 2 == 1) swap(Z1[j], Z2[j]);
				ZADD[j] = 0;
			}
		}
		if (T[i] == 3) {
			int pos1 = lower_bound(C.begin(), C.end(), L[i]) - C.begin();
			int pos2 = lower_bound(C.begin(), C.end(), R[i]) - C.begin();
			for (int j = pos1; j < pos2; j++) {
				if (used[j] == 0) ans[i] += S[j] + 1LL * (C[j + 1] - C[j]) * ZADD[j];
				else ans[i] += Z2[j] + (C[j + 1] - C[j]) * ZADD[j];
			}
		}
	}

	for (int i = 0; i < C.size() - 1; i++) {
		for (int j = C[i]; j < C[i + 1]; j++) {
			if (used[i] == 0) A[j] += ZADD[i];
			if (used[i] == 1) { if (A[j] % 2 == 0) A[j] = 0LL + ZADD[i]; else A[j] = 1LL + ZADD[i]; }
			if (used[i] == 2) { if (A[j] % 2 == 0) A[j] = 1LL + ZADD[i]; else A[j] = 0LL + ZADD[i]; }
		}
	}
}

int main() {
	scanf("%lld%lld", &N, &Q);
	for (int i = 1; i <= N; i++) scanf("%lld", &A[i]);
	for (int i = 1; i <= Q; i++) {
		scanf("%lld%lld%lld", &T[i], &L[i], &R[i]); R[i]++;
		if (T[i] == 2) scanf("%lld", &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) printf("%lld\n", ans[i]);
	}
	return 0;
}
0