士兵杀敌(四)
http://acm.nyist.net/JudgeOnline/problem.php?pid=123
树状数组的第二个用法:插线问点
#include"stdio.h"
#define max 1000003
int c[max],m;
int lowbit(int k)
{
return k&(-k);
}
void add(int k,int he)//前k项都增加he
{
while(k>0)
{
c[k]+=he;
k-=lowbit(k);
}
}
void Q(int k)//查询
{
int query=0;
while(k<=m)
{
query+=c[k];
k+=lowbit(k);
}
printf("%d\n",query);
}
int main()
{
int t,from,to,he;
char ch[6];
scanf("%d%d",&t,&m);
while(t--)
{
scanf("%s",ch);
if(ch[0]=='A')
{
scanf("%d%d%d",&from,&to,&he);
add(from-1,-he);
add(to,he);
}
else if(ch[0]=='Q')
{
scanf("%d",&from);
Q(from);
}
}
return 0;
}