【CDQ分治】BZOJ2683 简单题

题面在这里

把每个询问操作Q分为4个(容斥)

然后对于每个Q,要求出 t A < t Q , x A ≤ x Q , y A ≤ y Q t_A<t_Q,x_A\le x_Q,y_A\le y_Q tA<tQ,xAxQ,yAyQ w A w_A wA之和

直接CDQ分治就好了

示例程序:

#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
inline char nc(){
	static char buf[100000],*p1=buf,*p2=buf;
	return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline int red(){
	int res=0,f=1;char ch=nc();
	while (ch<'0'||'9'<ch) {if (ch=='-') f=-f;ch=nc();}
	while ('0'<=ch&&ch<='9') res=res*10+ch-48,ch=nc();
	return res*f;
}

const int maxn=800005;
int n,q;
bool vis[maxn];
struct data{
	int t,x,y,w;
	inline bool operator<(const data&b)const {return x<b.x || x==b.x&&y<b.y || x==b.x&&y==b.y&&t<b.t;}
}a[maxn],t[maxn];
#define lowbit(x) ((x)&-(x))
ll BIT[maxn],ans[maxn];
inline void ist(int x,int w){
	for (int i=x;i<=n;i+=lowbit(i)) BIT[i]+=w;
}
inline ll ask(int x){
	ll res=0;
	for (int i=x;i;i-=lowbit(i)) res+=BIT[i];
	return res;
}
void CDQ(int l,int r){
	if (l==r) return;
	int mid=l+r>>1;
	CDQ(l,mid);CDQ(mid+1,r);
	for (int i=l;i<=r;i++){
		t[i]=a[i];
		if (i<=mid) t[i].t=0;
	}
	sort(t+l,t+1+r);
	for (int i=l;i<=r;i++){
		if (!t[i].t&&t[i].w) ist(t[i].y,t[i].w);
		if (t[i].t&&!t[i].w) ans[t[i].t]+=ask(t[i].y);
	}
	for (int i=l;i<=r;i++) if (!t[i].t&&t[i].w) ist(t[i].y,-t[i].w);
}
int main(){
	n=red()+1;
	for (int c=red();c!=3;c=red())
	 if (c==1){
	 	a[++q].x=red()+1,a[q].y=red()+1;
	 	a[q].t=q;a[q].w=red();vis[q]=1;
	 }else{
	 	int x=red()+1,y=red()+1,xx=red()+1,yy=red()+1;
	 	a[++q].x=xx;a[q].y=yy;a[q].t=q;
	 	a[++q].x=xx;a[q].y=y-1;a[q].t=q;
	 	a[++q].x=x-1;a[q].y=yy;a[q].t=q;
	 	a[++q].x=x-1;a[q].y=y-1;a[q].t=q;
	 }
	CDQ(1,q);
	for (int i=1;i<=q;i++)
	 if (!vis[i]) printf("%lld\n",ans[i]-ans[i+1]-ans[i+2]+ans[i+3]),i+=3;
	return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值