您的位置:首页 > 其它

树状数组(插线问点)NYOJ123士兵杀敌(四)

2012-05-03 16:14 246 查看
题目:http://acm.nyist.net/JudgeOnline/problem.php?pid=123

这道题目可以用树状数组做,是很典型很裸的插线问点的题目,可能也可以用险段树写吧。

其中插线的过程可以这样理解:如果向[star,end]这个区间加value,就可以看做是向[o,end]加value,然后向[0,star-1]加-value。这样就实现了~~~~

题意很简单,一看就知道,以下是个人代码:

#include <stdio.h>
#include <iostream>
#include <string.h>
#define N 1000005
using namespace std;
int add
;
int n,m;

int lowbit(int k)
{
return k&(-k);
}
void update(int star,int value)
{
while(star>0)
{
add[star]+=value;
star-=lowbit(star);
}
}
int sum(int end)
{
int ans=0;
while(end<=m)
{
ans+=add[end];
end+=lowbit(end);
}
return ans;
}

int main()
{
memset(add,0,sizeof(add));
int i,star,end,que,value;
char ss[15];
scanf("%d%*c%d%*c",&n,&m);
for(i=0;i<n;i++)
{
scanf("%s%*c",ss);
if(ss[0]=='A')
{
scanf("%d%*c%d%*c%d%*c",&star,&end,&value);
update(end,value);
update(star-1,-value);
}
else
{
scanf("%d%*c",&que);
printf("%d\n",sum(que));
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: