AcWing 243. 一个简单的整数问题2
原题链接
简单
作者:
Lemmon_kk
,
2020-07-13 19:21:12
,
所有人可见
,
阅读 573
分块
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
const int N = 100100;
typedef long long LL;
int n, m, q, b;
LL a[N], base[N], res[N], pos[N];
void change(int l,int r,int x){
int idl = pos[l], idr = pos[r];
if(idl == idr){
for(int i = l;i <= r;i ++ ){
res[idl] += x;
a[i] += x;
}
}else{
for(int i = l;i < (idl + 1) * b;i ++ ){
a[i] += x;
res[idl] += x;
}
for(int i = idl + 1;i < idr;i ++ ){
base[i] += x;
res[i] += (LL)x * b;
}
for(int i = idr * b;i <= r;i ++ ){
a[i] += x;
res[idr] += x;
}
}
}
LL query(int l,int r){
int idl = pos[l], idr = pos[r];
LL ans = 0;
if(idl == idr){
for(int i = l;i <= r;i ++ ){
ans += base[idl] + a[i];
}
}
else{
for(int i = l;i < (idl + 1) * b;i ++ )
ans += base[idl] + a[i];
for(int i = idl + 1;i < idr;i ++ )
ans += res[i];
for(int i = idr * b;i <= r;i ++ )
ans += base[idr] + a[i];
}
return ans;
}
int main(){
scanf("%d%d", &n, &q);
b = sqrt(n);
for(int i = 1;i <= n;i ++ ){
scanf("%lld", &a[i]);
res[i / b] += a[i];
pos[i] = i / b;
}
char op;
int l, r, x;
while(q -- ){
cin >> op >> l >> r;
switch(op){
case 'Q':
cout << query(l, r) << endl;
break;
case 'C':
cin >> x;
change(l, r, x);
break;
}
}
return 0;
}