結果

問題 No.1234 典型RMQ
ユーザー chocoruskchocorusk
提出日時 2020-09-18 21:53:23
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 331 ms / 2,000 ms
コード長 2,742 bytes
コンパイル時間 1,570 ms
コンパイル使用メモリ 139,668 KB
実行使用メモリ 8,960 KB
最終ジャッジ日時 2024-04-26 11:07:26
合計ジャッジ時間 9,342 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 299 ms
8,704 KB
testcase_07 AC 256 ms
5,376 KB
testcase_08 AC 319 ms
8,832 KB
testcase_09 AC 298 ms
6,016 KB
testcase_10 AC 313 ms
8,832 KB
testcase_11 AC 306 ms
8,320 KB
testcase_12 AC 285 ms
5,888 KB
testcase_13 AC 261 ms
5,376 KB
testcase_14 AC 281 ms
6,016 KB
testcase_15 AC 278 ms
5,888 KB
testcase_16 AC 317 ms
8,704 KB
testcase_17 AC 282 ms
6,144 KB
testcase_18 AC 238 ms
5,376 KB
testcase_19 AC 331 ms
8,704 KB
testcase_20 AC 232 ms
8,960 KB
testcase_21 AC 317 ms
8,576 KB
testcase_22 AC 257 ms
8,832 KB
testcase_23 AC 245 ms
8,832 KB
testcase_24 AC 246 ms
8,832 KB
testcase_25 AC 243 ms
8,832 KB
testcase_26 AC 244 ms
8,960 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#include <list>
#define popcount __builtin_popcount
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
template<typename Monoid, typename OperatorMonoid=Monoid>
struct LazySegmentTree{
	using F=function<Monoid(Monoid, Monoid)>;
	using G=function<Monoid(Monoid, OperatorMonoid, int)>;
	using H=function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>;
	int sz;
	vector<Monoid> data;
	vector<OperatorMonoid> lazy;
	const F f;
	const G g;
	const H h;
	const Monoid e1;
	const OperatorMonoid e0;

	LazySegmentTree(int n, const F f, const G g, const H h, const Monoid &e1, const OperatorMonoid &e0): f(f), g(g), h(h), e1(e1), e0(e0){
		sz=1;
		while(sz<n) sz<<=1;
		data.resize(2*sz-1, e1);
		lazy.resize(2*sz-1, e0);
	}

	void build(vector<Monoid> v){
		for(int i=0; i<v.size(); i++) data[i+sz-1]=v[i];
		for(int i=sz-2; i>=0; i--) data[i]=f(data[2*i+1], data[2*i+2]);
	}

	void eval(int k, int l, int r){
		if(lazy[k]!=e0){
			data[k]=g(data[k], lazy[k], r-l);
			if(k<sz-1){
				lazy[2*k+1]=h(lazy[2*k+1], lazy[k]);
				lazy[2*k+2]=h(lazy[2*k+2], lazy[k]);
			}
		}
		lazy[k]=e0;
	}

	void update(int a, int b, const OperatorMonoid &x, int k, int l, int r){
		eval(k, l, r);
		if(r<=a || b<=l) return;
		if(a<=l && r<=b){
			lazy[k]=h(lazy[k], x);
			eval(k, l, r);
		}else{
			update(a, b, x, 2*k+1, l, (l+r)/2);
			update(a, b, x, 2*k+2, (l+r)/2, r);
			data[k]=f(data[2*k+1], data[2*k+2]);
		}
	}
	void update(int a, int b, const OperatorMonoid &x){
		return update(a, b, x, 0, 0, sz);
	}

	Monoid find(int a, int b, int k, int l, int r){
		eval(k, l, r);
		if(b<=l || r<=a) return e1;
		if(a<=l && r<=b) return data[k];
		else return f(find(a, b, 2*k+1, l, (l+r)/2), find(a, b, 2*k+2, (l+r)/2, r));
	}
	Monoid find(int a, int b){
		return find(a, b, 0, 0, sz);
	}
	Monoid operator[](const int &k){
		return find(k, k+1);
	}
};
int main()
{
	int n;
	cin>>n;
	vector<ll> a(n);
	for(int i=0; i<n; i++) cin>>a[i];
	auto f=[&](ll a, ll b){ return min(a, b);};
	auto g=[&](ll a, ll x, int len){
		return a+x;
	};
	auto h=[&](ll x, ll y){
		return x+y;
	};
	const ll INF=1e18;
	LazySegmentTree<ll> seg(n, f, g, h, INF, 0);
	seg.build(a);
	int q; cin>>q;
	while(q--){
		int k, l, r; ll c;
		cin>>k>>l>>r>>c;
		l--;
		if(k==1) seg.update(l, r, c);
		else cout<<seg.find(l, r)<<endl;
	}
	return 0;
}

0