UVA 11992 Fast Matrix Operations

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

Rujia Liu's Present 3: A Data Structure Contest Celebrating the 100th Anniversary of Tsinghua University
Special Thanks: Yeji Shen, Dun Liang

Note: Please make sure to test your program with the gift I/O files before submitting!

题目大意:有一个r行c列的全0矩阵,支持三种操作:

1 x1 y1 x2 y2 v 子矩阵(x1,y1,x2,y2)的所有元素增加v(v > 0)。

2 x1 y1 x2 y2 v 子矩阵(x1,y1,x2,y2)的所有元素设为v(v > 0)。

3 x1 y1 x2 y2    查询子矩阵(x1,y1,x2,y2)的元素和、最小值和最大值。

子矩阵(x1,y1,x2,y2)是指满足x1 <= x <= x2,y1 <= y <= y2,的所有元素(x,y)。输入保证任何时刻矩阵内的所有元素之和不超过10 ^ 9。

其中,矩阵不超过20行,元素总数不超过10 ^ 6,且操作数不超过20000。

每一行建一个线段树 有意思
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int MAX_N = 100010;
const int INF = 0x3f3f3f3f;
int s[30][MAX_N<<2],MAX[30][MAX_N<<2],MIN[30][MAX_N<<2],addv[30][MAX_N<<2],setv[30][MAX_N<<2];
void up(int i,int p){
        s[i][p]=s[i][p*2]+s[i][p*2+1];
        MIN[i][p]=min(MIN[i][p*2],MIN[i][p*2+1]);
        MAX[i][p]=max(MAX[i][p*2],MAX[i][p*2+1]);
}
void down(int i,int p,int l,int r){
    int mid = (l+r)>>1;
    if(setv[i][p]){
        setv[i][p*2] = setv[i][p];
        setv[i][p*2+1] = setv[i][p];
        addv[i][p*2]=addv[i][p*2+1]=0;
        s[i][p*2]=setv[i][p*2]*(mid-l+1);
        s[i][p*2+1]=setv[i][p*2+1]*(r-mid);
        MIN[i][p*2]=MIN[i][p*2+1]=setv[i][p];
        MAX[i][p*2]= MAX[i][p*2+1]=setv[i][p];
        setv[i][p] = 0;
    }
    if(addv[i][p]){
        addv[i][p*2] += addv[i][p];
        addv[i][p*2+1] += addv[i][p];
        s[i][p*2]+=addv[i][p]*(mid-l+1);
        s[i][p*2+1]+=addv[i][p]*(r-mid);
        MIN[i][p*2]+=addv[i][p*2];
        MIN[i][p*2+1]+=addv[i][p*2+1];
        MAX[i][p*2]+=addv[i][p];
        MAX[i][p*2+1]+=addv[i][p];
        addv[i][p] = 0;
    }
}
void build(int i,int p,int l,int r){
    addv[i][p]=0;
    setv[i][p]=0;
    if(l==r){
        MIN[i][p]=MAX[i][p]=s[i][p]=0;
        return;
    }
    int mid =(l+r)>>1;
    build(i,p*2,l,mid);
    build(i,p*2+1,mid+1,r);
    up(i,p);
}
void change(int a,int i,int p,int l,int r,int x,int y,int v){
    if(x<=l&&r<=y){
        if(a==1){
            addv[i][p]+=v;
            s[i][p]+=v*(r-l+1);
            MIN[i][p]+=v;
            MAX[i][p]+=v;
        }
        else if(a==2){
            addv[i][p]=0;
            setv[i][p]=v;
            s[i][p]=v*(r-l+1);
            MIN[i][p]=MAX[i][p]=v;
        }
        return;
    }
    down(i,p,l,r);
    int mid=(l+r)>>1;
    if(x<=mid) change(a,i,p*2,l,mid,x,y,v);
    if(y>mid) change(a,i,p*2+1,mid+1,r,x,y,v);
    up(i,p);
}
void query(int i,int p,int l,int r,int x,int y,int& sum,int& maxx,int& minn){
  if(x<=l&&r<=y){
    sum=s[i][p];
    maxx=MAX[i][p];
    minn=MIN[i][p];
    return;
  }
down(i,p,l,r);
int mid=(l+r)>>1;
int ts,tmax,tmin;
  sum=0;maxx=-1;minn=INF;
  if(x<=mid) {
    query(i,p*2,l,mid,x,y,ts,tmax,tmin);
    sum+=ts;
    maxx=max(maxx,tmax);
    minn=min(minn,tmin);
  }
  if(y>mid){
    query(i,p*2+1,mid+1,r,x,y,ts,tmax,tmin);
    sum+=ts;
    maxx=max(maxx,tmax);
    minn=min(minn,tmin);
  }
}


int main(){
    int r,c,m;
    while(~scanf("%d%d%d",&r,&c,&m)){
        for(int i=i;i<=r;++i)
           build(i,1,1,c);
    while(m--){
        int a,x1,y1,x2,y2,v;
        scanf("%d",&a);
        if(a==1||a==2){
                scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&v);
            for(int i=x1;i<=x2;++i)
               change(a,i,1,1,c,y1,y2,v);
        }
        else if(a==3){
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
           int sum = 0,minn=INF,maxx=-1;
           int ts,tmin,tmax;
           for(int i=x1;i<=x2;i++){
            query(i,1,1,c,y1,y2,ts,tmax,tmin);
            sum+=ts;
            maxx=max(maxx,tmax);
            minn=min(minn,tmin);
           }
           printf("%d %d %d\n",sum,minn,maxx);
        }
    }
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值