P2163 [SHOI2007] 园丁的烦恼

中学的时候年轻气盛,应该拿主席树把这道题艹过去了。正好复习离线二维数点,再做了一遍。

我们把询问差分安排到x轴上,然后y轴用树状数组统计即可。注意到此题要离散化,但是询问中的x可能不在原序列中。不过这不要紧,记得二分完减一就好。

const int N = 5e5 + 5, S = 1e7 + 5;
int n, m, t, ans[N], x[N];
pair <int, int> a[N];

struct Query {
	int p, q, x, y;
	Query(int p = 0, int q = 0, int x = 0, int y = 0) : p(p), q(q), x(x), y(y) {}
};

int c[S];
inline void update(int pos, int val) {
	for (; pos < N; pos += lowbit(pos)) c[pos] += val;
}

inline int query(int pos) {
	int ret = 0;
	for (; pos; pos -= lowbit(pos)) ret += c[pos];
	return ret;
}

vector <Query> v[N];

signed main(void) {
	read(n), read(m);
	for (int i = 1; i <= n; i++) read(a[i].first), read(a[i].second), x[i] = a[i].first;
	sort(a + 1, a + n + 1); sort(x + 1, x + n + 1); t = unique(x + 1, x + n + 1) - (x + 1);
	
	for (int i = 1; i <= m; i++) {
		int l, r, _x, _y;
		read(l), read(_x), read(r), read(_y);
		int _l = lower_bound(x + 1, x + t + 1, l) - x - 1;
		int _r = upper_bound(x + 1, x + t + 1, r) - x - 1;
		v[_l].push_back(Query(i, -1, _x, _y));
		v[_r].push_back(Query(i, 1, _x, _y));
	}
	
	for (int i = 1, j = 1; i <= t; i++) {
		while (a[j].first == x[i] && j <= n) update(a[j].second + 1, 1), j++;
		for (auto u : v[i]) ans[u.p] -= u.q * query(u.x), ans[u.p] += u.q * query(u.y + 1);
	}
	
	for (int i = 1; i <= m; i++) writeln(ans[i]);
	//fwrite(pf, 1, o1 - pf, stdout);
	return 0;
}
posted @   EternalEpic  阅读(66)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示