题目链接hdu1166

题目大意:RT,多次查询与更新。

解题思路:单点更新,区间查询,利用树状数组二分的特性将复杂度均摊到O(logn),注意cin会卡超时,用scanf稳过。


AC代码

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <string>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <vector>
#include <utility>

using namespace std;
typedef long long ll;
const int maxn = (int)5e4+5;

/*单点更新 区间查询*/

template<typename T>
inline T lowbit (T a) {
	return a & (-a);
}

int c[maxn],n;

void add (int idx, int num) {
	while (idx <= n) {
		c[idx] += num;
		idx += lowbit(idx);
	}
}

int get_sum (int idx) {
	int ans = 0;
	while (idx > 0) {
		ans += c[idx];
		idx -= lowbit(idx);
	}
	return ans;
}

int main() {
	ios::sync_with_stdio(false);
	int T,tmp;
	string command;
	cin >> T;
	for (int j = 1; j <= T; j++) {
		cin >> n;
		memset(c,0,sizeof(c));
		for (int i = 1; i <= n; i++) {
			cin >> tmp;
			add(i, tmp);
		}
		int idx,num;
		cout << "Case " << j << ":\n";
		while (cin >> command && command.at(0) != 'E') {
			cin >> idx >> num;
			switch(command.at(0)) {
			case 'Q':
				cout << get_sum(num) - get_sum(idx - 1) << '\n';
				break;
			case 'A':
				add(idx, num);
				break;
			case 'S':
				add(idx, -num);
				break;
			default:
				break;
			}
		}
	}
	return 0;
}