<strong><span style="font-size:18px;">#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int a[50005];
int tree[50005];//树状数组
char s[20];
int n;
void add(int k, int num)
{
while(k <= n)
{
tree[k] += num;
k += k & (-k);//lowbit(k)
}
}
int search(int k)
{
int sum=0;
while(k)
{
sum += tree[k];
k -= k & (-k);
}
return sum;
}
int main()
{
int cases, i, j, x, y;
j = 1;
cin>>cases;
while(cases--)
{
memset(tree, 0, sizeof(tree));
printf("Case %d:\n",j++);
scanf("%d", &n);
for(i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
add(i, a[i]);
}
while(scanf("%s", s) != EOF)
{
if(strcmp(s, "End") == 0)
{
break;
}
else if(strcmp(s, "Query") == 0)
{
scanf("%d%d", &x, &y);
int ans = search(y) - search(x-1);
cout<<ans<<endl;
}
else if(strcmp(s, "Add") == 0)
{
scanf("%d%d", &x, &y);
add(x, y);
}
else
{
scanf("%d%d", &x, &y);
add(x, -y);
}
}
}
return 0;
}
</span></strong>
树状数组模板 HDU1166
最新推荐文章于 2017-04-16 12:37:15 发布