您的位置:首页 > 其它

HDU 1166 敌兵布阵

2013-10-22 10:32 260 查看


转载请注明出处:忆梦http://blog.csdn.net/yimeng2013/article/details/12942309

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1166

树状数组模板题

注意:输入输出比较多,用cin和cout会超时

#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;
#define N 50000+10
int c
, a
;
int n;
int lowbit(int x)
{
return x & -x;
}

int sum(int x)
{
int ret = 0;
while(x > 0)
{
ret += c[x];
x -= lowbit(x);
}
return ret;
}

void add(int x, int d)
{
while(x <= n)
{
c[x] += d;
x += lowbit(x);
}
}
int main ()
{
int T;
cin >> T;
for(int t = 1; t <= T; t++)
{
printf("Case %d:\n", t);

memset(c, 0, sizeof(c));
scanf("%d",&n);
for(int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
add(i, a[i]);
}

char op[10];
int aa, bb;
while(scanf("%s", op) != EOF)
{
if(op[0]=='E') break;
scanf("%d %d", &aa, &bb);
if(op[0] == 'Q')
{
printf("%d\n",sum(bb) - sum(aa-1) );
}
else if(op[0] == 'A')
{
add(aa,bb);
}
else if(op[0] == 'S')
{
add(aa,-bb);
}

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