一维树状数组BIT
2017-05-03 22:08
218 查看
#include<iostream> #include<cstdio> using namespace std; const int maxn = 107; int D[maxn]; int Bit[maxn]; int n; //Bit[i]中存储的是i和前头共(i&-i)个数字 //实现求sum和D[i]+=x的操作 int sum(int i) { int ret = 0; while (i) { ret += Bit[i]; i -= (i&-i); } return ret; } void add(int i, int x) { while (i <= n) { Bit[i] += x; i += (i&-i); } } void show() { cout << "show the awrry:" << endl; for (int i = 1; i <= n; i++) { if (i != 1) cout << " "; cout << D[i]; } cout << endl; } int main() { memset(Bit, 0, sizeof(Bit)); ios::sync_with_stdio(false); cout << "please input n" << endl; cin >> n; cout << "please input " << n << "nums" << endl; for (int i = 1; i <= n; i++) { cin >> D[i]; } for (int i = 1; i <= n; i++) { add(i, D[i]); } cout << "please input q" << endl; int q; cin >> q; cout << "please input q id and x" << endl; for (int i = 0; i<q; i++) { int id, x; cin >> id >> x; D[id] += x; add(id, x); } show(); cout << "please ask quesions:" << endl; while (true) { int x; cin >> x; if (x == -1) { break; } if (x <= n) cout << sum(x) << endl; } return 0; }
相关文章推荐
- 树状数组 BIT - 模板
- 树状数组(二叉索引树 BIT Fenwick树) *【一维基础模板】(查询区间和+修改更新)
- LA 4329 Ping Pong 2008 北京区域赛 H , BIT 二叉索引树(树状数组)的应用
- 【原创】【数据结构】一维树状数组的基本操作(单点修改,区间查询) (HDU1166 敌兵布阵)
- HDU1556 Color the ball(树状数组BIT 区间修改单点询问)
- 树状数组(BIT)
- 树状数组(BIT)
- 一维树状数组小结
- LA 4329 Ping Pong 2008 北京区域赛 H , BIT 二叉索引树(树状数组)的应用
- 学习笔记 BIT(树状数组)
- 一维二维树状数组区间加与区间求和
- poj 2352 一维树状数组
- hdu4000(一维树状数组模板)
- 树状数组 BIT
- LA 4329 Ping Pong 2008 北京区域赛 H , BIT 二叉索引树(树状数组)的应用
- BIT(树状数组简介)
- 一维与二维树状数组
- 树状数组BIT-乒乓比赛
- ZOJ2492 ping pong(树状数组BIT)
- 一维树状数组和二维树状数组