結果

問題 No.879 Range Mod 2 Query
ユーザー QCFiumQCFium
提出日時 2020-01-26 13:21:04
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,542 bytes
コンパイル時間 2,123 ms
コンパイル使用メモリ 174,784 KB
実行使用メモリ 12,416 KB
最終ジャッジ日時 2024-09-14 10:58:04
合計ジャッジ時間 28,730 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
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 TLE -
testcase_17 TLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

int ri() {
	int n;
	scanf("%d", &n);
	return n;
}


struct SegTree {
	int n;
	struct Node {
		int64_t sum;
		int added;
		int compressed;
		int oddcnt;
		int l;
		int r;
	};
	std::vector<Node> nodes;
	SegTree (const std::vector<int> &a) {
		for (n = 1; n < (int) a.size(); n <<= 1);
		nodes.resize(n << 1);
		for (int i = 0; i < (int) a.size(); i++) nodes[i + n] = {a[i], 0, 0, a[i] & 1, i, i + 1};
		for (int i = a.size(); i < n; i++) nodes[i + n] = {0, 0, 0, 0, i, i + 1};
		for (int i = n - 1; i; i--) fetch(i);
	}
	void fetch(int i) {
		nodes[i].sum = nodes[i << 1].sum + nodes[i << 1 | 1].sum;
		nodes[i].oddcnt = nodes[i << 1].oddcnt + nodes[i << 1 | 1].oddcnt;
		nodes[i].l = nodes[i << 1].l;
		nodes[i].r = nodes[i << 1 | 1].r;
	}
	void flush(int i) {
		if (nodes[i].compressed) {
			if (nodes[i].compressed & 1) nodes[i].oddcnt = nodes[i].r - nodes[i].l - nodes[i].oddcnt, nodes[i].compressed = 1;
			else nodes[i].compressed = 2;
			nodes[i].sum = nodes[i].oddcnt;
			if (i < n) {
				nodes[i << 1].compressed += nodes[i].compressed;
				if (nodes[i << 1].added) {
					if (nodes[i << 1].added & 1) nodes[i << 1].compressed++;
					nodes[i << 1].added = 0;
				}
				nodes[i << 1 | 1].compressed += nodes[i].compressed;
				if (nodes[i << 1 | 1].added) {
					if (nodes[i << 1 | 1].added & 1) nodes[i << 1 | 1].compressed++;
					nodes[i << 1 | 1].added = 0;
				}
			}
			nodes[i].compressed = 0;
		}
		if (nodes[i].added) {
			if (nodes[i].added & 1) nodes[i].oddcnt = nodes[i].r - nodes[i].l - nodes[i].oddcnt;
			nodes[i].sum += (int64_t) (nodes[i].r - nodes[i].l) * nodes[i].added;
			if (i < n) {
				nodes[i << 1].added += nodes[i].added;
				nodes[i << 1 | 1].added += nodes[i].added;
			}
			nodes[i].added = 0;
		}
	}
	void mod2(int l, int r, int node = 1) {
		flush(node);
		if (l >= nodes[node].r || r <= nodes[node].l) return;
		if (l <= nodes[node].l && r >= nodes[node].r) {
			nodes[node].compressed = 2;
			flush(node);
		} else {
			mod2(l, r, node << 1);
			mod2(l, r, node << 1 | 1);
			fetch(node);
		}
	}
	void add(int l, int r, int val, int node = 1) {
		flush(node);
		if (l >= nodes[node].r || r <= nodes[node].l) return;
		if (l <= nodes[node].l && r >= nodes[node].r) {
			nodes[node].added = val;
			flush(node);
		} else {
			add(l, r, val, node << 1);
			add(l, r, val, node << 1 | 1);
			fetch(node);
		}
	}
	int64_t sum(int l, int r, int node = 1) {
		flush(node);
		if (l >= nodes[node].r || r <= nodes[node].l) return 0;
		if (l <= nodes[node].l && r >= nodes[node].r) return nodes[node].sum;
		int64_t res = sum(l, r, node << 1) + sum(l, r, node << 1 | 1);
		fetch(node);
		return res;
	}
};
struct SegTreeGu {
	std::vector<int> a;
	SegTreeGu(const std::vector<int> &a) : a(a) {}
	void mod2(int l, int r) {
		for (int i = l; i < r; i++) a[i] &= 1;
	}
	int sum(int l, int r) {
		int res = 0;
		for (int i = l; i < r; i++) res += a[i];
		return res;
	}
	void add(int l, int r, int val) {
		for (int i = l; i < r; i++) a[i] += val;
	}
};

int main() {
	int n = ri(), q = ri();
	std::vector<int> a(n);
	for (auto &i : a) i = ri();
	SegTree tree(a);
	SegTreeGu tree2(a);
	for (int i = 0; i < q; i++) {
		int t = ri();
		int l = ri() - 1;
		int r = ri();
		int val = t == 2 ? ri() : 0;
		if (t == 1) tree.mod2(l, r);
		else if (t == 2) tree.add(l, r, val);
		else if (t == 3) printf("%lld\n", (long long) tree.sum(l, r));
		if (t == 1) tree2.mod2(l, r);
		else if (t == 2) tree2.add(l, r, val);
		else if (t == 3) printf("%lld\n", (long long) tree2.sum(l, r));
	}
	return 0;
}
0