結果

問題 No.876 Range Compress Query
ユーザー pockynypockyny
提出日時 2019-09-06 22:02:32
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 204 ms / 2,000 ms
コード長 1,081 bytes
コンパイル時間 739 ms
コンパイル使用メモリ 74,524 KB
最終ジャッジ日時 2025-01-07 16:49:42
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;
using ll = long long;
ll n,t[200010] = {},mx = 100000000000000;
void built(){
	for(int i = n-1;i>0;i--){
		t[i] = t[i<<1] + t[i<<1|1];
	}
}

void update(ll p,ll a){
	for(t[p+=n] = a;p>1;p >>= 1){
		t[p>>1] = t[p] + t[p^1];
	}
}

ll query(int l, int r){
	ll mn = 1;
	for(l += n, r += n; l<r; l >>= 1,r >>= 1){
		if(l&1) mn += t[l++];
		if(r&1) mn += t[--r];
	}
	return mn;
}

void write(){
	int i;
	cout << endl;
	for(i=1;i<=10;i++){
		cout << t[i] << " ";
		if(i==1 || i==3 || i==7){
			cout << endl;
		}
	}
	cout << endl;
}
ll a[100010];
int main(){
	int i,j,q;
	cin >> n >> q;
	for(i=0;i<n;i++){
		cin >> a[i];
	}
	n--;
	for(i=0;i<n;i++){
		a[i] = a[i] - a[i + 1];
	}
	for(i=0;i<n;i++){
		if(a[i]!=0) t[i + n] = 1;
	}
	built();
	for(i=0;i<q;i++){
		//write();
		int x,y,l,r;
		cin >> y >> l >> r;
		l--; r--;
		if(y==1){
			cin >> x;
			if(l>0){
				a[l - 1] -= x;
				update(l - 1,(a[l - 1]!=0));
			}
			a[r] += x; update(r,(a[r]!=0));
		}else{
			cout << query(l,r) << endl;
		}
	}
	//write();
}
0