#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]加2 printf("%d",sum(n));//输入10则a[1]到a[10]的和为2(a初始全为0) return 0; }
原文地址:http://www.cnblogs.com/ganhang-acm/p/3859310.html