【P4390Mokia 摩基亚】CDQ分治70分WA求调
查看原帖
【P4390Mokia 摩基亚】CDQ分治70分WA求调
792748
封禁用户楼主2023/3/29 15:32
#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>

using namespace std;

const int N = 200010, V = 2000010;

struct OPT {
	int type, x, y, time;
	int res, z;
}a[N], tmp[N];

struct Qeury {
	int x1, y1;
	int x2, y2;
	int time;
}q[N];

int n;
int sz;

int cnt;

void input() {
	int opt, x1, y1, x2, y2, x;
	cin >> opt >> sz;
	for (int i = 1; ; i++) {
		cin >> opt;
		if (opt == 1) {
			cin >> x1 >> y1 >> x;
			a[++n] = {opt, x1, y1, i, 0, x}; 
		}
		else if (opt == 2) {
			cin >> x1 >> y1 >> x2 >> y2;
			q[++cnt] = {x1, y1, x2, y2, i};
			a[++n] = {opt, x2, y2, i, 0, 0};
			a[++n] = {opt, x2, y1 - 1, i, 0, 0};
			a[++n] = {opt, x1 - 1, y2, i, 0, 0};
			a[++n] = {opt, x1 - 1, y1 - 1, i, 0, 0};
		}
		else break;
	}
}

bool cmp(const OPT a, const OPT b) {
	if (a.x != b.x) return a.x < b.x;
	if (a.y != b.y) return a.y < b.y;
	if (a.type != b.type) return a.type < b.type;
	return a.time < b.time;
}

int tr[V];

void add(int u, int x) {
	while (u <= sz) {
		tr[u] += x;
		u += u & -u;
	}
}

int query(int u) {
	int res = 0;
	while (u) {
		res += tr[u];
		u -= u & -u;
	}
	return res;
}

void cdq(int l, int r) {
	if (l == r) return;
	
	int mid = (l + r) / 2;
	cdq(l, mid);
	cdq(mid + 1, r);
	
	int p = l, q = mid + 1, tot = l;
	while (p <= mid && q <= r) {
		if (a[p].y <= a[q].y) {
			if (a[p].type == 1) add(a[p].time, a[p].z);
			tmp[tot++] = a[p++];
		}
		else {
			if (a[q].type == 2) a[q].res += query(a[q].time);
			tmp[tot++] = a[q++];
		}
	}
	while (p <= mid) {
		if (a[p].type == 1) add(a[p].time, a[p].z);
		tmp[tot++] = a[p++];
	}
	while (q <= r) {
		if (a[q].type == 2) a[q].res += query(a[q].time);
		tmp[tot++] = a[q++];
	}
	for (int i = l; i <= mid; i++) {
		if (a[i].type == 1)
		add(a[i].time, -a[i].z);
	}
	for (int i = l; i <= r; i++) a[i] = tmp[i];
}

unordered_map<int, unordered_map<int, unordered_map<int, int> > > um;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	input();
	sort(a + 1, a + n + 1, cmp);
	cdq(1, n);
	for (int i = 1; i <= n; i++) {
		um[a[i].x][a[i].y][a[i].time] = a[i].res;
	}
	for (int i = 1; i <= cnt; i++) {
		int x1 = q[i].x1, y1 = q[i].y1;
		int x2 = q[i].x2, y2 = q[i].y2;
		int t = q[i].time;
		int ans = um[x2][y2][t] - um[x2][y1 - 1][t] - um[x1 - 1][y2][t] + um[x1 - 1][y1 - 1][t];
		cout << ans << '\n';
	}
	return 0;
}
2023/3/29 15:32
加载中...