树状数组求条
查看原帖
树状数组求条
255169
__LePetitPrince__楼主2023/9/2 22:49

应该不是 long long 的问题

#include <iostream>
#include <cstdio>
using namespace std;
const int S = 1e5 + 5;
long long n, q; 
long long t[S];
long long lowbit(long long x) {
	return x & (-x);
}
void update(long long x, long long a) {
	for ( ; x <= n; x += lowbit(x)) {
		t[x] += a;
	}
}
long long sum(long long x) {
	int ret = 0;
	for ( ; x; x -= lowbit(x)) {
		ret += t[x];
	}
	return ret;
}
int main() {
	cin >> n >> q;
	char c;
	long long a, b;
	while (q--) {
//		scanf("%c%d%d", &c, &a, &b);
		cin >> c >> a >> b;
		if (c == 'x') {
			update(a, b);
		} else if (c == 'y') {
			cout << sum(b) - sum(a - 1) << endl;
		}
	}
	return 0;
} 
2023/9/2 22:49
加载中...