UVa11992 - Fast Matrix Operations(线段树)

题目链接

简介:
给出一个矩阵,支持以下操作:
子矩阵加,子矩阵赋值,子矩阵查询元素和,最大值和最小值

分析:
矩阵不超过20行
所以我们可以每一行都建一棵线段树,这样就转换成了一维问题

set和add操作,我们默认set的优先级高
但是我认为,因为我们随时进行push
应该不会有操作叠加或者同时又add和set标记的情况

tip

UVa上一直RE。。。STO

//这里写RE的代码片

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

using namespace std;

const int INF=1<<20;
const int N=50003;
struct node{
    int x,y,mn,mx,sum,st,ad;
};
node t[21][N<<2];
int n,m,q;

void update(node *t,int bh)
{
    int lc=bh<<1;
    int rc=(bh<<1)+1;
    t[bh].mn=min(t[lc].mn,t[rc].mn);
    t[bh].mx=max(t[lc].mx,t[rc].mx);
    t[bh].sum=t[lc].sum+t[rc].sum;
    return;
}

void push(node *t,int bh)
{
    int lc=bh<<1;
    int rc=bh<<1|1;
    if (t[bh].st!=-1||t[bh].ad!=0)
    {
        if (t[bh].st!=-1)
        {
            t[lc].sum=(t[lc].y-t[lc].x+1)*t[bh].st;
            t[lc].mn=t[bh].st; t[lc].mx=t[bh].st;
            t[rc].sum=(t[rc].y-t[rc].x+1)*t[bh].st;
            t[rc].mn=t[bh].st; t[rc].mx=t[bh].st;
            t[lc].st=t[rc].st=t[bh].st;
        }
        if (t[bh].ad!=0)
        {
            t[lc].sum+=(t[lc].y-t[lc].x+1)*t[bh].ad;
            t[lc].mn+=t[bh].ad; t[lc].mx+=t[bh].ad;
            t[rc].sum+=(t[rc].y-t[rc].x+1)*t[bh].ad;
            t[rc].mn+=t[bh].ad; t[rc].mx+=t[bh].ad;
            t[lc].ad+=t[bh].ad;
            t[rc].ad+=t[bh].ad;
        }
        t[bh].ad=0;
        t[bh].st=-1;
    }
}

void build(node *t,int bh,int l,int r)
{
    t[bh].x=l; t[bh].y=r; t[bh].st=-1; t[bh].ad=0;
    t[bh].mn=0; t[bh].mx=0; t[bh].sum=0;
    if (l==r) return;
    int mid=(l+r)>>1;
    build(t,bh<<1,l,mid);
    build(t,(bh<<1)+1,mid+1,r);
    update(t,bh);
}

void add(node *t,int bh,int l,int r,int v)
{
    push(t,bh);
    if (t[bh].x>=l&&t[bh].y<=r)
    {
        t[bh].sum+=(t[bh].y-t[bh].x+1)*v;
        t[bh].mn+=v;
        t[bh].mx+=v;
        t[bh].ad=v;
        return;
    }
    int mid=(t[bh].x+t[bh].y)>>1;
    if (l<=mid) add(t,bh<<1,l,r,v);
    if (r>mid) add(t,(bh<<1)+1,l,r,v);
    update(t,bh);
}

void set(node *t,int bh,int l,int r,int v)
{
    push(t,bh);
    if (t[bh].x>=l&&t[bh].y<=r)
    {
        t[bh].sum=(t[bh].y-t[bh].x+1)*v;
        t[bh].mn=v;
        t[bh].mx=v;
        t[bh].st=v;
        return;
    }
    int mid=(t[bh].x+t[bh].y)>>1;
    if (l<=mid) set(t,bh<<1,l,r,v);
    if (r>mid) set(t,(bh<<1)+1,l,r,v);
    update(t,bh);
}

int askmin(node *t,int bh,int l,int r)
{
    push(t,bh);
    if (t[bh].x>=l&&t[bh].y<=r)
        return t[bh].mn;
    int mid=(t[bh].x+t[bh].y)>>1;
    int ans=INF;
    if (l<=mid) ans=min(ans,askmin(t,bh<<1,l,r));
    if (r>mid) ans=min(ans,askmin(t,(bh<<1)+1,l,r));
    return ans;
}

int askmax(node *t,int bh,int l,int r)
{
    push(t,bh);
    if (t[bh].x>=l&&t[bh].y<=r)
        return t[bh].mx;
    int mid=(t[bh].x+t[bh].y)>>1;
    int ans=-INF;
    if (l<=mid) ans=max(ans,askmax(t,bh<<1,l,r));
    if (r>mid) ans=max(ans,askmax(t,(bh<<1)+1,l,r));
    return ans;
}

int asksum(node *t,int bh,int l,int r)
{
    push(t,bh);
    if (t[bh].x>=l&&t[bh].y<=r)
        return t[bh].sum;
    int mid=(t[bh].x+t[bh].y)>>1;
    int ans=0;
    if (l<=mid) ans+=asksum(t,bh<<1,l,r);
    if (r>mid) ans+=asksum(t,(bh<<1)+1,l,r);
    return ans;
}

int main()
{
    while (scanf("%d%d%d",&n,&m,&q)!=EOF)
    {
        for (int i=1;i<=n;i++)
            build(t[i],1,1,m);

        int opt,xa,ya,xb,yb,v;
        for (int i=1;i<=q;i++)
        {
            scanf("%d%d%d%d%d",&opt,&xa,&ya,&xb,&yb);
            if (opt==1)
            {
                scanf("%d",&v);
                for (int j=xa;j<=xb;j++)
                    add(t[j],1,ya,yb,v);
            }
            else if (opt==2)
            {
                scanf("%d",&v);
                for (int j=xa;j<=xb;j++)
                    set(t[j],1,ya,yb,v);
            }
            else
            {
                int minn=INF;
                int maxx=-INF;
                int sum=0;
                for (int j=xa;j<=xb;j++)
                {
                    minn=min(minn,askmin(t[j],1,ya,yb));
                    maxx=max(maxx,askmax(t[j],1,ya,yb));
                    sum+=asksum(t[j],1,ya,yb);
                }
                printf("%d %d %d\n",sum,minn,maxx);
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值