清点人数

题目描述

思路

用一个数组记录lowbit的值

代码

#include <stdio.h>
#include <string.h>
int n, k;
char st;
int arr[500005], lowbit[500005];

void update(int x, int y) {
	while (x <= n) {
		arr[x] += y;
		x += lowbit[x];
	}
}
int sum(int x) {
	int res = 0;
	while (x) {
		res += arr[x];
		x -= lowbit[x];
	}
	return res;
}
int main() {
	scanf("%d %d\n", &n, &k);
	for (int i = 1; i <= n; ++i) {
		lowbit[i] = i & (-i);
	}
	for (int i = 1, a, b; i <= k; ++i) {
		st = getchar();
		// putchar(st);
		if (st == 'A') {
			scanf("%d\n", &a);
			printf("%d\n", sum(a));
		} else if (st == 'B') {
			scanf("%d %d\n", &a, &b);
			update(a, b);
		} else {
			scanf("%d %d\n", &a, &b);
			update(a, -b);
		}
	}
	return 0;
}
posted @ 2019-09-08 21:02  cabbage-leaf  阅读(197)  评论(0编辑  收藏  举报