結果

問題 No.1099 Range Square Sum
ユーザー leaf_1415leaf_1415
提出日時 2020-06-26 21:49:46
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 280 ms / 2,000 ms
コード長 2,864 bytes
コンパイル時間 1,211 ms
コンパイル使用メモリ 86,984 KB
実行使用メモリ 17,068 KB
最終ジャッジ日時 2023-09-18 04:08:15
合計ジャッジ時間 5,866 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
15,144 KB
testcase_01 AC 10 ms
15,016 KB
testcase_02 AC 9 ms
15,064 KB
testcase_03 AC 10 ms
15,220 KB
testcase_04 AC 10 ms
15,016 KB
testcase_05 AC 10 ms
15,160 KB
testcase_06 AC 10 ms
15,120 KB
testcase_07 AC 10 ms
15,164 KB
testcase_08 AC 10 ms
15,160 KB
testcase_09 AC 9 ms
15,316 KB
testcase_10 AC 10 ms
15,016 KB
testcase_11 AC 11 ms
15,244 KB
testcase_12 AC 12 ms
15,248 KB
testcase_13 AC 12 ms
15,176 KB
testcase_14 AC 11 ms
15,020 KB
testcase_15 AC 12 ms
15,132 KB
testcase_16 AC 12 ms
15,232 KB
testcase_17 AC 11 ms
15,012 KB
testcase_18 AC 11 ms
15,160 KB
testcase_19 AC 12 ms
15,060 KB
testcase_20 AC 11 ms
15,020 KB
testcase_21 AC 271 ms
16,844 KB
testcase_22 AC 270 ms
17,068 KB
testcase_23 AC 280 ms
16,844 KB
testcase_24 AC 271 ms
16,840 KB
testcase_25 AC 268 ms
16,772 KB
testcase_26 AC 208 ms
16,772 KB
testcase_27 AC 206 ms
16,772 KB
testcase_28 AC 208 ms
16,832 KB
testcase_29 AC 206 ms
16,872 KB
testcase_30 AC 206 ms
17,064 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, seg2, delay;
	
	LazySegTree(){}
	LazySegTree(int size){
		this->size = size;
		seg.resize(1<<(size+1));
		seg2.resize(1<<(size+1));
		delay.resize(1<<(size+1));
	}
	
	void init()
	{
		for(int i = 0; i < (1<<(size+1)); i++){
			seg[i] = 0, seg2[i] = 0;
			delay[i] = 0;
		}
	}
	
	void eval(int l, int r, int k)
	{
		if(delay[k]){
			seg2[k] += 2*seg[k]*delay[k] + (r-l+1)*delay[k]*delay[k];
			seg[k] += delay[k] * (r-l+1);  //総和クエリのときは区間幅を乗じる必要がある
			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, seg2[i] = val*val;
		while(i > 1){
			i /= 2;
			seg[i] = seg[i*2] + seg[i*2+1];
			seg2[i] = seg2[i*2] + seg2[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] = seg[k*2] +seg[k*2+1];
		seg2[k] = seg2[k*2] +seg2[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 0;
		if(a <= l && r <= b) return seg2[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 (lval + rval);
	}
	llint query(int a, int b)
	{
		return query(a, b, 1, 0, (1<<size)-1);
	}
};

llint n, Q;
llint a[200005];
LazySegTree seg(18);

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 t, l, r, x;
	for(int i = 1; i <= Q; i++){
		cin >> t >> l >> r;
		if(t == 1){
			cin >> x;
			seg.add(l, r, x);
		}else{
			cout << seg.query(l, r) << endl;
		}
	}
	
	return 0;
}
0