結果

問題 No.1099 Range Square Sum
ユーザー okok
提出日時 2020-06-27 00:06:25
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 308 ms / 2,000 ms
コード長 2,409 bytes
コンパイル時間 935 ms
コンパイル使用メモリ 88,476 KB
実行使用メモリ 17,076 KB
最終ジャッジ日時 2023-09-18 09:34:32
合計ジャッジ時間 5,584 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 3 ms
4,376 KB
testcase_13 AC 3 ms
4,376 KB
testcase_14 AC 3 ms
4,384 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 3 ms
4,380 KB
testcase_18 AC 3 ms
4,376 KB
testcase_19 AC 3 ms
4,376 KB
testcase_20 AC 3 ms
4,376 KB
testcase_21 AC 305 ms
17,076 KB
testcase_22 AC 308 ms
16,980 KB
testcase_23 AC 306 ms
16,952 KB
testcase_24 AC 308 ms
16,892 KB
testcase_25 AC 306 ms
17,044 KB
testcase_26 AC 220 ms
16,948 KB
testcase_27 AC 219 ms
16,956 KB
testcase_28 AC 219 ms
16,932 KB
testcase_29 AC 220 ms
17,068 KB
testcase_30 AC 219 ms
16,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include<utility>

using namespace std;

#define int long long
#define endl "\n"

constexpr long long INF = (long long)1e18;
constexpr long long MOD = 1'000'000'007; 

struct fast_io {
	fast_io(){
		std::cin.tie(nullptr);
		std::ios::sync_with_stdio(false);
	};
} fio;

template<typename T>
class lazy_segment_tree{
	int n;
	T fval;
	vector<T> dat;
	vector<int>  lazy;
	
public:
	
	lazy_segment_tree(){
		
	}
	
	lazy_segment_tree(int n_, T val){
		init(n_, val);
	}
	
	~lazy_segment_tree(){
		
	}
	
	void init(int n_, T val){
		fval = val;
		
		n = 1;
		
		while(n < n_) n *= 2;
		
		dat.resize(2 * n - 1);
		lazy.resize(2 * n - 1, 0);
	
		for(int i = 0; i < 2 * n - 1; i++) dat[i] = fval;
	}
	
	void eval(int k, int l, int r){
		
		if(lazy[k] != 0){
			int x = lazy[k] / (r - l);
			
			dat[k] = {dat[k].first + 2 * dat[k].second * x + (r - l) * x * x, dat[k].second + (r - l) * x};
			
			if(r - l > 1) {
				lazy[2*k+1] = lazy[2*k+1] + lazy[k]/2;
				lazy[2*k+2] = lazy[2*k+2] + lazy[k]/2;
			}
			
			lazy[k] = 0;
		}
	}
	
	void update(int a, int b, int x, int k = 0, int l = 0, int r = -1){
		if(r < 0) r = n;
		eval(k, l, r);
		if(b <= l || r <= a) {return ;}
		if(a <= l && r <= b){
			lazy[k] = lazy[k] + (r - l) * 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);
			dat[k] = {dat[2*k+1].first + dat[2*k+2].first, dat[2*k+1].second + dat[2*k+2].second};
		}
	}
	
	T query(int a, int b, int k = 0, int l = 0, int r = -1){
		if(r < 0) r = n;
		eval(k, l, r);
		if(r <= a || b <= l){
			return fval;
		}
		if(a <= l && r <= b){
			return dat[k];
		}else {
			T vl = query(a, b, k * 2 + 1, l, (l + r) / 2 );
			T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
			return {vl.first + vr.first, vl.second + vr.second};
		}
	}
};


signed main(){
	cout<<fixed<<setprecision(10);
	
	int N, Q;
	vector<int> A;
	lazy_segment_tree<pair<int,int>> lseg;
	
	cin>>N;
	
	A.resize(N);
	
	lseg.init(N+3, {0,0});
	
	for(int i = 0; i < N; i++){
		cin>>A[i];
		lseg.update(i+1, i+2, A[i]);
	}
	
	cin>>Q;
	
	for(int i = 0; i < Q; i++){
		int q;
		
		cin>>q;
		
		if(q == 1) {
			int l, r, x;
			
			cin>>l>>r>>x;
			
			lseg.update(l, r+1, x);
			
		} else {
			int l, r;
			
			cin>>l>>r;
			
			cout<<lseg.query(l, r+1).first<<endl;
		}
	}
	
	
	return 0;
}
0