【数据结构】BZOJ3132 上帝造题的七分钟 题解

题意

给出一个开始为0的矩阵,有两个操作:1.将某个子矩阵内所有元素加上某个值。 2.求出某个子矩阵内所有元素和。

分析

膜拜http://www.cnblogs.com/jianglangcaijin/p/3253686.html的图
设A[i][j]表示(i,j)-(n,m)的增加量,则(1,1)-(x,y)的和sum等于:

然后维护a,b,c,d四个树状数组即可,具体怎么实现,看代码吧…
(卡常卡死我了)

代码

好懂版(OJ上交会卡常):

#include<cstdio>
#include<algorithm>
#define MAXN 2050
using namespace std;
int n,m,x1,y1,x2,y2,del;
char op[10];
int lowbit(int x)
{
    return x&(-x);
}
struct node
{
    int bit[MAXN][MAXN];
    void change(int x,int y,int d)
    {
        for(int i=x;i<=m;i+=lowbit(i))
            for(int j=y;j<=n;j+=lowbit(j))
            bit[i][j]+=d;
    }
    int addup(int x,int y)
    {
        int sum=0;
        for(int i=x;i;i-=lowbit(i))
            for(int j=y;j;j-=lowbit(j))
            sum+=bit[i][j];
        return sum;
    }
}A,B,C,D;
int getsum(int x,int y)
{
    return A.addup(x,y)*(x+1)*(y+1)-B.addup(x,y)*(y+1)-C.addup(x,y)*(x+1)+D.addup(x,y);
}
void add(int x1,int y1,int x2,int y2,int d)
{
    A.change(x1,y1,d);
    A.change(x1,y2+1,-d);
    A.change(x2+1,y1,-d);
    A.change(x2+1,y2+1,d);

    B.change(x1,y1,d*x1);
    B.change(x2+1,y1,-d*(x2+1));
    B.change(x1,y2+1,-d*x1);
    B.change(x2+1,y2+1,d*(x2+1));

    C.change(x1,y1,d*y1);
    C.change(x2+1,y1,-d*y1);
    C.change(x1,y2+1,-d*(y2+1));
    C.change(x2+1,y2+1,d*(y2+1));

    D.change(x1,y1,d*x1*y1);
    D.change(x2+1,y1,-d*(x2+1)*y1);
    D.change(x1,y2+1,-d*(y2+1)*x1);
    D.change(x2+1,y2+1,d*(x2+1)*(y2+1));
}
int main()
{
    while(~scanf("%s",op))
    {
        if(op[0]=='X') scanf("%d%d",&n,&m);
        if(op[0]=='k') 
        {
            scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
            printf("%d\n",getsum(x2,y2)+getsum(x1-1,y1-1)-getsum(x1-1,y2)-getsum(x2,y1-1));
        }
        if(op[0]=='L')
        {
            scanf("%d%d%d%d%d",&x1,&y1,&x2,&y2,&del);
            add(x1,y1,x2,y2,del);
        }
    }
}

能过版:

#include<cstdio>
#include<algorithm>
#define MAXN 2050
using namespace std;
int n,m,x1,y1,x2,y2,del;
char op[10];
void read(int &x)
{
    int f=1;x=0;char s=getchar();
    while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
    while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
    x*=f;
}
inline int lowbit(int x)
{
    return x&(-x);
}
int bit[MAXN][MAXN][4];
void change(int x,int y,int d,int o)
{
    for(int i=x;i<=n;i+=lowbit(i))
        for(int j=y;j<=m;j+=lowbit(j))
        bit[i][j][o]+=d;
}
inline int addup(int x,int y,int o)
{
    int sum=0;
    for(int i=x;i;i-=lowbit(i))
        for(int j=y;j;j-=lowbit(j))
        sum+=bit[i][j][o];
    return sum;
}
int getsum(int x,int y)
{
    return addup(x,y,0)*(x+1)*(y+1)-addup(x,y,1)*(y+1)-addup(x,y,2)*(x+1)+addup(x,y,3);
}
void add(int x1,int y1,int x2,int y2,int d)
{
    change(x1,y1,d,0);
    change(x1,y2+1,-d,0);
    change(x2+1,y1,-d,0);
    change(x2+1,y2+1,d,0);

    change(x1,y1,d*x1,1);
    change(x2+1,y1,-d*(x2+1),1);
    change(x1,y2+1,-d*x1,1);
    change(x2+1,y2+1,d*(x2+1),1);

    change(x1,y1,d*y1,2);
    change(x2+1,y1,-d*y1,2);
    change(x1,y2+1,-d*(y2+1),2);
    change(x2+1,y2+1,d*(y2+1),2);

    change(x1,y1,d*x1*y1,3);
    change(x2+1,y1,-d*(x2+1)*y1,3);
    change(x1,y2+1,-d*(y2+1)*x1,3);
    change(x2+1,y2+1,d*(x2+1)*(y2+1),3);
}
int main()
{
    while(~scanf("%s",op))
    {
        if(op[0]=='X')
        {
            read(n);
            read(m);
        }
        else if(op[0]=='L')
        {
            read(x1);
            read(y1);
            read(x2);
            read(y2);
            read(del);
            add(x1,y1,x2,y2,del);
        }
        else
        {
            read(x1);
            read(y1);
            read(x2);
            read(y2);
            printf("%d\n",getsum(x2,y2)+getsum(x1-1,y1-1)-getsum(x1-1,y2)-getsum(x2,y1-1));
        }
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值