BZOJ 2683: 简单题【CDQ分治 + 树状数组

80 篇文章 0 订阅
4 篇文章 0 订阅

……今天终于学了CDQ分治……感觉挺有趣


T+WA*2

T:强行把nlog^2 写成 n^2log,2333

WA1:按照x和y排序的时候,忘了x相同的应该是先修改再询问

WA2(不白,不膜,不清真(?)):………………我…………排序…………数组从1开始的,然而sort(tmp,tmp+cnt_tmp,cmp);………………没看出来2333


……顺便吐槽  这个题解 的代码似乎有 bug? 我粘下来对拍 一拍就WA…………开始怀疑人生忽然发现……我的和CA爷的程序似乎跑出来一样???

#include<bits/stdc++.h>
#define MAXN 800005
using namespace std;	int N;
inline int read(){
	register char ch = getchar();
	while(!isdigit(ch))	ch = getchar();
	register int rtn = 0;
	while(isdigit(ch))	rtn = rtn*10 + ch - '0' , ch = getchar();
	return rtn;
}
//============
int ned[MAXN];
int out[MAXN] , cnt_out;
struct t1{
	int typ;	// 0 for modify ;  1 for add ;  -1 for minus
	int x,y;
	int A;		// if is modify
	int id;		// id of action
	int out_id;
	t1(){}
	t1(int typ,int x,int y,int A,int id,int out_id):
		typ(typ),x(x),y(y),A(A),id(id),out_id(out_id){}
}rec_act[MAXN] , tmp[MAXN];	int cnt_act , cnt_tmp;

inline bool cmp_xyt(const t1 &a , const t1 &b){
	if(a.x == b.x){
		if(!a.typ)	return 1;
		if(!b.typ)	return 0;
		return 0;
	}
	return a.x < b.x;
}
//=================

int sum[500010];
inline void add(int pos,int x){
	for(register int i=pos;i<=N;i += i&-i)
		sum[i] += x;
}
inline int ask(int pos){
	int rtn = 0;
	for(register int i=pos;i;i -= i&-i)
		rtn += sum[i];
	return rtn;
}
//============

void CDQ(int l,int r){
//	printf("l = %d    r = %d\n",l,r);
	if(l==r)	return ;
	int mid = (l+r)>>1;
	
	cnt_tmp = 0;
	for(int i=l;i<=mid;++i)
		if(!rec_act[i].typ)
			tmp[++cnt_tmp] = rec_act[i];
	for(int i = mid+1;i<=r;++i)
		if(rec_act[i].typ!=0)
			tmp[++cnt_tmp] = rec_act[i];

	sort(tmp+1,tmp+cnt_tmp+1,cmp_xyt);
	
	for(int i=1;i<=cnt_tmp;++i){
		if(!tmp[i].typ)	add(tmp[i].y,tmp[i].A);
		else	out[tmp[i].out_id] += tmp[i].typ * ask(tmp[i].y);
	}
	for(int i=1;i<=cnt_tmp;++i){
		if(!tmp[i].typ)	add(tmp[i].y,-tmp[i].A);
	}
	CDQ(l,mid) , CDQ(mid+1,r);
}

int main(){
//	freopen("1.in","r",stdin);
//	freopen("1.out","w",stdout);
	N = read()+1;

	for(int opt = read(); opt ^ 3 ; opt = read()){
		++cnt_out;
		if(opt == 2)	ned[cnt_out] = 1;
		if(opt == 1){
			int xx = read()+1 , yy = read()+1 , k = read();
			++cnt_act;
			rec_act[cnt_act] = t1(0,xx,yy,k,cnt_act,cnt_out);
		}else{
			int xx1 = read()+1 , yy1 = read()+1 , xx2 = read()+1 , yy2 = read()+1;
			++cnt_act , rec_act[cnt_act] = t1(1,xx1 - 1,yy1 - 1,0,cnt_act,cnt_out);
			++cnt_act , rec_act[cnt_act] = t1(1,xx2,yy2,0,cnt_act,cnt_out);
			++cnt_act , rec_act[cnt_act] = t1(-1,xx1 - 1,yy2,0,cnt_act,cnt_out);
			++cnt_act , rec_act[cnt_act] = t1(-1,xx2,yy1 - 1,0,cnt_act,cnt_out);
		}
	}

	CDQ(1,cnt_act);

	for(int i=1;i<=cnt_out;++i)
		if(ned[i])	printf("%d\n",out[i]);
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值