三维数点的CDQ分治板子

int n, k, tot;
struct _ {int x,r,f;} a[N];
struct __ {
	int type;
	ll x,y;
	bool operator < (const __ & rhs) const {
		if (x!=rhs.x) return x<rhs.x;
		if (y!=rhs.y) return y<rhs.y;
		return type<rhs.type;
	}
} e[N];
ll ans, b[N];
int c[N];
inline void add(int x, int v) {
	for (; x<=*b; x+=x&-x) c[x]+=v;
}
inline int qry(int x) {
	int r = 0;
	for (; x; x^=x&-x) r+=c[x];
	return r;
}
inline void qry(ll x1, ll y1, ll x2, ll y2) {
	e[++tot] = {1,x2,y2};
	e[++tot] = {1,x1-1,y1-1};
	e[++tot] = {2,x1-1,y2};
	e[++tot] = {2,x2,y1-1};
	b[++*b]=y2, b[++*b]=y1-1;
}
inline void ins(int x, int y) {
	e[++tot] = {0,x,y};
}

void merge(int l, int r) {
	if (l==r) return;
	merge(l,mid),merge(mid+1,r);
	int now = l;
	REP(i,mid+1,r) {
		while (now<=mid&&e[now].x<=e[i].x) {
			if (e[now].type==0) add(e[now].y,1);
			++now;
		}
		if (e[i].type==1) ans+=qry(e[i].y);
		else if (e[i].type==2) ans-=qry(e[i].y);
	}
	while (now!=l) {
		if (e[--now].type==0) add(e[now].y,-1);
	}
	inplace_merge(e+l,e+mid+1,e+r+1);
}

int main() {
	REP(i,1,n) {
//		qry();
//		ins();
	}
	sort(b+1,b+1+*b),*b=unique(b+1,b+1+*b)-b-1;
	REP(i,1,tot) e[i].y=lower_bound(b+1,b+1+*b,e[i].y)-b;
	merge(1,tot);
	printf("%lld\n", ans);
}

 

转载于:https://www.cnblogs.com/uid001/p/10828193.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值