uva11992 - Fast Matrix Operations 区间修改 二维线段树

Problem F

Fast Matrix Operations

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). The size of input file does not exceed 500KB.

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

Output for the Sample Input

45 0 5
78 5 7
69 2 7
39 2 7

  这个题真好。。

  任意区间都能分解成不超过2h个不相交区间的并,这是区间修改和点修改的区别。

  二维线段树,其实就是一些一维线段树。。每一行看成一个线段树,矩阵修改就变成了把每一行的线段树修改。这里add操作其实是不需要pushdown的,但是为了方便,少写点代码,不管是set还是add都pushdown。

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<cstring>
#include<queue>
#include<map>
#include<set>
#include<stack>
#define INF 0x3f3f3f3f
#define MAXN 25
#define MAXM 510
#define MAXNODE 1<<17
#define eps 1e-9
#define pi 4*atan(1.0)
#define pii pair<int,int>
using namespace std;
int T,R,C,M,op,x1,y1,x2,y2,v,_sum,_min,_max;

//修改、查询区间均为[y1,y2]
struct IntervalTree{
    //同一结点先考虑set,再考虑add
    //sumv[o],minv[o],maxv[o]保存的是以o为根结点的树内修改后的和,最大,最小值,o的父节点及以上结点修改的情况没有包括在里面
    int setv[MAXNODE],addv[MAXNODE],sumv[MAXNODE],minv[MAXNODE],maxv[MAXNODE];

    void maintain(int o,int L,int R){
        int lc=(o<<1),rc=(o<<1)+1;
        if(L<R){
            sumv[o]=sumv[lc]+sumv[rc];
            minv[o]=min(minv[lc],minv[rc]);
            maxv[o]=max(maxv[lc],maxv[rc]);
        }
        if(setv[o]>=0){
            sumv[o]=setv[o]*(R-L+1);
            minv[o]=maxv[o]=setv[o];
        }
        if(addv[o]){
            sumv[o]+=addv[o]*(R-L+1);
            minv[o]+=addv[o];
            maxv[o]+=addv[o];
        }
    }

    //把o结点的set和add都往下推一层
    void pushdown(int o){
        int lc=(o<<1),rc=(o<<1)+1;
        if(setv[o]>=0){
            setv[lc]=setv[rc]=setv[o];
            setv[o]=-1;
            addv[lc]=addv[rc]=0;
        }
        if(addv[o]){
            addv[lc]+=addv[o];
            addv[rc]+=addv[o];
            addv[o]=0;
        }
    }

    void update(int o,int L,int R){
        if(y1<=L&&y2>=R){
            if(op==1) addv[o]+=v;
            else{
                setv[o]=v;
                addv[o]=0;
            }
        }
        else{
            pushdown(o);
            int mid=(L+R)/2,lc=(o<<1),rc=(o<<1)+1;
            if(y1<=mid) update(lc,L,mid);
            else maintain(lc,L,mid);    //更新过的子树会自动maintain,没有更新的子树也要maintain,因为在pushdown的时候可能被修改
            if(y2>mid) update(rc,mid+1,R);
            else maintain(rc,mid+1,R);
        }
        maintain(o,L,R);
    }

    void query(int o,int L,int R,int add){    //add是o结点的父结点及以上结点增加的值,也要被加入o
        if(setv[o]>=0){    //set是个例外,不需要[L,R]包含在[y1,y2]中
            int v=setv[o]+addv[o]+add;
            _sum+=v*(min(R,y2)-max(L,y1)+1);    //两个区间的交集
            _min=min(_min,v);
            _max=max(_max,v);
        }
        else if(y1<=L&&y2>=R){
            _sum+=sumv[o]+add*(R-L+1);
            _min=min(_min,minv[o]+add);
            _max=max(_max,maxv[o]+add);
        }
        else{
            int mid=(L+R)/2,lc=(o<<1),rc=(o<<1)+1;
            if(y1<=mid) query(lc,L,mid,add+addv[o]);
            if(y2>mid) query(rc,mid+1,R,add+addv[o]);
        }
    }

}tree[MAXN];

int main(){
    freopen("in.txt","r",stdin);
    while(scanf("%d%d%d",&R,&C,&M)!=EOF){
        memset(tree,0,sizeof(tree));
        for(int i=1;i<=R;i++){
            memset(tree[i].setv,-1,sizeof(tree[i].setv));
            tree[i].setv[1]=0;
        }
        while(M--){
            scanf("%d%d%d%d%d",&op,&x1,&y1,&x2,&y2);
            if(op<3){
                scanf("%d",&v);
                for(int i=x1;i<=x2;i++) tree[i].update(1,1,C);
            }
            else{
                _sum=0;
                _min=INF;
                _max=-INF;
                for(int i=x1;i<=x2;i++) tree[i].query(1,1,C,0);
                printf("%d %d %d\n",_sum,_min,_max);
            }
        }
    }
    return 0;
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值