黑猫的ACM模板

来源:互联网 发布:sqlserver最新版本 编辑:程序博客网 时间:2024/05/16 18:55
/** *树状数组 ( Binary Indexed Tree) *时间复杂度:log(n) *数组下标必须从 1 开始! *Max为范围 *Inssert(int pos,int val):     pos位置增加val *Sum(int pos)                  计算从 1 ~ pos 所有元素的和 */ #include<bit/stdc++.h> int Max long long c[Max]; int lowbit(int x) {     return x&(-1*x); } void Insert(int pos,int val) {     while(pos<=Max)     {         c[Max]+=val;         pos+=lowbit(pos);     } } long long Sum(int pos) {     long long ans = 0;     while(pos>0)     {         ans+=c[pos];         pos-=lowbit(pos);     }     return ans; }

如有问题请联系QQ:826409960

原创粉丝点击