結果

問題 No.876 Range Compress Query
ユーザー tkmst201tkmst201
提出日時 2019-09-06 22:38:54
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 421 ms / 2,000 ms
コード長 2,424 bytes
コンパイル時間 1,717 ms
コンパイル使用メモリ 154,468 KB
実行使用メモリ 7,420 KB
最終ジャッジ日時 2023-09-07 01:33:45
合計ジャッジ時間 6,012 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 3 ms
4,384 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 3 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 4 ms
4,384 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 402 ms
6,972 KB
testcase_12 AC 335 ms
7,420 KB
testcase_13 AC 342 ms
6,960 KB
testcase_14 AC 392 ms
7,360 KB
testcase_15 AC 303 ms
6,980 KB
testcase_16 AC 400 ms
6,964 KB
testcase_17 AC 402 ms
7,328 KB
testcase_18 AC 421 ms
7,332 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll INF = 1ll<<30;
const ll MOD = 1000000007;
const double EPS = 1e-9;
const bool debug = 0;
//---------------------------------//

struct SegTree {
	ll init_val;
	int n;
	vector<ll> dat, plus;
	
	SegTree(int _n) {
		init_val = 0;
		n = 1;
		while (n < _n) n *= 2;
		dat.resize(n * 2 - 1, init_val);
		plus.resize(n * 2 - 1, init_val);
	}
	
	void update(int i) {
		i += n - 1;
		dat[i] = 0;
		if (i + 1 < dat.size() && get(i - (n - 1)) != get(i + 1 - (n - 1))) dat[i] = 1;
		
		while (i > 0) {
			i = (i - 1) / 2;
			dat[i] = dat[i * 2 + 1] + dat[i * 2 + 2];
		}
	}
	
	void sumquery(int a, int b, ll x) {
		sum(a, b, x);
		update(a);
		if (a > 0) update(a - 1);
		update(b - 1);
		if (b - 1 > 0) update(b - 2);
	}
	
	void sum(int a, int b, ll x, int k = 0, int l = 0, int r = -1) {
		if (r < 0) r = n;
		
		if (r <= a || b <= l) return;
		if (a <= l && r <= b) plus[k] += x;
		else {
			sum(a, b, x, k * 2 + 1, l, (l + r) / 2);
			sum(a, b, x, k * 2 + 2, (l + r) / 2, r);
		}
	}
	
	ll get(int a, int k = 0, int l = 0, int r = -1) {
		int b = a + 1;
		if (r < 0) r = n;
		if (r <= a || b <= l) return init_val;
		
		ll res = plus[k];
		
		if (a <= l && r <= b) return res;
		
		res += get(a, k * 2 + 1, l, (l + r) / 2);
		res += get(a, k * 2 + 2, (l + r) / 2, r);
		return res;
	}
	
	// 探索範囲[a,b), 現在見ているkノード[l,r)
	ll query(int a, int b, int k = 0, int l = 0, int r = -1) {
		if (r < 0) r = n;
		
		if (l == r || r <= a || b <= l) return init_val;
		if (a <= l && r <= b) return dat[k];
		return query(a, b, k * 2 + 1, l, (l + r) / 2) + query(a, b, k * 2 + 2, (l + r) / 2, r);
	}
};

int N, Q;

int main() {
	cin >> N >> Q;
	
	SegTree seg(N);
	REP(i, N) {
		int a;
		scanf("%d", &a);
		seg.sumquery(i, i + 1, a);
	}
	
	REP(i, Q) {
		int q, l, r;
		cin >> q >> l >> r;
		l--; r--;
		if (q == 1) {
			ll x; cin >> x;
			seg.sumquery(l, r + 1, x);
		}
		else {
			cout << seg.query(l, r) + 1 << endl;
		}
	}
	
	return 0;
}
0