题意
线段树,单点修改,区间查询
题解
略
调试记录
cmd判断错误
#include <cstdio>
#include <cstring>
#define maxn 50005
using namespace std;
struct Tree{
struct node{
int l, r, key, lazy;
}a[maxn << 2];
int val[maxn];
void clear(){
memset(a, 0, sizeof a);
memset(val, 0, sizeof val);
}
void Build(int cur, int l, int r){
a[cur].l = l, a[cur].r = r;
a[cur].lazy = 0;
if (l == r){
a[cur].key = val[l];
return;
}
int mid = (l + r) >> 1;
Build(cur << 1, l, mid);
Build(cur << 1 | 1, mid + 1, r);
a[cur].key = a[cur << 1].key + a[cur << 1 | 1].key;
}
void pushdown(int cur){
if (a[cur].lazy == 0) return;
a[cur << 1].lazy += a[cur].lazy;
a[cur << 1 | 1].lazy += a[cur].lazy;
a[cur << 1].key += a[cur].lazy * (a[cur << 1].r - a[cur << 1].l + 1);
a[cur << 1 | 1].key += a[cur].lazy * (a[cur << 1 | 1].r - a[cur << 1 | 1].l + 1);
a[cur].lazy = 0;
}
void update(int cur, int l, int r, int k){
if (a[cur].l > r || a[cur].r < l) return;
if (a[cur].l >= l && a[cur].r <= r){
a[cur].key += (a[cur].r - a[cur].l + 1) * k;
a[cur].lazy += k;
return;
}
update(cur << 1, l, r, k);
update(cur << 1 | 1, l, r, k);
a[cur].key = a[cur << 1].key + a[cur << 1 | 1].key;
}
int Query(int cur, int l, int r){
if (a[cur].l > r || a[cur].r < l) return 0;
if (a[cur].l >= l && a[cur].r <= r) return a[cur].key;
pushdown(cur);
return Query(cur << 1, l, r) + Query(cur << 1 | 1, l, r);
}
}t;
int T, n;
int main(){
scanf("%d", &T);
for (int ti = 1; ti <= T; ti++){
printf("Case %d:\n", ti);
t.clear();
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &t.val[i]);
t.Build(1, 1, n);
char cmd[50];
scanf("%s", cmd);
while (cmd[0] != 'E'){
int x, y;
scanf("%d%d", &x, &y);
if (cmd[0] == 'Q') printf("%d\n", t.Query(1, x, y));
if (cmd[0] == 'A') t.update(1, x, x, y);
if (cmd[0] == 'S') t.update(1, x, x, -y);
scanf("%s", cmd);
}
}
return 0;
}