标签:
南将军手下有N个士兵,分别编号1到N,这些士兵的杀敌数都是已知的。
小工是南将军手下的军师,南将军经常想知道第m号到第n号士兵的总杀敌数,请你帮助小工来回答南将军吧。
南将军的某次询问之后士兵i可能又杀敌q人,之后南将军再询问的时候,需要考虑到新增的杀敌数。
5 6 1 2 3 4 5 QUERY 1 3 ADD 1 2 QUERY 1 3 ADD 2 3 QUERY 1 2 QUERY 1 5
6 8 8 20
好几天没刷题了 水一道先
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#define MAX 1000100
using namespace std;
int n,m;
int sum[MAX<<2];
void pushup(int o)
{
sum[o]=sum[o<<1]+sum[o<<1|1];
}
void gettree(int o,int l,int r)
{
if(l==r)
{
scanf("%d",&sum[o]);
return ;
}
int mid=(l+r)>>1;
gettree(o<<1,l,mid);
gettree(o<<1|1,mid+1,r);
pushup(o);
}
void update(int o,int l,int r,int pos,int val)
{
if(l==r)
{
sum[o]+=val;
return ;
}
int mid=(l+r)>>1;
if(pos<=mid)
update(o<<1,l,mid,pos,val);
else
update(o<<1|1,mid+1,r,pos,val);
pushup(o);
}
int find(int o,int l,int r,int L,int R)
{
if(L<=l&&R>=r)
return sum[o];
int mid=(l+r)>>1;
int ans=0;
if(R<=mid)
ans+=find(o<<1,l,mid,L,R);
else if(L>mid)
ans+=find(o<<1|1,mid+1,r,L,R);
else
{
ans+=find(o<<1,l,mid,L,mid);
ans+=find(o<<1|1,mid+1,r,mid+1,R);
}
return ans;
}
int main()
{
int n,m,a,b;
char op[20];
scanf("%d%d",&n,&m);
gettree(1,1,n);
while(m--)
{
scanf("%s",op);
scanf("%d%d",&a,&b);
if(op[0]==‘Q‘)
printf("%d\n",find(1,1,n,a,b));
else
update(1,1,n,a,b);
}
return 0;
}
标签:
原文地址:http://www.cnblogs.com/tonghao/p/4838094.html