AcWing 243. 一个简单的整数问题2 分块暴力解法
原题链接
困难
作者:
皓首不倦
,
2020-10-08 03:20:08
,
所有人可见
,
阅读 475
'''
分块思想解决区间求和问题
'''
import sys
import math
n, m = map(int, input().split())
s = sys.stdin.readline()
arr = list(map(int, s.split()))
part_len = int(math.sqrt(n))
sum_list = [0] * n # 每一段的真实总和
bias = [0] * n # 每一段的偏置项
i = 0
while i <= n-1:
if i + part_len -1 <= n-1:
sum_list[i // part_len] = sum(arr[i:i+part_len])
i += part_len
for _ in range(m):
s = sys.stdin.readline()
if s[0] == 'Q':
l, r = map(int, s[2:].split())
l, r = l-1, r-1
i = l
ans = 0
while i <= r:
if i % part_len == 0 and i + part_len - 1 <= r:
ans += sum_list[i // part_len]
i += part_len
else:
ans += arr[i] + bias[i // part_len]
i += 1
print(ans)
else:
l, r, d = map(int, s[2:].split())
l, r = l-1, r-1
i = l
while i <= r:
if i % part_len == 0 and i + part_len -1 <= r:
sum_list[i // part_len] += d * part_len
bias[i // part_len] += d
i += part_len
else:
arr[i] += d
sum_list[i // part_len] += d
i += 1