树状数组 模板
2011-03-18 13:17
218 查看
#include<iostream> #include<string.h> #include<string> #include<stdio.h> using namespace std; template<class ValueType,int MaxSize> class TreeArray{ public: TreeArray():size(MaxSize){} int low(int n){return n&(-n);} void clear() { memset(ta,0,sizeof(ta)); } void add(int pos,ValueType v) { while(pos<size){ ta[pos]+=v; pos+=low(pos);//用来的到下一个包含的位置 } } ValueType sum(int pos) { ValueType v=0; while(pos>0){ v+=ta[pos]; pos-=low(pos); } return v; } private: ValueType ta[MaxSize]; int size; }; TreeArray<int,1000010> tr; int main() { int v; int n,m;cin>>n>>m; for(int i=1;i<=n;i++){ scanf("%d",&v); tr.add(i,v); } string s; int x,y; while(m--){ cin>>s; scanf("%d %d",&x,&y); if(s=="ADD"){ tr.add(x,y); }else{ cout<<tr.sum(y)-tr.sum(x-1)<<endl; } } return 0; }
相关文章推荐
- hdu 3584 (三维树状数组模板 )
- 【模板】树状数组 2
- 树状数组的讲解感觉挺好,和我的一维,二维,树状数组的模板,希望对大家有用
- 树状数组的模板;
- 树状数组模板
- 模板,树状数组
- poj 3468树状数组(模板)
- 树状数组模板
- ACM_模板_树状数组
- 模板-树状数组
- 【树状数组】树状数组一维二维模板
- [模板]-树状数组③-区间修改+区间查询
- 模板:树状数组
- 敌兵布阵 树状数组模板
- 二维树状数组模板
- 树状数组模板
- 求逆序数模板(树状数组+离散化 || 归并排序法)
- NYOJ-117/atcoder ARC 75-E - Meaningful Mean 求逆序数。 树状数组存模板!(原模板已更正)
- 树状数组模板
- 比较快也比较漂亮的熟练剖分模板(BZOJ 1036:树剖+线段树 BZOJ 1103:树剖+树状数组)