結果

問題 No.876 Range Compress Query
ユーザー QCFiumQCFium
提出日時 2019-09-06 21:51:51
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 61 ms / 2,000 ms
コード長 1,322 bytes
コンパイル時間 1,519 ms
コンパイル使用メモリ 168,188 KB
実行使用メモリ 5,012 KB
最終ジャッジ日時 2023-09-06 23:38:55
合計ジャッジ時間 3,273 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 59 ms
4,688 KB
testcase_12 AC 49 ms
4,636 KB
testcase_13 AC 50 ms
4,632 KB
testcase_14 AC 59 ms
4,628 KB
testcase_15 AC 43 ms
4,744 KB
testcase_16 AC 57 ms
4,956 KB
testcase_17 AC 59 ms
5,012 KB
testcase_18 AC 61 ms
5,000 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

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

struct SegTree {
	std::vector<int> data; // sum
	int n;
	SegTree (const std::vector<int> &a) {
		for (n = 1; n < (int) a.size(); n *= 2);
		data.resize(2 * n);
		for (int i = 0; i < (int) a.size(); i++) data[i + n] = !!a[i];
		for (int i = n - 1; i; i--) data[i] = data[i << 1] + data[i << 1 | 1];
	}
	void add(int i, int val) {
		for (i += n; i; i >>= 1) data[i] += val;
	}
	int sum(int l, int r) {
		int res = 0;
		for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
			if (r & 1) res += data[--r];
			if (l & 1) res += data[l++];
		}
		return res;
	}
};

int main() {
	int n = ri();
	int q = ri();
	std::vector<int> a(n);
	for (int i = 0; i < n; i++) a[i] = ri();
	std::vector<int> sub(n - 1);
	for (int i = 0; i + 1 < n; i++) sub[i] = a[i + 1] - a[i];
	SegTree tree(sub);
	for (int i = 0; i < q; i++) {
		int t = ri();
		int l = ri() - 1, r = ri() - 1;
		if (t == 1) {
			int x = ri();
			if (l) {
				sub[l - 1] += x;
				if (sub[l - 1] == x) tree.add(l - 1, 1);
				else if (sub[l - 1] == 0) tree.add(l - 1, -1);
			}
			if (r + 1 < n) {
				sub[r] -= x;
				if (sub[r] == -x) tree.add(r, 1);
				else if (sub[r] == 0) tree.add(r, -1);
			}
		} else {
			if (l == r) puts("1");
			else printf("%d\n", tree.sum(l, r) + 1);
		}
	}
	return 0;
}

0