记录编号 574802 评测结果 WWWWWWWWWWWWWWWWWWWW
题目名称 数列操作C 最终得分 0
用户昵称 Gravatarlihaoze 是否通过 未通过
代码语言 C++ 运行时间 3.470 s
提交时间 2022-08-18 23:30:39 内存使用 9.17 MiB
显示代码纯文本
#include <bits/stdc++.h>

using i64 = long long;

const int N = 1e5+10;
int n, m, t;
int L[N], R[N], pos[N];
i64 a[N], s[N], add[N];

void change(int l, int r, i64 d) {
	int p = pos[l], q = pos[r];
	if (p == q) {
		for (int i = l; i <= r; ++ i) a[i] += d;
		s[p] += d * (r - l + 1);
	} else {
		for (int i = p + 1; i <= q - 1; ++ i) 
			add[i] += d;
		for (int i = l; i <= R[p]; ++ i) 
			a[i] += d;
		for (int i = L[q]; i <= r; ++ i) 
			a[i] += d;
		s[p] += d * (R[p] - l + 1);
		s[q] += d * (r - L[q] + 1);
	}
}

i64 ask(int l, int r) {
	int p = pos[l], q = pos[r];
	i64 ans = 0;
	if (p == q) {
		for (int i = l; i <= r; ++ i) 
			ans += a[i];
		ans += add[p] * (r - l + 1);
	} else {
		for (int i = p + 1; i <= q - 1; ++ i) 
			ans += s[i] + add[i] * (R[i] - L[i] + 1);
		for (int i = l; i <= R[p]; ++ i) 
			ans += a[i];
		for (int i = L[q]; i <= r; ++ i) 
			ans += a[i];
		ans += add[p] * (R[p] - l + 1);
		ans += add[q] * (r - L[q] + 1);
	}
	return ans;
}
		
int main() {
	freopen("shuliec.in", "r", stdin); 
	freopen("shuliec.out", "w", stdout);
	std::cin >> n;
	t = std::sqrt(n);
	for (int i = 1; i <= t; ++ i) 
		L[i] = (i - 1) * std::sqrt(n) + 1,
		R[i] = i * std::sqrt(n);
	if (R[t] < n) ++ t, L[t] = R[t - 1] + 1, R[t] = n;
	for (int i = 1; i <= t; ++ i) 
		for (int j = L[i]; j <= R[i]; ++ j) 
			pos[j] = i, s[i] += a[j];
	for (int i = 1; i <= n; ++ i) 
		std::cin >> a[i];
	std::cin >> m;
	while (m --) {
		char op[5];
		int x, y, z;
		std::cin >> op;
		if (op[0] == 'S') {
			std::cin >> x >> y;
			std::cout << ask(x, y) << '\n';
		} else {
			std::cin >> x >> y >> z;
			change(x, y, z);	
		}
	}
	return 0;
}