結果

問題 No.876 Range Compress Query
ユーザー leaf_1415leaf_1415
提出日時 2019-09-06 21:34:37
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 79 ms / 2,000 ms
コード長 1,419 bytes
コンパイル時間 1,953 ms
コンパイル使用メモリ 62,660 KB
実行使用メモリ 6,672 KB
最終ジャッジ日時 2023-09-06 22:48:30
合計ジャッジ時間 2,391 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,076 KB
testcase_01 AC 4 ms
4,916 KB
testcase_02 AC 3 ms
5,032 KB
testcase_03 AC 4 ms
5,080 KB
testcase_04 AC 4 ms
4,924 KB
testcase_05 AC 3 ms
5,132 KB
testcase_06 AC 3 ms
5,028 KB
testcase_07 AC 3 ms
5,068 KB
testcase_08 AC 4 ms
4,920 KB
testcase_09 AC 4 ms
4,968 KB
testcase_10 AC 3 ms
5,016 KB
testcase_11 AC 72 ms
6,324 KB
testcase_12 AC 61 ms
6,244 KB
testcase_13 AC 62 ms
6,400 KB
testcase_14 AC 72 ms
6,196 KB
testcase_15 AC 54 ms
6,508 KB
testcase_16 AC 71 ms
6,584 KB
testcase_17 AC 72 ms
6,672 KB
testcase_18 AC 79 ms
6,396 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <utility>
#define llint long long
#define inf 1e18

using namespace std;
typedef pair<llint, llint> P;

struct SegTree{
	int size;
	vector<llint> seg;
	
	SegTree(){}
	SegTree(int size){
		this->size = size;
		seg.resize(1<<(size+1));
	}
	
	void init()
	{
		for(int i = 0; i < (1<<(size+1)); i++) seg[i] = 0;
	}
	
	void update(int i, llint val)
	{
		i += (1 << size);
		seg[i] = val;
		while(i > 1){
			i /= 2;
			seg[i] = (seg[i*2] + seg[i*2+1]);
		}
	}

	llint query(int a, int b, int k, int l, int r)
	{
		if(b < l || r < a) return 0;
		if(a <= l && r <= b) return seg[k];
		llint lval = query(a, b, k*2, l, (l+r)/2);
		llint rval = query(a, b, k*2+1, (l+r)/2+1, r);
		return lval + rval;
	}
	llint query(int a, int b)
	{
		if(a > b) return 0;
		return query(a, b, 1, 0, (1<<size)-1);
	}
};

llint n, Q;
llint a[100005], d[100005];
SegTree seg(17);

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n >> Q;
	for(int i = 1; i <= n; i++) cin >> a[i];
	for(int i = 1; i <= n-1; i++) d[i] = a[i+1]-a[i];
	
	seg.init();
	for(int i = 1; i <= n-1; i++) seg.update(i, (d[i]==0)?0:1);
	
	llint t, l, r, x;
	for(int q = 0; q < Q; q++){
		cin >> t >> l >> r;
		if(t == 1){
			cin >> x;
			d[l-1]+=x, d[r]-=x;
			seg.update(l-1, (d[l-1]==0)?0:1);
			seg.update(r, (d[r]==0)?0:1);
		}
		else{
			cout << seg.query(l, r-1)+1 << "\n";
		}
	}
	flush(cout);
	return 0;
}
0