题目链接:http://wikioi.com/problem/1080/
算法与思路:树状数组,虽然这题题目是线段树练习,但是明显有单点更新,区间求和的特征,
所以树状数组也能做,至于思路就不赘述了,不懂树状数组可以看我前面几篇相关的题解。
#include<stdio.h>
#include<string.h>
int n, m;
int a[100010];
int lowbit(int x)
{
return x & (-x);
}
void update(int pos, int num)
{
while(pos <= n)
{
a[pos] += num;
pos += lowbit(pos);
}
}
int sum(int x)
{
int sum = 0;
while(x > 0)
{
sum += a[x];
x -= lowbit(x);
}
return sum;
}
int main()
{
int i, j, temp;
int que, x, y;
scanf("%d", &n);
memset(a, 0, sizeof(a));
for(i = 1; i <= n; i++)
{
scanf("%d", &temp);
update(i, temp);
}
scanf("%d", &m);
for(i = 1; i <= m; i++)
{
scanf("%d %d %d", &que, &x, & y);
if(que == 1)
update(x, y);
else
printf("%d\n", sum(y) - sum(x - 1));
}
return 0;
}