首页 > 代码库 > 树状数组改点求段

树状数组改点求段

#include<stdio.h>int a[20],n;int lowbit(int x){    return x&(-x);}void add(int x,int c){    int i;    for(i=x; i<=n; i+=lowbit(i))a[i]+=c;}int sum(int x){    int s=0,i;    for(i=x; i; i-=lowbit(i))s+=a[i];    return s;}int main(){    scanf("%d",&n);    add(10,2);//把a[10]加2printf("%d",sum(n));//输入10则a[1]到a[10]的和为2(a初始全为0)    return 0;}