《树状数组》hdu acm 5.3.1 二维

#include<iostream>
#include<cstring>
using namespace std;
#define N 1005
int map[N][N]; int sum(int x, int y) { int ret = 0; int i, j; for (i = x;i>0;i -= (i&-i)) for (j = y;j>0;j -= (j&-j))
			ret += map[i][j]; return ret; } void add(int x, int y, int d) { int i, j; for (i = x;i<N;i += (i&-i)) for (j = y;j<N;j += (j&-j))
			map[i][j] += d; } int main() { int t, n, a, b, c, d, e, i, j, k, v; char s[5];
	scanf("%d", &t); for (k = 1;k <= t;++k) {
		scanf("%d", &n);
		printf("Case %d:\n", k); for (i = 1;i<N;i++) { for (j = 1;j < N;j++) {
				map[i][j] = 0; } } for (i = 1;i<N;i++) { for (j = 1;j<N;j++) {
				add(i, j, 1); } } while (n--) {
			scanf("%s", s); if (s[0] == 'S') {
				scanf("%d%d%d%d", &a, &b, &c, &d); if (a<c) swap(a, c); if (b<d) swap(b, d);
				printf("%d\n", sum(a + 1, b + 1) - sum(a + 1, d) - sum(c, b + 1) + sum(c, d)); } else if (s[0] == 'A') {
				scanf("%d%d%d", &a, &b, &c);
				add(a + 1, b + 1, c); } else if (s[0] == 'D') {
				scanf("%d%d%d", &a, &b, &c);
				v = sum(a + 1, b + 1) - sum(a, b + 1) - sum(a + 1, b) + sum(a, b);
				c = v<c ? v : c;
				add(a + 1, b + 1, -c); } else {
				scanf("%d%d%d%d%d", &a, &b, &c, &d, &e);
				v = sum(a + 1, b + 1) - sum(a, b + 1) - sum(a + 1, b) + sum(a, b);
				e = v<e ? v : e;
				add(a + 1, b + 1, -e);
				add(c + 1, d + 1, e); } } } return 0; }

你可能感兴趣的:(《树状数组》hdu acm 5.3.1 二维)