/* I will wait for you */
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<iostream>
#include<fstream>
#include<vector>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<string>
#define make make_pair
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
const int maxn=2000010;
const int maxm=1010;
const int maxs=26;
const int INF=1<<29;
const int P=1000000007;
const double error=1e-9;
struct que{ int x,y,ty,val,id,pos; }a[maxn];
int p,n,cnt,clo,ans[maxn],s[maxn];
bool cmp(que a,que b)
{
return a.x<b.x||a.x==b.x&&a.y<b.y||a.x==b.x&&a.y==b.y&&a.ty<b.ty;
}
void ins_query()
{
int x1,x2,y1,y2;
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
ans[++clo]=(x2-x1+1)*(y2-y1+1)*p;
a[++cnt]=(que){x1-1,y1-1,1,1,cnt,clo};
a[++cnt]=(que){x2,y2,1,1,cnt,clo};
a[++cnt]=(que){x1-1,y2,1,-1,cnt,clo};
a[++cnt]=(que){x2,y1-1,1,-1,cnt,clo};
}
void ins(int x,int c)
{
for(;x<=n;x+=x&(-x)) s[x]+=c;
}
int sum(int x)
{
int f=0;for(;x;x-=x&(-x)) f+=s[x];return f;
}
void cdq(int l,int r)
{
if(l!=r)
{
int mid=(l+r)>>1;
cdq(l,mid);cdq(mid+1,r);
sort(a+l,a+r+1,cmp);
for(int i=l;i<=r;i++)
{
if(!a[i].ty&&a[i].id<=mid) ins(a[i].y,a[i].val);
if(a[i].ty&&a[i].id>mid) ans[a[i].pos]+=a[i].val*sum(a[i].y);
}
for(int i=l;i<=r;i++) if(!a[i].ty&&a[i].id<=mid) ins(a[i].y,-a[i].val);
}
}
int main()
{
int t,x,y,c;scanf("%d%d",&p,&n);
while(scanf("%d",&t)==1&&t!=3)
{
if(t==1) scanf("%d%d%d",&x,&y,&c),a[++cnt]=(que){x,y,0,c,cnt};
if(t==2) ins_query();
}
cdq(1,cnt);
for(int i=1;i<=clo;i++) printf("%d\n",ans[i]);
return 0;
}
BZOJ1176【CDQ分治】【树状数组】
最新推荐文章于 2020-01-17 16:01:25 发布