P2068 统计和(树状数组模板)
这是一道树状数组题
#include<iostream> using namespace std; const int maxn = 1e5 + 10; int tree[maxn], n, m; char h; int x, y; void add(int k, int num){ while (k <= n){ tree[k] += num; k += k&-k; } } int sum(int k){ int sum = 0; while (k){ sum += tree[k]; k -= k&-k; } return sum; } int main(){ cin >> n >> m; while (m--){ cin >> h >> x >> y; if (h == 'x')add(x, y); else cout << sum(y) - sum(x - 1) << endl; } }