寒假集训之树状数组
2018-01-13 09:26
225 查看
自学链接
模板题:HDU1166
模板题:HDU1166
#include <bits/stdc++.h> using namespace std; const int maxn=1000000; int tree[maxn+10]; int a[maxn+10]; int n; inline int lowbit(int x) { return (x&-x); } void add(int x,int value) { for(int i=x;i<=n;i+=lowbit(i)) tree[i]+=value; } long long get(int x) { long long sum=0; for(int i=x;i;i-=lowbit(i)) sum+=tree[i]; return sum; } int main() { int t;cin>>t; int tmp=t; while(t--) { printf("Case %d:\n",tmp-t); scanf("%d",&n); memset(tree,0,sizeof tree); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); add(i,a[i]); } char s[10]; int i,j; while(scanf("%s",s)&&strcmp(s,"End")!=0) { scanf("%d%d",&i,&j); if(strcmp(s,"Query")==0) printf("%lld\n",get(j)-get(i-1));//查询区间之和用减法就好 else if(strcmp(s,"Add")==0) add(i,j); else if(strcmp(s,"Sub")==0) add(i,-1*j);//某个点减少了,用加负数 } } return 0; }
相关文章推荐
- 暑假集训日记--8.19--树状数组
- ACM集训——POJ2352---一维树状数组的应用
- 2017暑期集训Day 25 树状数组
- UOJ276 [清华集训2016] 汽水 【二分答案】【点分治】【树状数组】
- 暑假集训日记--8.16--练习赛题+树状数组
- 2018年全国多校算法寒假训练营练习比赛(第五场) Big Water Problem(树状数组)
- 集训第二十二天(2017/8/21):树状数组刷题
- [ 后缀自动机 树上启发式合并 线段树 树状数组 ] [ 雅礼集训 2017 Day7 ] LOJ#6041
- 陕西省集训之树状数组
- 集训第十六天(2017/8/15):树状数组
- 集训第二十三天(2017/8/22):树状数组和团队练习赛
- 2018年全国多校算法寒假训练营练习比赛(第五场) (树状数组模板)
- 【NOIP2017提高A组集训10.25】天才绅士少女助手克里斯蒂娜(树状数组)
- 树状数组模版
- Educational Codeforces Round 8(E. Zbazi in Zeydabad(树状数组优化))★ ★
- Codeforces 396C On Changing Tree(树状数组)
- Cow Sorting (树状数组)
- 【逆序对】【线段树】【树状数组】Data Structure Special Training 1 T1 rotinv 题解
- hdoj-1166-敌兵布阵(树状数组)
- 寒假训练1—H(第一次写出结构体数组)