您的位置:首页 > 其它

敌兵布阵---hud1166(线段树或者树状数组模板)

2015-07-30 18:27 381 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166

线段树中对某一点的值进行改变;

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>

using namespace std;

#define N 50500
#define met(a, b) memset(a, b, sizeof(a))
int Tree
, n;

int lowbit(int x)
{
return x&(-x);
}

void Update(int pos, int num)
{
while(pos<=n)
{
Tree[pos]+=num;
pos += lowbit(pos);
}
}

int GetSum(int pos)
{
int s=0;
while(pos)
{
s += Tree[pos];
pos -= lowbit(pos);
}
return s;
}
int main()
{
int T, t=1;
scanf("%d", &T);
while(T--)
{
met(Tree, 0);
int num, pos, x, y;
scanf("%d", &n);
for(int i=1; i<=n; i++)
{
scanf("%d", &num);
Update(i, num);
}
printf("Case %d:\n", t++);
char s[110];
while(scanf("%s", s), s[0]!='E')
{
if(s[0]=='A')
{
scanf("%d %d", &pos, &num);
Update(pos, num);
}
if(s[0]=='S')
{
scanf("%d %d", &pos, &num);
Update(pos, -num);
}
if(s[0]=='Q')
{
scanf("%d %d", &x, &y);
int ans = GetSum(y)-GetSum(x-1);
printf("%d\n", ans);
}
}
}
return 0;
}


树状数组
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: