結果

問題 No.876 Range Compress Query
ユーザー 東前頭十一枚目東前頭十一枚目
提出日時 2019-09-07 18:08:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 217 ms / 2,000 ms
コード長 1,480 bytes
コンパイル時間 1,549 ms
コンパイル使用メモリ 171,240 KB
実行使用メモリ 6,576 KB
最終ジャッジ日時 2023-09-09 06:59:46
合計ジャッジ時間 4,519 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 211 ms
6,224 KB
testcase_12 AC 177 ms
6,220 KB
testcase_13 AC 177 ms
6,164 KB
testcase_14 AC 207 ms
6,224 KB
testcase_15 AC 150 ms
6,444 KB
testcase_16 AC 205 ms
6,520 KB
testcase_17 AC 205 ms
6,488 KB
testcase_18 AC 217 ms
6,576 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

template<class T>
struct SegmentTree {
	using F = function<T(T, T)>;
	const T init;
	const F f;
	int sz;
	vector<T> seg;
	SegmentTree(int n, const T init, const F f) :
		init(init),
		f(f)
		{
			sz = 1;
			while(sz < n) sz <<= 1;
			seg.resize(sz << 1, init);
		}
	void update(int id) {
		while(id >>= 1) {
			seg[id] = f(seg[2 * id], seg[2 * id + 1]);
		}
	}
	void replace(int id, T x){
		id += sz;
		seg[id] = x;
		update(id);
	}
	// [l,r)
	T get(int l, int r){
		T L = init, R = init;
		for(l += sz, r += sz; l < r; l >>= 1, r>>= 1) {
			if(l & 1) L = f(L, seg[l++]);
			if(r & 1) R = f(seg[--r], R);
		}
		return f(L, R);
	}
	void debug(){
		int i = 1;
		for(int r = 2; r <= 2 * sz; r <<= 1) {
			while(i < r) {
				cout << seg[i++] << " ";
			}
			cout << "\n";
		}
	}
};

int main() {
	int n, q; cin >> n >> q;
	int64_t a[n]; for(int i = 0; i < n; ++i) cin >> a[i];
	int64_t b[n - 1];
	SegmentTree<int64_t> seg(n - 1, 0, [](int lhs, int rhs){return lhs + rhs;});
	for(int i = 0; i < n - 1; ++i) {
		b[i] = a[i] - a[i + 1];
		if(b[i] != 0) seg.replace(i, 1);
	}
	while(q--) {
		int qq; cin >> qq;
		int l, r; cin >> l >> r;
		--l, --r;
		if(qq == 1) {
			int64_t x; cin >> x;
			if(l - 1 >= 0) {
				b[l - 1] -= x;
				seg.replace(l - 1, (b[l - 1] != 0 ? 1 : 0));
			}
			if(r < n - 1) {
				b[r] += x;
				seg.replace(r, (b[r] != 0 ? 1 : 0));
			}
		}
		if(qq == 2) {
			cout << seg.get(l, r) + 1 << '\n';
		}
	}
	return 0;
}
0