BNU20410 UVA11992 线段树区间更新

Fast Matrix Operations

Time Limit: 5000ms
Memory Limit: 131072KB
This problem will be judged on  UVA. Original ID:  11992
64-bit integer IO format:  %lld      Java class name:  Main
Type: 
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •                   
  • [PDF Link]

    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

    https://www.bnuoj.com/v3/problem_show.php?pid=20410


    给你一个初始全是0的矩阵r*c    三个操作1把满足 x1<=x<=x2  y1<=y<y2    (x,y)的子矩阵都加V 

    2就把子矩阵变成V  3就是查询子矩阵和,最小值,最大值;

    多维转换成一维  注意set的操作放下去的时候add标记得清零


    #include<stdio.h>
    #include<algorithm>
    using namespace std;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    int SUM,MIN,MAX;
    struct node
    {
     int minn,maxn,sum,add,st;
    } tree[1000010*4];
    
    void pushup(int rt)
    {
        int lc=rt<<1,rc=rt<<1|1;
        tree[rt].sum=tree[lc].sum+tree[rc].sum;
        tree[rt].minn=min(tree[lc].minn,tree[rc].minn);
        tree[rt].maxn=max(tree[lc].maxn,tree[rc].maxn);
    }
    
    void build(int l,int r,int rt)
    {
        tree[rt].add=0;
        tree[rt].st=-1;
        tree[rt].sum=0;
        tree[rt].minn=0;
        tree[rt].maxn=0;
        if(l==r)  return ;
        int m=(l+r)>>1;
        build(lson);
        build(rson);
        pushup(rt);
    }
    
    void pushdown(int rt,int len)
    {
        int lc=(rt<<1);
        int rc=(rt<<1|1);
        if(tree[rt].st>=0)
        {
            tree[lc].st=tree[rc].st=tree[rt].st;
            tree[lc].add=tree[rc].add=0;//add清除
            tree[rt].st=-1;
            tree[lc].sum=tree[lc].st*(len-(len>>1));
            tree[rc].sum=tree[rc].st*(len>>1);
            tree[lc].minn=tree[lc].st;
            tree[lc].maxn=tree[lc].st;
            tree[rc].minn=tree[rc].st;
            tree[rc].maxn=tree[rc].st;
        }
        if(tree[rt].add)
        {
            tree[lc].add+=tree[rt].add;
            tree[rc].add+=tree[rt].add;
            tree[lc].sum+=tree[rt].add*(len-(len>>1));//放下去个一半
            tree[rc].sum+=tree[rt].add*(len>>1);//只要加上上层传下来的
            tree[lc].minn+=tree[rt].add;
            tree[lc].maxn+=tree[rt].add;
            tree[rc].minn+=tree[rt].add;
            tree[rc].maxn+=tree[rt].add;
            tree[rt].add=0;
        }
    }
    
    void update1(int L,int R,int l,int r,int rt,int p)
    {
        if(L<=l&&R>=r)
        {
            tree[rt].add+=p;  //本层更新
            tree[rt].sum+=(r-l+1)*p;
            tree[rt].minn+=p;
            tree[rt].maxn+=p;
            return ;
        }
        pushdown(rt,r-l+1);
        int m=(l+r)>>1;
        if(L<=m) update1(L,R,lson,p);
        if(R>m)  update1(L,R,rson,p);
        pushup(rt);                 //更新返回
    }
    
    void update2(int L,int R,int l,int r,int rt,int p)
    {
        if(L<=l&&R>=r)
        {
            tree[rt].add=0;
            tree[rt].st=p;
            tree[rt].sum=(r-l+1)*p;
            tree[rt].minn=p;
            tree[rt].maxn=p;
            return ;
        }
        pushdown(rt,r-l+1);
        int m=(l+r)>>1;
        if(L<=m) update2(L,R,lson,p);
        if(R>m)  update2(L,R,rson,p);
        pushup(rt);
    }
    
    int query(int L,int R,int l,int r,int rt)
    {
        if(L<=l&&R>=r)
        {
            MIN=min(MIN,tree[rt].minn);
            MAX=max(MAX,tree[rt].maxn);
            return tree[rt].sum;
        }
        pushdown(rt,r-l+1);
        int ret=0;
        int m=(l+r)>>1;
        if(L<=m)   ret+=query(L,R,lson);
        if(R>m)    ret+=query(L,R,rson);
        return ret;
    }
    
    int main()
    {
        int r,c,q,s,i,x1,x2,y1,y2,v,n;
        while(scanf("%d%d%d",&r,&c,&q)!=EOF)
        {
            n=r*c;
            build(1,n,1);
            while(q--)
            {
                scanf("%d",&s);
                if(s!=3)
                {
                    if(s==1)
                    {
                        scanf("%d %d %d %d %d",&x1,&y1,&x2,&y2,&v);
                        for(i=x1-1; i<x2; i++)
                            update1(i*c+y1,i*c+y2,1,n,1,v);
                    }
                    else
                    {
                        scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&v);
                        for(i=x1-1; i<x2; i++)
                            update2(i*c+y1,i*c+y2,1,n,1,v);
                    }
                }
                else
                {
                    scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
                    SUM=0;
                    MIN=0x7f7f7f7f;
                    MAX=0;
                    for(i=x1-1; i<x2; i++)
                        SUM+=query(i*c+y1,i*c+y2,1,n,1);
                        printf("%d %d %d\n",SUM,MIN,MAX);
    
                }
            }
        }
        return 0;
    }


    • 0
      点赞
    • 1
      收藏
      觉得还不错? 一键收藏
    • 0
      评论
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

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

    抵扣说明:

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

    余额充值