题面:http://sycstudio.com/bzojch/p/2683.html
CDQ裸题。
上洛谷群借了个权限号AC了。
// It is made by XZZ
#include<cstdio>
#include<algorithm>
#define il inline
#define rg register
#define vd void
#define sta static
typedef long long ll;
il int gi(){
rg int x=0,f=1;rg char ch=getchar();
while(ch<'0'||ch>'9')f=ch=='-'?-1:f,ch=getchar();
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
return x*f;
}
const int maxn=500001,maxm=200001<<1;
struct yyb{int x,y,y2,id,sao;}s[maxm],_s[maxm];
int ans[maxn],n,m,q;
il bool cmp(const yyb&a,const yyb&b){return a.x==b.x?a.id==-1:a.x<b.x;}
namespace BIT{
int t[maxn];
il vd Upd(rg int r,int s){while(r<=n)t[r]+=s,r+=r&-r;}
il int query(rg int r){rg int ret=0;while(r)ret+=t[r],r-=r&-r;return ret;}
il int Query(const int&l,const int&r){return query(r)-query(l-1);}
}
il vd CDQ(int l,int r){
if(l==r)return;
int k=l-1,mid=(l+r)>>1,i=l,j=mid+1,_i=mid+1,_j=r+1;
CDQ(l,mid),CDQ(mid+1,r);
while((i^_i)&&(j^_j))
if(cmp(s[i],s[j])){
_s[++k]=s[i];
if(s[i].id==-1)BIT::Upd(s[i].y,s[i].sao);
++i;
}else{
_s[++k]=s[j];
if(s[j].id!=-1)ans[s[j].id]+=BIT::Query(s[j].y,s[j].y2)*s[j].sao;
++j;
}
while(j^_j){
_s[++k]=s[j];
if(s[j].id!=-1)ans[s[j].id]+=BIT::Query(s[j].y,s[j].y2)*s[j].sao;
++j;
}
for(rg int p=l;p<i;++p)if(s[p].id==-1)BIT::Upd(s[p].y,-s[p].sao);
while(i^_i)_s[++k]=s[i++];
for(rg int p=l;p<=r;++p)s[p]=_s[p];
}
int main(){
#ifdef xzz
freopen("in.in","r",stdin);
freopen("out.out","w",stdout);
#endif
n=gi();
int opt;
while(1){
opt=gi();
if(opt==3)break;
sta int x1,y1,x2,y2,A;
if(opt==1){
x1=gi(),y1=gi(),A=gi();
s[++m]=(yyb){x1,y1,0,-1,A};
}else{
x1=gi(),y1=gi(),x2=gi(),y2=gi();
s[++m]=(yyb){x2,y1,y2,++q,1};
s[++m]=(yyb){x1-1,y1,y2,q,-1};
}
}
CDQ(1,m);
for(rg int i=1;i<=q;++i)printf("%d
",ans[i]);
return 0;
}