UVA 11992- Fast Matrix Operations(线段树区间各种操作)

Description

Download as PDF

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

                                                                                   

思路:线段树的区间的值add 和 set ,区间求最大值,最小值,总和。

注意:当add 和set 混合使用的时候应该注意在存在set 之后应该将之前的add 清空,但是注意不要将后面的add 清零~在代码中已有注释。

CODE:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
const int inf=1000000009;
typedef long long ll;
using namespace std;

const int Max=60005;
int R,C,M,q;
int sum,minn,maxn;

struct SegmentTree
{
    int sumv[Max<<2];
    int addv[Max<<2];
    int setv[Max<<2];
    int maxv[Max<<2];
    int minv[Max<<2];
    void maintain(int pos){
        sumv[pos]=sumv[pos*2]+sumv[pos*2+1];
        maxv[pos]=max(maxv[pos*2],maxv[pos*2+1]);
        minv[pos]=min(minv[pos*2],minv[pos*2+1]);
    }
    void build(int pos,int l,int r){
        setv[pos]=addv[pos]=0;
        if(l==r){
            sumv[pos]=maxv[pos]=minv[pos]
            =setv[pos]=addv[pos]=0;
        }
        else{
            int mid=(l+r)/2;
            build(pos*2,l,mid);
            build(pos*2+1,mid+1,r);
            maintain(pos);
        }
    }
    void pushdown(int pos,int l,int r){
        int mid=(l+r)/2;
        if(setv[pos]!=0){
            setv[pos*2]=setv[pos*2+1]=setv[pos];

            sumv[pos*2]=(mid-l+1)*setv[pos];
            sumv[pos*2+1]=(r-mid)*setv[pos];

            minv[pos*2]=minv[pos*2+1]=setv[pos];
            maxv[pos*2]=maxv[pos*2+1]=setv[pos];

            setv[pos]=0;
            addv[pos*2]=addv[pos*2+1]=0;//记得清0~
        }
        if(addv[pos]!=0){
            addv[pos*2]+=addv[pos];
            addv[pos*2+1]+=addv[pos];

            sumv[pos*2]+=(mid-l+1)*addv[pos];
            sumv[pos*2+1]+=(r-mid)*addv[pos];

            minv[pos*2]+=addv[pos];
            minv[pos*2+1]+=addv[pos];

            maxv[pos*2]+=addv[pos];
            maxv[pos*2+1]+=addv[pos];

            addv[pos]=0;
        }
    }
    void update(int pos,int l,int r,int x,int y,int v){
        if(x<=l && y>=r){
            if(q==1){
                addv[pos]+=v;
                sumv[pos]+=(r-l+1)*v;
                minv[pos]+=v;
                maxv[pos]+=v;
            }
            else{
                setv[pos]=v;
                sumv[pos]=(r-l+1)*v;
                minv[pos]=v;
                maxv[pos]=v;
                addv[pos]=0;//同样要记得清零!
            }
        }
        else{
            pushdown(pos,l,r);
            int mid=(l+r)/2;
            if(x<=mid) update(pos*2,l,mid,x,y,v);
            if(y>mid) update(pos*2+1,mid+1,r,x,y,v);
            maintain(pos);
        }
    }
    void query(int pos,int l,int r,int x,int y){
        if(x<=l && y>=r){
            sum+=sumv[pos];
            minn=min(minn,minv[pos]);
            maxn=max(maxn,maxv[pos]);
        }
        else{
            pushdown(pos,l,r);
            int mid=(l+r)/2;
            if(x<=mid) query(pos*2,l,mid,x,y);
            if(y>mid) query(pos*2+1,mid+1,r,x,y);
        }
    }
}Tree[25];

int main()
{
    //freopen("in.in","r",stdin);
    while(~scanf("%d%d%d",&R,&C,&M)){
        for(int i=1;i<=R;i++){
            Tree[i].build(1,1,C);
        }
        while(M--){
            int x1,y1,x2,y2,vv;
            scanf("%d %d%d%d%d",
                  &q,&x1,&y1,&x2,&y2);
            if(q==3){
                sum=0;minn=inf;maxn=-inf;
                for(int i=x1;i<=x2;i++){
                    Tree[i].query(1,1,C,y1,y2);
                }
                printf("%d %d %d\n",sum,minn,maxn);
            }
            else{
                scanf("%d",&vv);
                for(int i=x1;i<=x2;i++){
                    Tree[i].update(1,1,C,y1,y2,vv);
                }
            }
        }
    }
    return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值