题意:先初始化一个全0矩阵,两种更新操作:某个子矩阵的元素全部增加v,和某个子矩阵的元素全部设为v,一种查询:某个子矩阵的元素和,最大值,最小值。
矩阵最多20行,建20棵线段树。然后就是成段更新的基本操作了,需要注意的是set操作比add操作的优先级要高,即在set的时候该区间节点的add域一定设为0,反过来却不需要。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define lson(x) ((x) << 1)
#define rson(x) ((x) << 1) | 1
using namespace std;
const int maxn = 1000000 + 10;
struct node
{
int l, r;
int minv, maxv;
int sum, add, set;
}tree[25][2 * maxn];
int r, c, m;
void pushup(int now, int col)
{
int ln = lson(now), rn = rson(now), l = tree[col][now].l, r = tree[col][now].r;
if(l < r)
{
tree[col][now].minv = min(tree[col][ln].minv, tree[col][rn].minv);
tree[col][now].maxv = max(tree[col][ln].maxv, tree[col][rn].maxv);
tree[col][now].sum = tree[col][ln].sum + tree[col][rn].sum;
}
}
void pushdown(int now, int col)
{
int ln = lson(now), rn = rson(now), l = tree[col][now].l, r = tree[col][now].r;
int mid = (l + r) >> 1;
if(tree[col][now].set != -1)
{
tree[col][ln].set = tree[col][ln].minv = tree[col][ln].maxv = tree[col][now].set;
tree[col][rn].set = tree[col][rn].minv = tree[col][rn].maxv = tree[col][now].set;
tree[col][ln].sum = (mid - l + 1) * tree[col][now].set; tree[col][rn].sum = (r - mid) * tree[col][now].set;
tree[col][ln].add = tree[col][rn].add = 0;
tree[col][now].set = -1;
}
tree[col][ln].add += tree[col][now].add; tree[col][ln].minv += tree[col][now].add; tree[col][ln].maxv += tree[col][now].add;
tree[col][rn].add += tree[col][now].add; tree[col][rn].minv += tree[col][now].add; tree[col][rn].maxv += tree[col][now].add;
tree[col][ln].sum += (mid - l + 1) * tree[col][now].add; tree[col][rn].sum += (r - mid) * tree[col][now].add;
tree[col][now].add = 0;
}
void build(int l, int r, int now, int col)
{
tree[col][now].l = l; tree[col][now].r = r; tree[col][now].add = 0; tree[col][now].set = -1;
if(l == r)
{
tree[col][now].minv = tree[col][now].maxv = tree[col][now].sum = 0;
}
else
{
int mid = (l + r) >> 1;
build(l, mid, lson(now), col);
build(mid + 1, r, rson(now), col);
pushup(now, col);
}
}
void update1(int l, int r, int now, int col, int v)
{
if(l <= tree[col][now].l && r >= tree[col][now].r)
{
tree[col][now].minv += v; tree[col][now].maxv += v;
tree[col][now].sum += (tree[col][now].r - tree[col][now].l + 1) * v;
tree[col][now].add += v;
}
else
{
pushdown(now, col);
int mid = (tree[col][now].l + tree[col][now].r) >> 1;
if(r <= mid) update1(l, r, lson(now), col, v);
else if(l > mid) update1(l, r, rson(now), col, v);
else
{
update1(l, mid, lson(now), col, v);
update1(mid + 1, r, rson(now), col, v);
}
pushup(now, col);
}
}
void update2(int l, int r, int now, int col, int v)
{
if(l <= tree[col][now].l && r >= tree[col][now].r)
{
tree[col][now].set = tree[col][now].minv = tree[col][now].maxv = v;
tree[col][now].sum = (tree[col][now].r - tree[col][now].l + 1) * v;
tree[col][now].add = 0;
}
else
{
pushdown(now, col);
int mid = (tree[col][now].l + tree[col][now].r) >> 1;
if(r <= mid) update2(l, r, lson(now), col, v);
else if(l > mid) update2(l, r, rson(now), col, v);
else
{
update2(l, mid, lson(now), col, v);
update2(mid + 1, r, rson(now), col, v);
}
pushup(now, col);
}
}
void query(int l, int r, int now, int col, int & sum, int & minv, int & maxv)
{
if(l <= tree[col][now].l && r >= tree[col][now].r)
{
sum += tree[col][now].sum;
minv = min(minv, tree[col][now].minv);
maxv = max(maxv, tree[col][now].maxv);
}
else
{
pushdown(now, col);
int mid = (tree[col][now].l + tree[col][now].r) >> 1;
if(r <= mid) query(l, r, lson(now), col, sum, minv, maxv);
else if(l > mid) query(l, r, rson(now), col, sum, minv, maxv);
else
{
query(l, mid, lson(now), col, sum, minv, maxv);
query(mid + 1, r, rson(now), col, sum, minv, maxv);
}
}
}
int main()
{
freopen("in", "r", stdin);
while(~scanf("%d %d %d", &r, &c, &m))
{
for(int i = 1; i <= r; ++i)
build(1, c, 1, i);
while(m--)
{
int cas; scanf("%d", &cas);
if(cas == 1)
{
int x1, y1, x2, y2, v;
scanf("%d %d %d %d %d", &x1, &y1, &x2, &y2, &v);
for(int i = x1; i <= x2; ++i) update1(y1, y2, 1, i, v);
}
else if(cas == 2)
{
int x1, y1, x2, y2, v;
scanf("%d %d %d %d %d", &x1, &y1, &x2, &y2, &v);
for(int i = x1; i <= x2; ++i) update2(y1, y2, 1, i, v);
}
else
{
int x1, y1, x2, y2;
int minv = 100000000, maxv = 0, sum = 0;
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
for(int i = x1; i <= x2; ++i)
query(y1, y2, 1, i, sum, minv, maxv);
printf("%d %d %d\n", sum, minv, maxv);
}
}
}
return 0;
}
/*
4 4 8
1 1 2 4 4 5
3 2 1 4 4
1 1 1 3 4 2
3 1 2 4 4
3 1 1 3 4
2 2 1 4 4 2
3 1 2 4 4
1 1 1 4 3 3
45 0 5
78 5 7
69 2 7
39 2 7
*/