树状数组laekov
2017-01-23 15:34
127 查看
lowbit
数组的第 i 位存储的是以 i 为结尾的长度为lowbit(i) 的一段的和.![](https://images2015.cnblogs.com/blog/1008672/201701/1008672-20170123154125300-934531846.png)
int lowBit(x) { return x & -x; }
加点
int n, bt[maxn]; void btAdd(int pos, int delta) { for (; pos <= n; pos += lowBit(p)) { bt[pos] += delta; } }
查询
int btSum(int pos) { int ans = 0; for (; pos; pos -= lowBit(p)) { ans += bt[p]; } return ans; }
完整代码
略有不同的,dad曾经教给我,树状数组这么写//线段树练习1 #include<iostream> #include<cstdio> #include<cstring> #define maxn 100010 using namespace std; int n,m,t[maxn]; void add(int k,int z) { while(k<=n) { t[k]+=z; k+=k&(-k); } } int find(int k) { int ans=0; while(k) { ans+=t[k]; k-=k&(-k); } return ans; } int main() { int i,j,k,x; scanf("%d",&n); for(i=1;i<=n;i++) { scanf("%d",&x); add(i,x); } scanf("%d",&m); for(i=1;i<=m;i++) { int x,y,z,w; scanf("%d",&w); scanf("%d%d",&x,&y); if(w==1) add(x,y); else if(w==2) printf("%d\n",find(y)-find(x-1)); } return 0; }
//线段树练习2 #include<iostream> using namespace std; int t[100010],n,m; void add(int k,int z) { while(k<=n) { t[k]+=z; k+=k&(-k); } } int find(int a) { int ans=0; while(a) { ans+=t[a]; a-=a&(-a); } return ans; } int main() { int s; cin>>n; for(int i=1;i<=n;i++) cin>>s, add(i,s); cin>>m; for(int i=1;i<=m;i++) { int x,y,w,z; cin>>w; if(w==2) { cin>>x; cout<<find(x)-find(x-1)<<endl; } if(w==1) { cin>>x>>y>>z; for(int j=x;j<=y;j++) add(j,z); } } }
相关文章推荐
- 树状数组laekov
- 树状数组
- POJ 2352 HDU1541 Stars(树状数组)
- HDU5122 K.Bro Sorting 树状数组
- 【树状数组】 ACdream 1127 Base Station
- pku 2481 Cows(树状数组)
- cf Babaei and Birthday Cake(树状数组解决LIS问题)
- CodeForces - 369E Valera and Queries(树状数组)
- 树状数组专题(六)hdu1556
- 利用差分实现的树状数组区间修改 区间求和
- poj 2029 二维树状数组入门
- HDU 4046 Panda [树状数组]【数据结构】
- [NOIP模拟题][树状数组][线段树]
- hdu1541 Stars (树状数组)
- POJ1195 Mobile phones(简单二维树状数组)
- bzoj 4411 USACO16FEB 负载平衡Load Balancing(扫描线,树状数组,三分答案,前缀和)
- HDU 1166 敌兵布阵 (树状数组)
- 树状数组求逆序对数板子
- poj 1195 二维树状数组应用实例
- POJ2352 Stars 【树状数组】or【线段树】