樹狀數組_ACM模板

樹狀數組

線段樹的分支

const int maxn = 50010;
int tree[maxn], n;
inline int lowbit(int x) {
	return x & (-x);
}
void add(int x, int val) {
	for (int i = x; i <= maxn; i += lowbit(i))
		tree[i] += val;
}
int get(int x) {
	int sum = 0;
	for (int i = x; i; i -= lowbit(i))
		sum += tree[i];
	return sum;
}
int main() {
	ios::sync_with_stdio(false);
	int T;
	cin >> T;
	string s;
	int x, y;
	for (int cas = 1; cas <= T; cas++) {
		cout << "Case " << cas << ":" << endl;
		memset(tree, 0, sizeof(tree));
		cin >> n;
		int in;
		for (int i = 1; i <= n; i++)
			cin >> in, add(i, in);
		while (cin >> s) {
			if (s == "End")
				break;
			else if (s == "Query") {
				cin >> x >> y;
				cout << get(y) - get(x - 1) << endl;
			}
			else if (s == "Add") {
				cin >> x >> y;
				add(x, y);
			}
			else if (s == "Sub") {
				cin >> x >> y;
				add(x, -y);
			}
		}
	}
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章