結果

問題 No.879 Range Mod 2 Query
ユーザー ianCKianCK
提出日時 2019-12-07 07:22:54
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,098 bytes
コンパイル時間 1,429 ms
コンパイル使用メモリ 61,252 KB
実行使用メモリ 12,336 KB
最終ジャッジ日時 2023-08-25 23:51:46
合計ジャッジ時間 4,430 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
7,068 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
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 AC 131 ms
12,208 KB
testcase_20 AC 133 ms
12,216 KB
testcase_21 AC 142 ms
12,116 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
typedef long long int ll;
constexpr int kN = int(1E5 + 10);

void swap(int &a, int &b) {
	int temp = a;
	a = b;
	b = temp;
	return ;
}

struct seg_tree {
	ll tot[kN << 2], flag[kN << 2];
	int odd[kN << 2], even[kN << 2];
	void pull(int n) {
		tot[n] = tot[n * 2 + 1] + tot[n * 2 + 2];
		odd[n] = odd[n * 2 + 1] + odd[n * 2 + 2];
		even[n] = even[n * 2 + 1] + even[n * 2 + 2];
		return ;
	}
	void addtag(int n, ll x) {
		if (x == -1) {
			flag[n] = x;
			tot[n] = odd[n];
		}
		else {
			if (flag[n] == -1) push(n);
			if (x & 1) swap(odd[n], even[n]);
			flag[n] += x;
			tot[n] += (odd[n] + even[n]) * x;
		}
		return ;
	}
	void push(int n) {
		if (odd[n] + even[n] > 1 && flag[n] != 0) {
			addtag(n * 2 + 1, flag[n]);
			addtag(n * 2 + 2, flag[n]);
		}
		flag[n] = 0;
		return ;
	}
	void init(int n, int l, int r, int a[]) {
		flag[n] = 0;
		if (l == r) {
			tot[n] = a[l];
			if (a[l] & 1) odd[n] = 1, even[n] = 0;
			else odd[n] = 0, even[n] = 1;
		}
		else {
			int mid = (l + r) >> 1;
			init(n * 2 + 1, l, mid, a);
			init(n * 2 + 2, mid + 1, r, a);
			pull(n);
		}
		return ;
	}
	void fix(int n, int l, int r, int L, int R, int x) {
		if (L <= l && r <= R) addtag(n, x);
		else if (!(L > r || l > R)) {
			int mid = (l + r) >> 1;
			push(n);
			fix(n * 2 + 1, l, mid, L, R, x);
			fix(n * 2 + 2, mid + 1, r, L, R, x);
			pull(n);
		}
		return ;
	}
	ll ask(int n, int l, int r, int L, int R) {
		if (L <= l && r <= R) return tot[n];
		else if (l > R || L > r) return 0;
		else {
			int mid = (l + r) >> 1;
			push(n);
			return ask(n * 2 + 1, l, mid, L, R) + ask(n * 2 + 2, mid + 1, r, L, R);
		}
	}
};

seg_tree sg;
int a[kN];

int main() {
	int n, q, k, l, r, x;
	scanf("%d%d", &n, &q);
	for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
	sg.init(0, 1, n, a);
	while (q--) {
		scanf("%d%d%d", &k, &l, &r);
		if (k == 1) sg.fix(0, 1, n, l, r, -1);
		else if (k == 2) {
			scanf("%d", &x);
			sg.fix(0, 1, n, l, r, x);
		}
		else printf("%lld\n", sg.ask(0, 1, n, l, r));
	}
}
0