码迷,mamicode.com
首页 > 其他好文 > 详细

UVA 11992 Fast Matrix Operations

时间:2014-08-27 18:50:58      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:des   style   color   os   io   for   ar   div   amp   


每一行都建一个线段树。。。。


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!

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

const int maxn=100100;

using namespace std;

const int INF=0x3f3f3f3f;

int setv[30][maxn<<2],addv[30][maxn<<2],sum[30][maxn<<2],maxnum[30][maxn<<2],minnum[30][maxn<<2];

void pushup(int num,int rt)
{
    int ls=rt<<1,rs=rt<<1|1;
    sum[num][rt]=sum[num][ls]+sum[num][rs];
    maxnum[num][rt]=max(maxnum[num][ls],maxnum[num][rs]);
    minnum[num][rt]=min(minnum[num][ls],minnum[num][rs]);
}

void pushdown(int num,int l,int r,int rt)
{
    int m=(l+r)/2;
    int ls=rt<<1,rs=rt<<1|1;
    if(setv[num][rt]>0)
    {
        setv[num][ls]=setv[num][rs]=setv[num][rt];
        addv[num][ls]=addv[num][rs]=0;
        sum[num][ls]=(m-l+1)*setv[num][rt];
        sum[num][rs]=(r-m)*setv[num][rt];
        minnum[num][ls]=minnum[num][rs]=setv[num][rt];
        maxnum[num][ls]=maxnum[num][rs]=setv[num][rt];
        setv[num][rt]=-1;
    }
    if(addv[num][rt]>0)
    {
        addv[num][ls]+=addv[num][rt];
        addv[num][rs]+=addv[num][rt];
        sum[num][ls]+=addv[num][rt]*(m-l+1);
        sum[num][rs]+=addv[num][rt]*(r-m);
        minnum[num][ls]+=addv[num][rt];
        minnum[num][rs]+=addv[num][rt];
        maxnum[num][ls]+=addv[num][rt];
        maxnum[num][rs]+=addv[num][rt];
        addv[num][rt]=0;
    }
}

void build(int num,int l,int r,int rt)
{
    addv[num][rt]=0; setv[num][rt]=-1;
    if(l==r)
    {
        minnum[num][rt]=maxnum[num][rt]=sum[num][rt]=0;
        return ;
    }
    int m=(l+r)/2;
    build(num,l,m,rt<<1);
    build(num,m+1,r,rt<<1|1);
    pushup(num,rt);
}

void update(int type,int value,int num,int L,int R,int l,int r,int rt)
{
    if(L<=l&&r<=R)
    {
        if(type==1)///add
        {
            addv[num][rt]+=value;
            sum[num][rt]+=value*(r-l+1);
            minnum[num][rt]+=value;
            maxnum[num][rt]+=value;
        }
        else if(type==2)///set
        {
            addv[num][rt]=0;
            setv[num][rt]=value;
            sum[num][rt]=(r-l+1)*value;
            minnum[num][rt]=maxnum[num][rt]=value;
        }
        return ;
    }
    pushdown(num,l,r,rt);
    int m=(l+r)/2;
    if(L<=m)
        update(type,value,num,L,R,l,m,rt<<1);
    if(R>m)
        update(type,value,num,L,R,m+1,r,rt<<1|1);
    pushup(num,rt);
}

void query(int num,int L,int R,int l,int r,int rt,int& s,int& maxv,int& minv)
{
   // cout<<" ..........  "<<l<<" "<<r<<" "<<rt<<endl;
    if(L<=l&&r<=R)
    {
        s=sum[num][rt];
        maxv=maxnum[num][rt];
        minv=minnum[num][rt];
        return ;
    }
    pushdown(num,l,r,rt);
    int m=(l+r)/2;
    int ts,tmaxv,tminv;
    s=0;maxv=-1;minv=INF;
    if(L<=m)
    {
        query(num,L,R,l,m,rt<<1,ts,tmaxv,tminv);
        s+=ts;
        maxv=max(maxv,tmaxv);
        minv=min(minv,tminv);
    }
    if(m<R)
    {
        query(num,L,R,m+1,r,rt<<1|1,ts,tmaxv,tminv);
        s+=ts;
        maxv=max(maxv,tmaxv);
        minv=min(minv,tminv);
    }
}

int main()
{
    int n,m,q;
    while(scanf("%d%d%d",&m,&n,&q)!=EOF)
    {
        for(int i=1;i<=m;i++)
            build(i,1,n,1);
        int type,x1,x2,y1,y2,v;
        while(q--)
        {
            scanf("%d",&type);
            if(type==1||type==2)
            {
                scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&v);
                for(int i=x1;i<=x2;i++)
                    update(type,v,i,y1,y2,1,n,1);
            }
            else if(type==3)
            {
                 scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
                 int s=0,minv=INF,maxv=-1;
                 int ts,tmin,tmax;
                 for(int i=x1;i<=x2;i++)
                 {
                     query(i,y1,y2,1,n,1,ts,tmax,tmin);
                     s+=ts;
                     maxv=max(maxv,tmax);
                     minv=min(minv,tmin);
                 }
                 printf("%d %d %d\n",s,minv,maxv);
            }
        }
    }
    return 0;
}

/*
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
*/





UVA 11992 Fast Matrix Operations

标签:des   style   color   os   io   for   ar   div   amp   

原文地址:http://blog.csdn.net/ck_boss/article/details/38873321

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!