更新语句改为update(x, y - query(x, x));即可。
/*0.259s*/
const int mx = 200005;
int N, tree[mx];
void update(int p, int m)
{
while (p <= N)
{
tree[p] += m;
p += p & -p;
}
}
int sum(int n)
{
int sum = 0;
while (n)
{
sum += tree[n];
n -= n & -n;
}
return sum;
}
int query(int sta, int end) {return sum(end) - sum(sta - 1);}
#include<cstdio>
#include<cstring>
int main()
{
//freopen("in.txt", "r", stdin);
char ch, str[10];
int i, x, y, tmpx, cas = 0;
bool ok = false;
while (scanf("%d", &N), N)
{
if (ok) putchar(10);
else ok = true;
printf("Case %d:\n", ++cas);
memset(tree, 0, sizeof(tree));
for (i = 1; i <= N; ++i)
{
scanf("%d", &x);
update(i, x);
}
getchar();
while ((ch = getchar()) != 'E')
{
scanf("%d%d", &x, &y);
if (ch == 'S')
{
tmpx = query(x, x);
update(x, y - tmpx);
}
else printf("%d\n", query(x, y));
getchar();
}
gets(str);
}
return 0;
}

本文介绍了一种使用树状数组进行区间更新与查询的优化方法,通过具体的代码实现展示了如何高效地处理一系列更新与查询操作。这种方法特别适用于需要频繁进行区间更新和查询的问题场景。
236

被折叠的 条评论
为什么被折叠?



