結果

問題 No.1234 典型RMQ
ユーザー leaf_1415leaf_1415
提出日時 2020-09-18 21:40:50
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 211 ms / 2,000 ms
コード長 2,646 bytes
コンパイル時間 2,693 ms
コンパイル使用メモリ 87,500 KB
実行使用メモリ 8,208 KB
最終ジャッジ日時 2023-08-08 18:35:10
合計ジャッジ時間 7,349 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
7,412 KB
testcase_01 AC 5 ms
7,308 KB
testcase_02 AC 5 ms
7,292 KB
testcase_03 AC 5 ms
7,216 KB
testcase_04 AC 5 ms
7,196 KB
testcase_05 AC 5 ms
7,276 KB
testcase_06 AC 200 ms
7,856 KB
testcase_07 AC 172 ms
7,096 KB
testcase_08 AC 211 ms
7,740 KB
testcase_09 AC 190 ms
7,516 KB
testcase_10 AC 207 ms
7,756 KB
testcase_11 AC 197 ms
7,780 KB
testcase_12 AC 185 ms
7,596 KB
testcase_13 AC 174 ms
7,128 KB
testcase_14 AC 188 ms
7,588 KB
testcase_15 AC 183 ms
7,592 KB
testcase_16 AC 206 ms
7,876 KB
testcase_17 AC 190 ms
7,768 KB
testcase_18 AC 165 ms
7,176 KB
testcase_19 AC 211 ms
7,780 KB
testcase_20 AC 154 ms
8,116 KB
testcase_21 AC 200 ms
7,820 KB
testcase_22 AC 164 ms
8,208 KB
testcase_23 AC 162 ms
7,988 KB
testcase_24 AC 161 ms
8,144 KB
testcase_25 AC 163 ms
8,140 KB
testcase_26 AC 164 ms
8,124 KB
testcase_27 AC 5 ms
7,200 KB
testcase_28 AC 5 ms
7,124 KB
testcase_29 AC 5 ms
7,268 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#include <utility>
#define llint long long
#define inf 1e18
#define rep(x, s, t) for(llint (x) = (s); (x) < (t); (x)++)
#define Rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))

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

// range-add, range-min query
 
struct LazySegTree{
	int size;
	vector<llint> seg, delay;
	
	LazySegTree(){}
	LazySegTree(int size){
		this->size = size;
		seg.resize(1<<(size+1));
		delay.resize(1<<(size+1));
	}
	
	void init()
	{
		for(int i = 0; i < (1<<(size+1)); i++){
			seg[i] = inf;
			delay[i] = 0;
		}
	}
	
	void eval(int l, int r, int k)
	{
		if(delay[k]){
			seg[k] += delay[k];  //総和クエリのときは区間幅を乗じる必要がある
			if(l < r){
				delay[k*2] += delay[k];
				delay[k*2+1] += delay[k];
			}
			delay[k] = 0;
		}
	}
	
	void update(int i, llint val)
	{
		int l = 0, r = (1<<size)-1, k = 1;
		eval(l, r, k);
		for(int j = size-1; j >= 0; j--){
			k <<= 1;
			if(i & (1<<j)){
				k++;
				r = (l+r)/2;
			}
			else l = (l+r)/2+1;
			eval(l, r, k);
		}
		
		i += (1 << size);
		seg[i] = val;
		while(i > 1){
			i /= 2;
			seg[i] = min(seg[i*2], seg[i*2+1]);
		}
	}
	
	void add(int a, int b, int k, int l, int r, llint val)
	{
		eval(l, r, k);
		
		if(b < l || r < a) return;
		if(a <= l && r <= b){
			delay[k] += val;
			eval(l, r, k);
			return;
		}
		add(a, b, k*2, l, (l+r)/2, val);
		add(a, b, k*2+1, (l+r)/2+1, r, val);
		seg[k] = min(seg[k*2], seg[k*2+1]);
	}
	void add(int a, int b, llint val){
		if(a > b) return;
		add(a, b, 1, 0, (1<<size)-1, val);
	}
 
	llint query(int a, int b, int k, int l, int r)
	{
		eval(l, r, k);
		
		if(b < l || r < a) return inf;
		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 min(lval, rval);
	}
	llint query(int a, int b)
	{
		return query(a, b, 1, 0, (1<<size)-1);
	}
};

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

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n;
	for(int i = 1; i <= n; i++) cin >> a[i];
	
	seg.init();
	for(int i = 1; i <= n; i++) seg.update(i, a[i]);
	
	cin >> Q;
	llint k, l, r, c;
	for(int i = 1; i <= Q; i++){
		cin >> k >> l >> r >> c;
		if(k == 1) seg.add(l, r, c);
		else cout << seg.query(l, r) << endl;
	}
	
	return 0;
}
0