結果

問題 No.1099 Range Square Sum
ユーザー heno239heno239
提出日時 2020-06-26 21:45:13
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 200 ms / 2,000 ms
コード長 3,073 bytes
コンパイル時間 1,209 ms
コンパイル使用メモリ 116,512 KB
実行使用メモリ 18,488 KB
最終ジャッジ日時 2023-09-18 03:53:27
合計ジャッジ時間 5,002 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 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,380 KB
testcase_13 AC 3 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 3 ms
4,384 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 3 ms
4,376 KB
testcase_21 AC 199 ms
18,360 KB
testcase_22 AC 197 ms
18,472 KB
testcase_23 AC 194 ms
18,408 KB
testcase_24 AC 200 ms
18,488 KB
testcase_25 AC 197 ms
18,488 KB
testcase_26 AC 158 ms
18,472 KB
testcase_27 AC 160 ms
18,352 KB
testcase_28 AC 157 ms
18,348 KB
testcase_29 AC 158 ms
18,476 KB
testcase_30 AC 158 ms
18,404 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 998244353;
const ll INF = mod * mod;
typedef pair<int, int>P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-8;
const ld pi = acosl(-1.0);

struct SegT {
private:
	int n; vector<ll> node, lazy;
	vector<ll> node2;
public:
	SegT(vector<ll> a) {
		int sz = a.size();
		n = 1;
		while (n < sz)n <<= 1;
		node.resize(2 * n - 1, 0);
		node2.resize(2 * n - 1, 0);
		lazy.resize(2 * n - 1, 0);
		rep(i, sz) {
			node[i + n - 1] = a[i];
		}
		per(i, n - 1) {
			node[i] = node[2 * i + 1] + node[2 * i + 2];
			node2[i] = node2[2 * i + 1] + node2[2 * i + 2] + node[2 * i + 1] * node[2 * i + 2];
		}
	}
	void eval(int k, int l, int r) {
		ll len = r - l;
		node2[k] += (len - 1)*node[k]*lazy[k];
		node2[k] += len*(len - 1) / 2 * lazy[k] * lazy[k];
		node[k] += len*lazy[k];
		if (r - l > 1) {
			lazy[2 * k + 1] += lazy[k];
			lazy[2 * k + 2] += lazy[k];
		}
		lazy[k] = 0;
	}
	void add(ll x, 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;
		if (a <= l && r <= b) {
			lazy[k] += x; eval(k, l, r);
		}
		else {
			add(x, a, b, k * 2 + 1, l, (l + r) / 2);
			add(x, a, b, k * 2 + 2, (l + r) / 2, r);
			node[k] = node[2 * k + 1] + node[2 * k + 2];
			node2[k] = node2[2 * k + 1] + node2[2 * k + 2] + node[2 * k + 1] * node[2 * k + 2];
		}
	}
	LP 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{ 0,0 };
		if (a <= l && r <= b)return{ node[k],node2[k] };
		else {
			LP vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
			LP vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
			return{ vl.first + vr.first,vl.second + vr.second + vl.first*vr.first };
		}
	}
};


void solve() {
	int n; cin >> n;
	vector<ll> a(n);
	rep(i, n)cin >> a[i];
	SegT st(a);
	int q; cin >> q;
	rep(i, q) {
		int t, l, r; cin >> t >> l >> r; l--; r--;
		if (t == 1) {
			int x; cin >> x;
			st.add(x, l, r + 1);
		}
		else {
			LP p = st.query(l, r + 1);
			ll ans = p.first*p.first - 2 * p.second;
			cout << ans << "\n";
		}
	}
}


signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(7);
	//init_f();
	//init();
	//int t; cin >> t; rep(i, t)
	solve();
	stop
		return 0;
}
0