光看数据就知道二维数据结构肯定搞不了了,看一眼询问次数就知道离散化也不行,所以就要用一种新的办法——CDQ分治,具体做法如下:将所有询问按照x,y坐标排序,利用CDQ分治的思想,按时间顺序分为左右两半,然后处理左边对右边的影响,用树状数组维护当前点包括它上面所有行的数值的和,将一个询问拆成4个,这道题就可以了
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<cstring>
#include<string>
#include<iostream>
#include<iomanip>
#include<algorithm>
using namespace std;
#define QAQ 2147483647
int c[2000001];
int time_clock[2000001];
int n,w;
int top;
struct caozuo
{
int x,y,num,val,ans;
bool operator < (caozuo b) const
{
return x<b.x || (x==b.x && y<b.y);
}
}q[200020],nq[200020];
bool cmp1(caozuo a,caozuo b)
{
return a.num<b.num;
}
bool cmp2(caozuo a,caozuo b)
{
return a.x<b.x || (a.x==b.x && a.y<b.y);
}
int tt;
inline void add_val(int x,int v)
{
for(int i=x;i<=n;i+=i&-i)
{
if(time_clock[i]!=tt) c[i]=0;
time_clock[i]=tt;
c[i]+=v;
}
}
inline int get_ans(int x)
{
int re=0;
for(int i=x;i;i-=i&-i)
if(time_clock[i]==tt) re+=c[i];
return re;
}
void add_point(int x,int y,int val)
{
q[++top].x=x;
q[top].y=y;
q[top].val=val;
q[top].num=top;
if(val==QAQ) q[top].ans+=y*x*w;
}
void CDQ(int l,int r)
{
if(l==r) return;
int mid=l+r>>1;
int ll=l;
int rr=mid+1;
for(int i=l;i<=r;i++)
{
if(q[i].num<=mid) nq[ll++]=q[i];
else nq[rr++]=q[i];
}
memcpy(q+l,nq+l,sizeof(q[0])*(r-l+1));
CDQ(l,mid);
CDQ(mid+1,r);
tt++;
int j=l;
for(int i=mid+1;i<=r;i++)
{
while(q[i].x>=q[j].x && j<=mid)
{
if(q[j].val!=QAQ) add_val(q[j].y,q[j].val);
j++;
}
if(q[i].val==QAQ) q[i].ans+=get_ans(q[i].y);
}
int l1=l,l2=mid+1;
for(int i=l;i<=r;i++)
{
if(q[l1]<q[l2]&&l1<=mid||l2>r)
nq[i]=q[l1++];
else
nq[i]=q[l2++];
}
memcpy(q+l,nq+l,sizeof(q[0])*(r-l+1));
}
int main()
{
scanf("%d%d",&w,&n);
int lei;
int x,y,val;
while(1)
{
scanf("%d",&lei);
if(lei==3) break;
if(lei==1)
{
scanf("%d%d%d",&x,&y,&val);
add_point(x,y,val);
}
if(lei==2)
{
int x1,y1,x2,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
add_point(x2,y2,QAQ);
add_point(x1-1,y1-1,QAQ);
add_point(x1-1,y2,QAQ);
add_point(x2,y1-1,QAQ);
}
}
sort(q+1,q+1+top,cmp2);
CDQ(1,top);
sort(q+1,q+1+top,cmp1);
for(int i=1;i<=top;i++)
{
if(q[i].val==QAQ)
{
printf("%d\n",q[i].ans+q[i+1].ans-q[i+2].ans-q[i+3].ans);
i+=3;
}
}
return 0;
}