NYIST 116 士兵杀敌(二)
时间:2015-01-29 22:34:02
收藏:0
阅读:209
链接:click here
题意:
南将军手下有N个士兵,分别编号1到N,这些士兵的杀敌数都是已知的。
小工是南将军手下的军师,南将军经常想知道第m号到第n号士兵的总杀敌数,请你帮助小工来回答南将军吧。
南将军的某次询问之后士兵i可能又杀敌q人,之后南将军再询问的时候,需要考虑到新增的杀敌数。
思路:RMQ 入门代码:
#include <stdio.h> //RMQ
#include <string.h>
#include <stdlib.h>
int a[1000005];
int N,M;
#define lowbit(x) ((x)&(-x))
void update(int i,int x)
{
while(i<=N)
{
a[i]+=x;
i+=lowbit(i);
}
}
int sum(int n)
{
int sum=0;
while(n>0)
{
sum+=a[n];
n-=lowbit(n);
}
return sum;
}
int main()
{
int i,j,pre,last,t;
char str[10];
scanf("%d%d",&N,&M);
for(i=1; i<=N; i++)
{
scanf("%d",&t);
update(i,t);
}
for(i=0; i<M; i++)
{
scanf("%s%d%d",str,&pre,&last);
if(str[0]=='Q')
printf("%d\n",sum(last)-sum(pre-1));
else
update(pre,last);
}
return 0;
}
评论(0)