bzoj1452 [JSOI2009]Count

Description

这里写图片描述

Input

这里写图片描述

Output

这里写图片描述

Sample Input

这里写图片描述

Sample Output

1
2

HINT

这里写图片描述

Solution

二维树状数组傻逼题,这里写一下,方便以后抄板,加深理解。

#include<bits/stdc++.h>
using namespace std;

inline int read() {
	int x = 0, flag = 1; char ch = getchar();
	while (ch > '9' || ch < '0') { if (ch == '-') flag = -1; ch = getchar(); }
	while (ch <= '9' && ch >= '0') { x = x * 10 + ch - '0'; ch = getchar(); }
	return x * flag;
}
inline void write(int x) { if (x >= 10) write(x / 10); putchar(x % 10 + '0'); }

#define N 305
#define rep(i, a, b) for (int i = a; i <= b; i++)

int n, m;
int bit[N / 3][N][N];
int g[N][N];

inline int lowbit(int k) { return (k & (-k)); }

void add(int x, int y, int num, int v) {
	for (int i = x; i <= n; i += lowbit(i)) for (int j = y; j <= m; j += lowbit(j))
		bit[num][i][j] += v;
}

int ask(int x, int y, int num) {
	int ans = 0;
	for (int i = x; i; i -= lowbit(i)) for (int j = y; j; j -= lowbit(j)) ans += bit[num][i][j];
	return ans;
}

int main() {
	cin >> n >> m;
	rep(i, 1, n) rep(j, 1, m) g[i][j] = read(), add(i, j, g[i][j], 1);
	int q = read();
	while (q--) {
		int t = read();
		if (t == 1) {
			int x = read(), y = read(), c = read();
			add(x, y, g[x][y], -1); g[x][y] = c; add(x, y, c, 1);
		}
		else {
			int x1 = read(), x2 = read(), y1 = read(), y2 = read(), c = read();
			int ans = ask(x2, y2, c) - ask(x1 - 1, y2, c) - ask(x2, y1 - 1, c)
				+ ask(x1 - 1, y1 - 1, c);
			write(ans), puts("");
		}
	}
	return 0;
}
posted @ 2018-02-05 09:32  aziint  阅读(117)  评论(0编辑  收藏  举报
Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.