[BZOJ1176] [Balkan2007]Mokia

[Balkan2007]Mokia

Description
维护一个W*W的矩阵,初始值均为S.每次操作可以增加某格子的权值,或询问某子矩阵的总权值.修改操作数M<=160000,询问数Q<=10000,W<=2000000.
Input
第一行两个整数,S,W;其中S为矩阵初始值;W为矩阵大小
接下来每行为一下三种输入之一(不包含引号):
“1 x y a”
“2 x1 y1 x2 y2”
“3”
输入1:你需要把(x,y)(第x行第y列)的格子权值增加a
输入2:你需要求出以左上角为(x1,y1),右下角为(x2,y2)的矩阵内所有格子的权值和,并输出
输入3:表示输入结束
Output
对于每个输入2,输出一行,即输入2的答案
Sample Input
0 4
1 2 3 3
2 1 1 3 3
1 2 2 2
2 2 2 3 4
3
Sample Output
3
5
HINT
保证答案不会超过int范围

Solution
问题有三维,一是时间,二是 x ,三是y
这里我们先对 x 有序化,然后CDQ分治时间t,树状数组维护 y <script type="math/tex" id="MathJax-Element-91">y</script>
其实三个维度等价,无所谓

Code

#include <bits/stdc++.h>

using namespace std;

const int N = 200010;
const int M = 2000010;

struct Event{
    int x,y,op,dt;
    int t,num;
}lst[N],np[N];

int s,n,m,cnt;
int ans[N],d[M];

inline bool cmp(const Event &a,const Event &b){
    return a.x<b.x || a.x==b.x && a.y<b.y || a.x==b.x && a.y==b.y && !a.op && b.op;
}

inline void mdf(int x,int dt){for(;x<=n;x+=x&-x) d[x]+=dt;}
inline int qry(int x){int rs=0;for(;x>0;x^=x&-x) rs+=d[x];return rs;}

inline void solve(int l,int r){
    if(l==r) return;
    int mid=l+r>>1;
    for(int i=l;i<=r;i++){
        if(lst[i].t<=mid && !lst[i].op) mdf(lst[i].y,lst[i].dt);
        if(lst[i].t>mid && lst[i].op) ans[lst[i].num]+=qry(lst[i].y)*lst[i].op;
    }
    for(int i=l;i<=r;i++)
        if(lst[i].t<=mid && !lst[i].op) mdf(lst[i].y,-lst[i].dt);
    int lp=l,rp=mid+1;
    for(int i=l;i<=r;i++)
        if(lst[i].t<=mid) np[lp++]=lst[i];
        else np[rp++]=lst[i];
    memcpy(lst+l,np+l,sizeof(Event)*(r-l+1));
    solve(l,mid);solve(mid+1,r);
}
int main()
{
    scanf("%d%d",&s,&n);
    for(int op;scanf("%d",&op) && op!=3;){
        int x1,y1,x2,y2,t;
        scanf("%d%d",&x1,&y1);
        if(op==1){
            scanf("%d",&t);
            cnt++;
            lst[cnt].x=x1;lst[cnt].y=y1;lst[cnt].dt=t;lst[cnt].op=0;lst[cnt].t=cnt;
        }
        else{
            scanf("%d%d",&x2,&y2);m++;
            cnt++;lst[cnt].x=x1-1;lst[cnt].y=y1-1;lst[cnt].op=1;lst[cnt].t=cnt;lst[cnt].num=m;
            cnt++;lst[cnt].x=x1-1;lst[cnt].y=y2;lst[cnt].op=-1;lst[cnt].t=cnt;lst[cnt].num=m;
            cnt++;lst[cnt].x=x2;lst[cnt].y=y1-1;lst[cnt].op=-1;lst[cnt].t=cnt;lst[cnt].num=m;
            cnt++;lst[cnt].x=x2;lst[cnt].y=y2;lst[cnt].op=1;lst[cnt].t=cnt;lst[cnt].num=m;
            ans[m]=s*(x2-x1+1)*(y2-y1+1);
        }
    }
    sort(lst+1,lst+cnt+1,cmp);
    solve(1,cnt);
    for(int i=1;i<=m;i++) printf("%d\n",ans[i]);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值