• UVa 11992 Fast Matrix Operations 成段更新,求最值与和


    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
    

    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<cmath>
    #include<cstring>
    #include<algorithm>
    #define N 555555
    
    using namespace std;
    const int OO=1e9;
    int num[30][N];
    int _min,_max,_sum;
    
    struct Tree
    {
        int l;
        int r;
        int max;
        int min;
        int sum;
        int add;
        int set;
    }big_tree[21][N*4];
    
    void push_up(int root,Tree tree[])
    {
        tree[root].max=max(tree[root<<1].max,tree[root<<1|1].max);
        tree[root].min=min(tree[root<<1].min,tree[root<<1|1].min);
        tree[root].sum=tree[root<<1].sum+tree[root<<1|1].sum;
    }
    
    void push_down(int root,Tree tree[])
    {
        if (tree[root].set!=-1)
        {
            if (tree[root].l!=tree[root].r)
            {
                //传递懒惰标记
                tree[root<<1].add=tree[root<<1|1].add=0;
                tree[root<<1].set=tree[root<<1|1].set=tree[root].set;
                //最更新大值
                tree[root<<1].max=tree[root<<1|1].max=tree[root].set;
                //更新最小值
                tree[root<<1].min=tree[root<<1|1].min=tree[root].set;
                //更新区间和
                tree[root<<1].sum=(tree[root<<1].r-tree[root<<1].l+1)*tree[root].set;
                tree[root<<1|1].sum=(tree[root<<1|1].r-tree[root<<1|1].l+1)*tree[root].set;
            }
            tree[root].set=-1;
        }
        if (tree[root].add>0)
        {
            if (tree[root].l!=tree[root].r)
            {
                //传递懒惰标记
                tree[root<<1].add+=tree[root].add;
                tree[root<<1|1].add+=tree[root].add;
                //更新最大值
                tree[root<<1].max+=tree[root].add;
                tree[root<<1|1].max+=tree[root].add;
                //更新最小值
                tree[root<<1].min+=tree[root].add;
                tree[root<<1|1].min+=tree[root].add;
                //更新区间和
                tree[root<<1].sum+=(tree[root<<1].r-tree[root<<1].l+1)*tree[root].add;
                tree[root<<1|1].sum+=(tree[root<<1|1].r-tree[root<<1|1].l+1)*tree[root].add;
            }
            tree[root].add=0;
        }
    }
    
    void build(int root,int l,int r,Tree tree[])
    {
        tree[root].l=l;
        tree[root].r=r;
        if(tree[root].l==tree[root].r)
        {
            tree[root].max=0;
            tree[root].min=0;
            tree[root].sum=0;
            tree[root].add=0;
            tree[root].set=-1;
            return;
        }
        int mid=(l+r)/2;
        build(root<<1,l,mid,tree);
        build(root<<1|1,mid+1,r,tree);
        push_up(root,tree);
    }
    
    void update_add(int root,int L,int R,int val,Tree tree[])
    {
    
        if(L<=tree[root].l&&R>=tree[root].r)
        {
            tree[root].add+=val;
            tree[root].max+=val;
            tree[root].min+=val;
            tree[root].sum+=(tree[root].r-tree[root].l+1)*val;
            return;
        }
        push_down(root,tree);
        int mid=(tree[root].l+tree[root].r)/2;
        if(L<=mid)
            update_add(root<<1,L,R,val,tree);
        if (R>mid)
            update_add(root<<1|1,L,R,val,tree);
        push_up(root,tree);
    }
    
    void update_set(int root,int L,int R,int val,Tree tree[])
    {
        if(L<=tree[root].l&&R>=tree[root].r)
        {
            tree[root].set=val;
            tree[root].add=0;
            tree[root].max=val;
            tree[root].min=val;
            tree[root].sum=(tree[root].r-tree[root].l+1)*val;
            return;
        }
        push_down(root,tree);
        int mid=(tree[root].l+tree[root].r)/2;
        if(L<=mid)
            update_set(root<<1,L,R,val,tree);
        if (R>mid)
            update_set(root<<1|1,L,R,val,tree);
        push_up(root,tree);
    }
    
    
    void query(int root,int L,int R,Tree tree[])
    {
        if(L<=tree[root].l&&R>=tree[root].r)
        {
            _min=min(_min,tree[root].min);
            _max=max(_max,tree[root].max);
            _sum+=tree[root].sum;
            return;
        }
        push_down(root,tree);
        int mid=(tree[root].l+tree[root].r)/2;
        if(L<=mid) query(root<<1,L,R,tree);
        if(R>mid) query(root<<1|1,L,R,tree);
    }
    
    int main()
    {
        int r,c,m;
        while (~scanf("%d%d%d",&r,&c,&m))
        {
            int tp,x1,x2,y1,y2;
            for (int i=1;i<=r;i++)
            {
                build(1,1,c,big_tree[i]);
            }
            while (m--)
            {
                int v;
                scanf("%d%d%d%d%d",&tp,&x1,&y1,&x2,&y2);
                if (tp==1)
                {
                    scanf("%d",&v);
                    for (int i=x1;i<=x2;i++)
                    {
                        update_add(1,y1,y2,v,big_tree[i]);
                    }
                }
                if (tp==2)
                {
                    scanf("%d",&v);
                    for (int i=x1;i<=x2;i++)
                    {
                        update_set(1,y1,y2,v,big_tree[i]);
                    }
                }
                if (tp==3)
                {
                    _max=0;
                    _min=OO;
                    _sum=0;
                    for (int i=x1;i<=x2;i++)
                    {
                        query(1,y1,y2,big_tree[i]);
                    }
                    printf("%d %d %d\n",_sum,_min,_max);
                }
            }
        }
        return 0;
    }
    






  • 相关阅读:
    如何将 Vim 剪贴板里面的东西粘贴到 Vim 之外的地方? (Ubuntu18.04系统亲测)
    联想还是中国企业吗,我们还应该支持联想吗?以后我还是不要选择联想的产品了吧
    使用Linux桌面壁纸应用variety发现的一些问题
    线性代数预备知识——向量与空间
    线性代数预备知识——向量及方程组
    【线性代数的本质】线性空间、基向量的几何解释
    一时失误的权限设计
    闲谈企业管理执行力的问题
    执行力的不够的系统解决方案
    执行力的问题系统的无奈
  • 原文地址:https://www.cnblogs.com/cyendra/p/3226372.html
Copyright © 2020-2023  润新知