UVA 11992

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!

 Debug真是会死人的。。。

 挺简单的一道题写了200 + 行,被shl深深的鄙视了。

 本题线段树,题意是给你三个操作,把x1 , x2  ,y1 , y2 ,的矩阵进行加,赋值,和求和 , 虽然是二维的,但是行数小于20,可简化为一维。

#include <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <iomanip>

using namespace std;
//#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid  , rt << 1
#define rson mid + 1 , r , rt << 1|1
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)

#define mk make_pair
const int MAXN = 1000010 + 50;
const int maxw = 100 + 20;
const int MAXNNODE = 1000 +10;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
#define eps 1e-8
#define mod 10007
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pii;
const D e = 2.718281828459;
int row , col , n;
struct Node
{
    int l , r;
    int sum , min_num , max_num;
    int delta;
    bool lazyd , lazyv;
    Node(){}
    Node(int L , int R)
    {
        l = L , r = R;
        sum = max_num = min_num = delta = 0;
        lazyd = lazyv = false;
    }
    int mid(){return (l + r) >> 1;}
    int len(){return (r - l + 1);}
}t[MAXN << 2];
void build( int rt , int L,int R )
{
    t[rt] = Node(L , R);
    if(L == R)return ;
    int mid = t[rt].mid();
    build(rt << 1 , L , mid ) , build(rt << 1 | 1 , mid + 1 , R );
}
void pushdown(int rt)
{
    if(!t[rt].lazyd && !t[rt].lazyv)return ;
    if(t[rt].lazyd)
    {
        t[rt << 1].max_num += t[rt].delta;
        t[rt << 1].min_num += t[rt].delta;
        t[rt << 1].sum += t[rt].delta * t[rt << 1].len();
        t[rt << 1 | 1].max_num += t[rt].delta;
        t[rt << 1 | 1].min_num += t[rt].delta;
        t[rt << 1 | 1].sum += t[rt].delta * t[rt << 1 | 1].len();
        if(!t[rt << 1].lazyv)
        {
            t[rt << 1].lazyd = true;
            t[rt << 1].delta += t[rt].delta;
        }
        if(!t[rt << 1 | 1].lazyv)
        {
            t[rt << 1 | 1].lazyd = true;
            t[rt << 1 | 1].delta += t[rt].delta;
        }
        t[rt].delta = 0;
        t[rt].lazyd = false;
    }
    else
    {
        t[rt << 1].lazyv = t[rt << 1 | 1].lazyv = true;
        t[rt << 1].lazyd = t[rt << 1 | 1].lazyd = false;
        t[rt << 1].delta = t[rt << 1 | 1].delta = 0;
        t[rt << 1].max_num = t[rt << 1].min_num = t[rt].max_num;
        t[rt << 1].sum = t[rt << 1].max_num * t[rt << 1].len();

        t[rt << 1 | 1].max_num = t[rt << 1 | 1].min_num = t[rt].max_num;
        t[rt << 1 | 1].sum = t[rt << 1| 1].max_num * t[rt << 1 | 1].len();
        t[rt].lazyv = false;
    }
}
void pushup(int rt)
{
    t[rt].max_num = max(t[rt << 1].max_num , t[rt << 1 | 1].max_num);
    t[rt].min_num = min(t[rt << 1].min_num , t[rt << 1 | 1].min_num);
    t[rt].sum  = t[rt << 1].sum + t[rt << 1 | 1].sum;
}
void update (int rt , int L , int R , int k , int val)
{
    if(t[rt].l == L && t[rt].r == R)
    {
        if(k == 1)
        {
            t[rt].max_num += val;
            t[rt].min_num += val;
            t[rt].sum += val * t[rt].len();
            if(t[rt].lazyd)t[rt].delta += val;
            else if(t[rt].lazyv)t[rt].delta = 0;
            else
            {
                t[rt].delta = val;
                t[rt].lazyd = true;
            }
        }
        else
        {
            t[rt].lazyv = true;
            t[rt].lazyd = false ;
            t[rt].delta = 0;
            t[rt].max_num = t[rt].min_num = val;
            t[rt].sum = val * t[rt].len();
        }
        return;
    }

    pushdown(rt);
    int mid = t[rt].mid();
    if(R <= mid)update(rt << 1 ,L ,R ,  k , val);
    else if(L > mid)update(rt << 1 | 1 ,L  , R ,  k , val);
    else
    {
        update(rt << 1 , L , mid  , k , val);
        update(rt << 1 |1  , mid + 1 , R , k ,val);
    }
    pushup(rt);
}

Node query(int rt , int L , int R)
{
    if(t[rt].l == L && t[rt].r == R)return t[rt];
    pushdown(rt);
    int mid = t[rt].mid();
    if(R <= mid)return query(rt << 1  , L ,R);
    else if(L > mid)return query (rt << 1 | 1 , L , R);
    else
    {
        Node ans1 = query(rt << 1 , L , mid);
        Node ans2 = query(rt << 1 | 1 , mid + 1 , R);
        Node ans;
        ans.sum = ans1.sum + ans2.sum;
        ans.max_num = max(ans1.max_num , ans2.max_num);
        ans.min_num = min(ans1.min_num , ans2.min_num);
        return ans;
    }
}

int main()
{
    //ios::sync_with_stdio(false);
#ifdef Online_Judge
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
#endif // Online_Judge
    int q;
    while(~scanf("%d%d%d" , &row , & col , &q))
    {
        n = row * col;
        build(1 , 1 , n);

        while(q--)
        {
            int op , x1 , y1 , x2 , y2 , val;
            scanf("%d" , &op);
            if(op == 1)
            {
                scanf("%d%d%d%d%d" ,&x1 , &y1 , & x2 , &y2,  &val);
                FORR(i , x1 ,x2)
                {
                    int a = (i - 1) * col + y1;
                    int b = (i - 1) * col + y2;
                    update(1 , a , b , 1 , val);
                }
            }
            else if(op == 2)
            {
                scanf("%d%d%d%d%d" , &x1 ,& y1 , &x2 , & y2 , &val);
                FORR(i , x1 , x2)
                {
                    int a = (i - 1) * col + y1;
                    int b = (i - 1) * col + y2;
                    update(1  , a  ,b , 2 , val);
                }
            }
            else
            {
                Node ans;
                ans.sum = 0 ,ans.max_num = -INF , ans.min_num = INF;
                scanf("%d%d%d%d", &x1 , &y1 , &x2 , &y2);
                FORR(i , x1 , x2)
                {
                    int a = (i - 1) * col + y1;
                    int b = (i - 1) * col + y2;
                    Node res = query(1 , a , b);
                    ans.min_num = min(ans.min_num , res.min_num);
                    ans.max_num = max(ans.max_num , res.max_num);
                    ans.sum += res.sum;
                }
                printf("%d %d %d\n" , ans.sum , ans.min_num , ans.max_num);
            }

        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值