You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
2.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// http://acm.hdu.edu.cn/showproblem.php?pid=4007
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int N = 40010;
struct Node {
int l, r, lazy;
int maxc;
} tr[N << 2];
struct Seg {
int x, y1, y2, c;
bool operator<(const Seg &t) const {
if (x == t.x) return c > t.c;
return x < t.x;
}
} seg[N << 1];
void pushup(int u) {
tr[u].maxc = max(tr[u << 1].maxc, tr[u << 1 | 1].maxc);
}
void pushdown(int u) {
if (tr[u].lazy) {
tr[u << 1].lazy += tr[u].lazy;
tr[u << 1 | 1].lazy += tr[u].lazy;
tr[u << 1].maxc += tr[u].lazy;
tr[u << 1 | 1].maxc += tr[u].lazy;
tr[u].lazy = 0;
}
}
void build(int u, int l, int r) {
tr[u] = {l, r, 0, 0};
if (l == r) return;
int mid = (l + r) >> 1;
build(u << 1, l, mid);
build(u << 1 | 1, mid + 1, r);
}
void modify(int u, int l, int r, int v) {
if (l <= tr[u].l && tr[u].r <= r) {
tr[u].lazy += v;
tr[u].maxc += v;
return;
}
pushdown(u);
int mid = tr[u].l + tr[u].r >> 1;
if (l <= mid) modify(u << 1, l, r, v);
if (r > mid) modify(u << 1 | 1, l, r, v);
pushup(u);
}
//离散化数组
vector<int> ys;
int find(int x) {
return lower_bound(ys.begin(), ys.end(), x) - ys.begin();
}
int main() {
//加快读入
ios::sync_with_stdio(false), cin.tie(0);
int n, r;
while (cin >> n >> r) {
//多组数测数据
memset(tr, 0, sizeof tr);
ys.clear();
int idx = 0;
for (int i = 1; i <= n; i++) {
int x, y;
cin >> x >> y;
seg[++idx] = {x, y, y + r, 1};
seg[++idx] = {x + r, y, y + r, -1};
ys.push_back(y);
ys.push_back(y + r);
}
//对于边按x由小到大入边在前出边在后排序
sort(seg + 1, seg + 1 + idx);
//离散化=排序+ 去重
sort(ys.begin(), ys.end());
ys.erase(unique(ys.begin(), ys.end()), ys.end());
//离散化似乎应该成为必要的步骤,这样记忆起来省的麻烦,都是一样的套路才方便记忆
build(1, 0, ys.size() - 1); // 0~ys.size()-1,共 ys.size()个,线段树是建立在y轴的投影上
int ans = 0;
for (int i = 1; i <= idx; i++) {
modify(1, find(seg[i].y1), find(seg[i].y2), seg[i].c);
ans = max(ans, tr[1].maxc);
}
printf("%d\n", ans);
}
return 0;
}