题解:
二维树状数组的板子题,,,学了这么久第一次写二维树状数组,惭愧啊。
怎么写就不说了,看代码吧。
跟普通的是一样的写法
#include<bits/stdc++.h>
using namespace std;
#define R register int
#define AC 302
#define lowbit(x) (x & (-x))
int n, m, k;
int a[AC][AC], c[AC][AC][AC]; inline int read()
{
int x = ;char c = getchar();
while(c > '' || c < '') c = getchar();
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
return x;
} inline void add(int x, int y, int w, int h)
{
for(R i = x; i <= n; i += lowbit(i))
for(R j = y; j <= m; j += lowbit(j))
c[i][j][w] += h;
} inline int search(int x, int y, int w)
{
int ans = ;
if(! x || !y || x > n || y > m) return ;
for(R i = x; i ; i -= lowbit(i))
for(R j = y; j ; j -= lowbit(j))
ans += c[i][j][w];
return ans;
} void pre()
{
n = read(), m = read();
for(R i = ; i <= n; i++)
for(R j = ; j <= m; j++)
{
a[i][j] = read();
add(i, j, a[i][j], );//建立树状数组
}
} void work()
{
int opt, x1, x2, x3, x4, x5, tmp;
k = read();
for(R i = ; i <= k; i++)
{
opt = read();
if(opt == )
{
x1 = read(), x2 = read(), x3 = read();
if(x3 != a[x1][x2])
{
add(x1, x2, a[x1][x2], -);//消除原有的值
add(x1, x2, x3, );//新增值
a[x1][x2] = x3;
}
}
else
{
x1 = read(), x3 = read(), x2 = read(), x4 = read(), x5 = read();
tmp = search(x3, x4, x5) - search(x3, x2 - , x5) - search(x1 - , x4, x5) + search(x1 - , x2 - , x5);//error,,,-1啊
printf("%d\n", tmp);
}
}
} int main()
{
// freopen("in.in", "r", stdin);
pre();
work();
// fclose(stdin);
return ;
}