Uva 11992 Fast Matrix Operations

来源:互联网 发布:夏达 抄袭 知乎 编辑:程序博客网 时间:2024/04/28 12:47

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 rst 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- le (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


【分析】
这题真是够劲…虽然不难但是有点麻烦
题意:给一个矩阵,要求维护三个操作
1. 某子矩阵整体+val
2. 某子矩阵整体变为val
3. 某子矩阵中元素和,最小值和最大值
其实就是每行用一个线段树维护,打懒标记。
注意一下2操作完了以后要把1操作打过的标记清除干净


【代码】

//Uva 11992 #include<iostream>#include<cstring>#include<cstdio> #define ll long long#define fo(i,j,k) for(i=j;i<=k;i++)using namespace std;const int mxn=1000005;const int inf=1e9+7;struct node {int l,r,mx,mn,become,ad,sum;} t[21][1<<17];int k,n,m,T,q,c,x1,x2,y1,y2,now,ansmin,ansmax,ansm;inline void pushdown(int num){    if(t[now][num].become)    {        t[now][num<<1].become=t[now][num<<1|1].become=t[now][num].become;        t[now][num<<1].sum=(t[now][num<<1].r-t[now][num<<1].l+1)*t[now][num].become;        t[now][num<<1|1].sum=(t[now][num<<1|1].r-t[now][num<<1|1].l+1)*t[now][num].become;        t[now][num<<1].mn=t[now][num<<1|1].mn=t[now][num].become;        t[now][num<<1].mx=t[now][num<<1|1].mx=t[now][num].become;        t[now][num<<1].ad=t[now][num<<1|1].ad=0;        t[now][num].become=0;    }    if(t[now][num].ad)    {        t[now][num<<1].ad+=t[now][num].ad;        t[now][num<<1|1].ad+=t[now][num].ad;        t[now][num<<1].sum+=(t[now][num<<1].r-t[now][num<<1].l+1)*t[now][num].ad;        t[now][num<<1|1].sum+=(t[now][num<<1|1].r-t[now][num<<1|1].l+1)*t[now][num].ad;        t[now][num<<1].mn+=t[now][num].ad;        t[now][num<<1|1].mn+=t[now][num].ad;        t[now][num<<1].mx+=t[now][num].ad;        t[now][num<<1|1].mx+=t[now][num].ad;        t[now][num].ad=0;    }}inline void update(int num){    t[now][num].sum=t[now][num<<1].sum+t[now][num<<1|1].sum;    t[now][num].mx=max(t[now][num<<1].mx,t[now][num<<1|1].mx);    t[now][num].mn=min(t[now][num<<1].mn,t[now][num<<1|1].mn);}inline void build(int num,int l,int r){    t[now][num].l=l,t[now][num].r=r;    if(l==r) return;    int mid=l+r>>1;    build(num<<1,l,mid);    build(num<<1|1,mid+1,r);} inline void change(int num){    if(y1<=t[now][num].l && t[now][num].r<=y2)    {        t[now][num].sum=(t[now][num].r-t[now][num].l+1)*k;        t[now][num].mx=t[now][num].mn=k;        t[now][num].become=k;        t[now][num].ad=0;        return;    }    pushdown(num);    int mid=t[now][num].l+t[now][num].r>>1;    if(y1<=mid) change(num<<1);    if(mid<y2) change(num<<1|1);    update(num);}inline void add(int num){    if(y1<=t[now][num].l && t[now][num].r<=y2)    {        t[now][num].sum+=(t[now][num].r-t[now][num].l+1)*k;        t[now][num].mx+=k;        t[now][num].mn+=k;        t[now][num].ad+=k;        return;    }    pushdown(num);    int mid=t[now][num].l+t[now][num].r>>1;    if(y1<=mid) add(num<<1);    if(mid<y2) add(num<<1|1);    update(num);}inline ll query(int num){    if(y1<=t[now][num].l && t[now][num].r<=y2)    {        ansmax=max(ansmax,t[now][num].mx);        ansmin=min(ansmin,t[now][num].mn);        return t[now][num].sum;    }    pushdown(num);    ll ans=0;    int mid=t[now][num].l+t[now][num].r>>1;    if(y1<=mid) ans+=query(num<<1);    if(mid<y2) ans+=query(num<<1|1);    return ans;}int main(){    int i,j;    while(scanf("%d%d%d",&c,&n,&q)!=EOF)    {        memset(t,0,sizeof t);        fo(now,1,c) build(1,1,n);        while(q--)        {            scanf("%d%d%d%d%d",&T,&x1,&y1,&x2,&y2);            if(T==1)            {                scanf("%d",&k);                fo(now,x1,x2) add(1);            }            else if(T==2)            {                scanf("%d",&k);                fo(now,x1,x2) change(1);            }            else            {                ansm=0;ansmin=inf,ansmax=-inf;                fo(now,x1,x2) ansm+=query(1);                printf("%d %d %d\n",ansm,ansmin,ansmax);            }        }    }    return 0;}//1 4 100//1 1 1 1 2 2//2 1 1 1 4 4
0 0
原创粉丝点击