Ultraviolet A 11992

Source: Internet
Author: User
Tags x2 y2
Fast Matrix Operations
Time limit:5000 Ms   Memory limit:Unknown   64bit Io format:% LLD & % LlU

[Submit]
[Go Back] [Status]

Description

Problem ffast 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 81 1 2 4 4 53 2 1 4 41 1 1 3 4 23 1 2 4 43 1 1 3 42 2 1 4 4 23 1 2 4 41 1 1 4 3 3
Output for the sample input
45 0 578 5 769 2 739 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!

Broken eggs .. Of the range merging class of the Line Segment tree. A multi-dimensional line segment tree is used, represented by a one-dimensional array. Each 4 * C (c) represents the matrix columns and represents a line segment tree.For the smallest, largest, and Range Sum, you can simply merge them. Enable two lazy labels, add, and COV, respectively for adding and adding all values in the interval and setting v. When values are added to the range, values are directly added to the add of the range. When the range is set to V, The COV is directly set to V, and the value of ADD is changed to 0 because of the computing priority during processing .. No error is found .. T [T + id <1 | 1] // This cannot be written .. '+' Has a higher computing priority than '<. T [T + (ID <1 | 1)] ...... After the sample code is passed, it is handed in directly. Actually 1A ..
#include<cstdio>#include<vector>#include<algorithm>#include<cstring>using namespace std;int r,c,m;const int maxn = 1000010;const int inf = 1000000009;int amax[22],amin[22],asum[22];struct node{    int l,r,sum,min,max,add,cov;}T[maxn*5];void build(int i,int id,int l,int r){    int t=4*i*c;    T[id+t].l=l;  T[id+t].r=r;  T[id+t].add=0;  T[id+t].cov=-1;T[id+t].sum=T[id+t].min=T[id+t].max=0;    if(l==r)   return ;    int m=(l+r)>>1;    build(i,id<<1,l,m);build(i,id<<1|1,m+1,r);}void pushDown(int k,int id){    int t=4*k*c;    if(T[t+id].cov!=-1){        if(T[t+id].l!=T[t+id].r){        T[t+(id<<1)].add=T[t+(id<<1|1)].add=0;        T[t+(id<<1)].cov=T[t+(id<<1|1)].cov=T[t+id].cov;        T[t+(id<<1)].max=T[t+(id<<1|1)].max=T[t+id].cov;        T[t+(id<<1)].min=T[t+(id<<1|1)].min=T[t+id].cov;        T[t+(id<<1)].sum=(T[t+(id<<1)].r-T[t+(id<<1)].l+1)*T[t+id].cov;        T[t+(id<<1|1)].sum=(T[t+(id<<1|1)].r-T[t+(id<<1|1)].l+1)*T[t+id].cov;        }        T[t+id].cov=-1;    }    int tt=T[t+id].add;    if(T[t+id].add>0){        if(T[t+id].l!=T[t+id].r){        T[t+(id<<1)].add+=tt; T[t+(id<<1|1)].add+=tt;        T[t+(id<<1)].max+=tt; T[t+(id<<1|1)].max+=tt;        T[t+(id<<1)].min+=tt; T[t+(id<<1|1)].min+=tt;        T[t+(id<<1)].sum+=tt*(T[t+(id<<1)].r-T[t+(id<<1)].l+1);        T[t+(id<<1|1)].sum+=tt*(T[t+(id<<1|1)].r-T[t+(id<<1|1)].l+1);        }        T[t+id].add=0;    }}void pushUp(int k,int id){    int t=4*k*c;    T[t+id].max=max(T[t+(id<<1)].max,T[t+(id<<1|1)].max);    T[t+id].min=min(T[t+(id<<1)].min,T[t+(id<<1|1)].min);    T[t+id].sum=T[t+(id<<1)].sum+T[t+(id<<1|1)].sum;}void update_add(int k,int id,int l,int r,int val){     int t=4*k*c+id;     if(T[t].l==l&&T[t].r==r){        T[t].add+=val;        T[t].max+=val;        T[t].min+=val;        T[t].sum+=val*(r-l+1);        return ;     }     pushDown(k,id);     int m=(T[t].l+T[t].r)>>1;     if(m>=r)   update_add(k,id<<1,l,r,val);     else if(m<l) update_add(k,id<<1|1,l,r,val);     else{          update_add(k,id<<1,l,m,val);          update_add(k,id<<1|1,m+1,r,val);     }     pushUp(k,id);}void update_set(int k,int id,int l,int r,int val){     int t=4*k*c+id;     if(T[t].l==l&&T[t].r==r){        T[t].cov=val;  T[t].add=0;        T[t].max=val;        T[t].min=val;        T[t].sum=val*(r-l+1);        return ;     }     pushDown(k,id);     int m=(T[t].l+T[t].r)>>1;     if(m>=r)   update_set(k,id<<1,l,r,val);     else if(m<l) update_set(k,id<<1|1,l,r,val);     else{          update_set(k,id<<1,l,m,val);          update_set(k,id<<1|1,m+1,r,val);     }     pushUp(k,id);}void query(int k,int id,int l,int r){    int t=4*k*c+id;    if(T[t].l==l&&T[t].r==r){         asum[k]+=T[t].sum;         amax[k]=max(amax[k],T[t].max);         amin[k]=min(amin[k],T[t].min);         return ;    }     pushDown(k,id);     int m=(T[t].l+T[t].r)>>1;     if(m>=r)   query(k,id<<1,l,r);     else if(m<l) query(k,id<<1|1,l,r);     else{          query(k,id<<1,l,m);          query(k,id<<1|1,m+1,r);     }     pushUp(k,id);}int main(){    while(scanf("%d%d%d",&r,&c,&m)!=EOF){         for(int i=1;i<=r;i++)         build(i,1,1,c+1);         int id,x1,y1,x2,y2,v;         while(m--){             scanf("%d",&id);             if(id==1){                 scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&v);                 for(int i=x1;i<=x2;i++)                    update_add(i,1,y1,y2,v);             }             else if(id==2){                 scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&v);                 for(int i=x1;i<=x2;i++)                    update_set(i,1,y1,y2,v);             }             else {                 memset(asum,0,sizeof(asum));                 scanf("%d%d%d%d",&x1,&y1,&x2,&y2);                 int sum=0,rmax=-inf,rmin=inf;                 for(int i=x1;i<=x2;i++) amax[i]=-inf,amin[i]=inf;                 for(int i=x1;i<=x2;i++){                     query(i,1,y1,y2);                     sum+=asum[i];                     rmax=max(rmax,amax[i]);                     rmin=min(rmin,amin[i]);                 }                 printf("%d %d %d\n",sum,rmin,rmax);             }         }    }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.