【树状数组】HDU 1166

真是个入门题啊…… 直接考应用的

注意两点:

1,HDU又卡输入输出,老手段了,习惯写scanf吧

2,减去b其实就是加上-b  要有计算机人的思想(。。)

只改了输入输出,但是在差不多1e9的数据下面(?)快了不止一倍啊。

//离散化啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊
//不难的啊吧啊啊啊啊啊啊啊啊啊啊啊啊
#include<iostream>
#include<string>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
#define maxn 50005
ll bit[50005];//记得memset哦
ll a[50005];
ll lowbit(ll x) {
	return x & (-x);
}

void  add(ll k, ll num,ll n) {
	// k是在哪里加 num 是加了多少 n是限度吧
	//k是往上加..  所以是+lowbit(k);
	while (k <= n) {
		bit[k] += num;
		k += lowbit(k);
	}
}

ll  getsum(ll k) {
	ll sum = 0;
	// 这个是减的.. 
	while (k> 0) {
		sum += bit[k];
		k = k - lowbit(k);
		//把零碎都减完 ,就想象7是111,然后先是7自己,然后是110(6) 然后是100(4)
	}return sum;
}
int main()
{
	int kase = 0;
	int t; scanf("%d", &t); while (t--) {
		int  n; scanf("%d", &n);// cin >> n;
		memset(bit, 0, sizeof(bit));
		for (int i = 1; i <= n; i++) {
			scanf("%d",&a[i]);//cin >> a[i]; 
			add(i, a[i], n);
		}
		string s; ll a, b;
		printf("Case %d:\n", ++kase);
//		cout << "Case " << ++kase << ":"<<endl;
		while (cin >> s) {
			if (s[0] != 'E') {
				cin >> a >> b;
			}
			else break;
			if (s[0] == 'A') add(a, b, n);
			else if (s[0] == 'S') {
				b = 0 - b;
				add(a, b, n); }
			else if (s[0] = 'Q') {
				int x = getsum(b) - getsum(a - 1); 
				printf("%d\n", x);
			}
		}

	}

	return 0;
}

猜你喜欢

转载自blog.csdn.net/StrongerIrene/article/details/81320353
今日推荐