士兵杀敌四 树状数组之插点问线

#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;
}

你可能感兴趣的:(士兵杀敌四 树状数组之插点问线)