树状数组再理解
这是c数组存的东西,c数组如何来的
如何求和!
前7项和:
7+6+4,怎么来的,一开始是7,7二进制是111,反码是000,补码是001,所以7-1=6,6的二进制是110,反码001,补码010,是2,所以是6-2=4,4的二进制是100,反码011,补码100,是4,所以4-4=0,至于这个过程,可以通过下面这段代码,只需为什么,看开头博客。
例题
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include<stdio.h>
#include<string.h>
#include<limits.h>
#include<stdbool.h>
#include<vector>
#include<queue>
#include<map>
using namespace std;
const int maxn=80023;
int n,m,t;
long long int c[maxn];
int lobit(int x)
{
return x&(-x);
}
void update(int pos,int k)
{
while(pos<=n)
{
c[pos]+=k;
pos+=lobit(pos);
}
}
int sum(int pos)
{
int res=0;
while(pos>0)
{
res+=c[pos];
pos-=lobit(pos);
}
return res;
}
int main()
{
int i,j,ce=0,a1,a2;
char ch[20];
scanf("%d",&t);
while(t--)
{
memset(c,0,sizeof(c));
ce++;
//printf("Case %d:\n",ce);
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&a1);
update(i,a1);
}
printf("Case %d:\n",ce);
while(1)
{
scanf("%s",ch);
if(ch[0]=='Q')
{
scanf("%d%d",&a1,&a2);
printf("%d\n",sum(a2)-sum(a1-1));
}
else if(ch[0]=='S')
{
scanf("%d%d",&a1,&a2);
update(a1,-a2);
}
else if(ch[0]=='A')
{
scanf("%d%d",&a1,&a2);
update(a1,a2);
}
else
{
break;
}
}
}
return 0;
}