原题链接
Problem Description
There is a matrix containing at most 106
elements divided into r rows and c columns. Each element
has a location (x, y) where 1 ≤ x ≤ r, 1 ≤ y ≤ c. Initially, all the elements are zero. You need to
handle four kinds of operations:
1 x1 y1 x2 y2 v Increment each element (x, y) in submatrix (x1, y1, x2, y2) by v (v >
0)
2 x1 y1 x2 y2 v Set each element (x, y) in submatrix (x1, y1, x2, y2) to v
3 x1 y1 x2 y2 Output the summation, min value and max value of submatrix
(x1, y1, x2, y2)
In the above descriptions, submatrix (x1, y1, x2, y2) means all the elements (x, y) satisfying x1 ≤
x ≤ x2 and y1 ≤ x ≤ y2. It is guaranteed that 1 ≤ x1 ≤ x2 ≤ r, 1 ≤ y1 ≤ y2 ≤ c. After any operation,
the sum of all the elements in the matrix does not exceed 109
.
Input
There are several test cases. The first line of each case contains three positive integers r, c, m, where
m (1 ≤ m ≤ 20, 000) is the number of operations. Each of the next m lines contains a query. There
will be at most twenty rows in the matrix. The input is terminated by end-of-file (EOF).
Output
For each type-3 query, print the summation, min and max.
Sample Input
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
Sample Output
45 0 5
78 5 7
69 2 7
39 2 7
题目大意
给出一个矩阵,有三种不同的操作(add,set,query),其中query操作要求输出给定区域中的数字之和,最小以及最大值。
解题思路
题意很好理解,矩阵行数较少,但是总的数据元素很多,考虑矩阵的每一行用线段树维护,最后合并结果。(由于UVA的问题似乎变量不能设为y1?)
AC代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<string>
#include<queue>
#include<list>
#include<stack>
#include<set>
#include<map>
#define ll long long
#define ull unsigned long long
#define rep(i,a,b) for (int i=(a),_ed=(b);i<_ed;i++)
#define fil(a,b) memset((a),(b),sizeof(a))
#define cl(a) fil(a,0)
#define pb push_back
#define PI 3.1415927
#define inf 0x3f3f3f3f
#define fi first
#define se second
using namespace std;
const int maxnode=1<<17;
int op,x1,x2,y11,y2,x,v;
struct IntervalTree
{
int sumv[maxnode];
int minv[maxnode];
int maxv[maxnode];
int addv[maxnode];
int setv[maxnode];
void maintain(int o,int L,int R)
{
int lson=o<<1;
int rson=o<<1|1;
if(R>L)
{
sumv[o]=sumv[lson]+sumv[rson];
maxv[o]=max(maxv[lson],maxv[rson]);
minv[o]=min(minv[lson],minv[rson]);
}
if(setv[o]>=0)
{
minv[o]=maxv[o]=setv[o];
sumv[o]=setv[o]*(R-L+1);
}
if(addv[o])
{
minv[o]+=addv[o];
maxv[o]+=addv[o];
sumv[o]+=addv[o]*(R-L+1);
}
}
void pushdown(int o)
{
int lson=o<<1;
int rson=o<<1|1;
if(setv[o]>=0)
{
setv[lson]=setv[rson]=setv[o];
addv[lson]=addv[rson]=0;
setv[o]=-1;
}
if(addv[o])
{
addv[lson]+=addv[o];
addv[rson]+=addv[o];
addv[o]=0;
}
}
void update(int o,int L,int R)
{
int lson=o<<1;
int rson=o<<1|1;
if(y11<=L&&y2>=R)
{
if(op==1)
{
addv[o]+=v;
}
else
{
setv[o]=v;
addv[o]=0;
}
}
else
{
pushdown(o);
int m=L+R>>1;
if(y11<=m) update(lson,L,m);
else maintain(lson,L,m);
if(y2>m) update(rson,m+1,R);
else maintain(rson,m+1,R);
}
maintain(o,L,R);
}
void query(int o,int L,int R,int& ssum,int& smin,int& smax)
{
int lson=o<<1;
int rson=o<<1|1;
maintain(o,L,R);
if(y11<=L&&y2>=R)
{
ssum=sumv[o];
smin=minv[o];
smax=maxv[o];
}
else
{
pushdown(o);
int m=L+R>>1;
int lsum=0,lmin=inf,lmax=-inf;
int rsum=0,rmin=inf,rmax=-inf;
if(y11<=m)
{
query(lson,L,m,lsum,lmin,lmax);
}
else maintain(lson,L,m);
if(y2>m)
{
query(rson,m+1,R,rsum,rmin,rmax);
}
else maintain(rson,m+1,R);
ssum=lsum+rsum;
smin=min(lmin,rmin);
smax=max(lmax,rmax);
}
}
};
const int maxr=25;
IntervalTree tree[maxr];
int main(void)
{
int r,c,m;
while(cin>>r>>c>>m)
{
cl(tree);
for(int x=1;x<=r;++x)
{
memset(tree[x].setv,-1,sizeof(tree[x].setv));
tree[x].setv[1]=0;
}
while(m--)
{
scanf("%d%d%d%d%d",&op,&x1,&y11,&x2,&y2);
if(op<3)
{
scanf("%d",&v);
for(int x=x1;x<=x2;++x)
{
tree[x].update(1,1,c);
}
}
else
{
int gsum=0,gmin=inf,gmax=-inf;
for(int x=x1;x<=x2;++x)
{
int ssum,smin,smax;
tree[x].query(1,1,c,ssum,smin,smax);
gsum+=ssum;
gmin=min(gmin,smin);
gmax=max(gmax,smax);
}
printf("%d %d %d\n",gsum,gmin,gmax);
}
}
}
return 0;
}