bzoj3290 Theresa与数据结构

题目大意:

需要维护一个数据结构,支持

1.插入点\((x, y, z)\)

2.查询立方体\((x, y, z)\)\((x + r, y + r, z + r)\)中有多少个点

3.删除点\((x, y, z)\)


反正就是四维偏序,怎么开心怎么来

然后我明白了一件事

树套树的空间根本无法接受

以下代码在\(bzoj\)\(CE\), 然而即使不\(CE\)也会\(RE\)

大家看看就好......QAQ

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

#define ri register int
#define rep(io, st, ed)  for(ri io = st; io <= ed; io ++)
#define drep(io, ed, st) for(ri io = ed; io >= st; io --)

#define gc getchar
inline int read() {
	int p = 0, w = 1; char c = gc();
	while(c > '9' || c < '0') { if(c == '-') w = -1; c = gc(); }
	while(c >= '0' && c <= '9') p = p * 10 + c - '0', c = gc();
	return p * w;
}

const int sid = 500050;
const int eid = 3e7 + 5;

int n, q, qx, qy, qz, to;
int A[sid], X[sid], Y[sid], Z[sid], R[sid], Q[sid];
int Tx[sid], Ty[sid], Tz[sid];

int id;
int rt[sid], yrt[eid], ls[eid], rs[eid], sum[eid];

inline void mdf(int &o, int l, int r, int z, int c) {
	if(!o) o = ++ id;
	sum[o] += c;
	if(l == r) return;
	int mid = (l + r) >> 1;
	if(z <= mid) mdf(ls[o], l, mid, z, c);
	else mdf(rs[o], mid + 1, r, z, c);
}

inline void mdf(int &o, int l, int r, int y, int z, int c) {
	if(!o) o = ++ id;
	mdf(yrt[o], 1, qz, z, c);
	if(l == r) return;
	int mid = (l + r) >> 1;
	if(y <= mid) mdf(ls[o], l, mid, y, z, c);
	else mdf(rs[o], mid + 1, r, y, z, c);
}

inline int qry(int o, int l, int r, int zl, int zr) {
	if(zl > r || zr < l || !o) return 0;
	if(zl <= l && zr >= r) return sum[o];
	int mid = (l + r) >> 1;
	return qry(ls[o], l, mid, zl, zr) + qry(rs[o], mid + 1, r, zl, zr);
}

inline int qry(int o, int l, int r, int yl, int yr, int zl, int zr) {
	if(yl > r || yr < l || !o) return 0;
	if(yl <= l && yr >= r) return qry(yrt[o], 1, qz, zl, zr);
	int mid = (l + r) >> 1;
	return qry(ls[o], l, mid, yl, yr, zl, zr) + qry(rs[o], mid + 1, r, yl, yr, zl, zr);
}

inline void ins(int x, int y, int z, int opt) {
	for(ri i = x; i <= qx; i += i & (-i))
		mdf(rt[i], 1, qy, y, z, opt);
}

inline int ask(int x, int yl, int yr, int zl, int zr) {
	int ret = 0;
	for(ri i = x; i; i -= i & (-i))
		ret += qry(rt[i], 1, qy, yl, yr, zl, zr);
	return ret;
}

int main() {
	freopen("3290.in", "r", stdin);
	freopen("3290.out", "w", stdout);
	n = read();
	rep(i, 1, n) {
		X[i] = read(); Y[i] = read(); Z[i] = read();
		Tx[++ qx] = X[i]; Ty[++ qy] = Y[i]; Tz[++ qz] = Z[i];
	}
	q = read();
	static char s[50];
	rep(i, n + 1, n + q) {
		scanf("%s", s + 1);
		if(s[1] == 'A') {
			A[i] = 1; Q[++ to] = i;
			X[i] = read(); Y[i] = read(); Z[i] = read(); 
			Tx[++ qx] = X[i]; Ty[++ qy] = Y[i]; Tz[++ qz] = Z[i];
		}
		else if(s[1] == 'Q') {
			A[i] = 2;
			X[i] = read(); Y[i] = read(); 
			Z[i] = read(); R[i] = read();
			Tx[++ qx] = X[i]; Tx[++ qx] = X[i] + R[i];
			Ty[++ qy] = Y[i]; Ty[++ qy] = Y[i] + R[i];
			Tz[++ qz] = Z[i]; Tz[++ qz] = Z[i] + R[i];
		}
		else A[i] = 3 + Q[to --];
	}
	sort(Tx + 1, Tx + qx + 1);
	sort(Ty + 1, Ty + qy + 1);
	sort(Tz + 1, Tz + qz + 1);
	qx = unique(Tx + 1, Tx + qx + 1) - Tx - 1;
	qy = unique(Ty + 1, Ty + qy + 1) - Ty - 1;
	qz = unique(Tz + 1, Tz + qz + 1) - Tz - 1;
	rep(i, 1, n) {
		X[i] = lower_bound(Tx + 1, Tx + qx + 1, X[i]) - Tx;
		Y[i] = lower_bound(Ty + 1, Ty + qy + 1, Y[i]) - Ty;
		Z[i] = lower_bound(Tz + 1, Tz + qz + 1, Z[i]) - Tz;
		ins(X[i], Y[i], Z[i], 1);
	}
	rep(i, n + 1, n + q) {
		if(A[i] == 2) {
			int Lx = lower_bound(Tx + 1, Tx + qx + 1, X[i]) - Tx;
			int Rx = lower_bound(Tx + 1, Tx + qx + 1, X[i] + R[i]) - Tx;
			int Ly = lower_bound(Ty + 1, Ty + qy + 1, Y[i]) - Ty;
			int Ry = lower_bound(Ty + 1, Ty + qy + 1, Y[i] + R[i]) - Ty;
			int Lz = lower_bound(Tz + 1, Tz + qz + 1, Z[i]) - Tz;
			int Rz = lower_bound(Tz + 1, Tz + qz + 1, Z[i] + R[i]) - Tz;
			printf("%d\n", ask(Rx, Ly, Ry, Lz, Rz) - ask(Lx - 1, Ly, Ry, Lz, Rz));
		}
		else if(A[i] == 1) {
			X[i] = lower_bound(Tx + 1, Tx + qx + 1, X[i]) - Tx;
			Y[i] = lower_bound(Ty + 1, Ty + qy + 1, Y[i]) - Ty;
			Z[i] = lower_bound(Tz + 1, Tz + qz + 1, Z[i]) - Tz;
			ins(X[i], Y[i], Z[i], 1);
		}
		else {
			int p = A[i] - 3;
			ins(X[p], Y[p], Z[p], -1);
		}
	}
	return 0;
}
posted @ 2018-12-10 21:46  remoon  阅读(262)  评论(0编辑  收藏  举报