BZOJ 1604: [Usaco2008 Open]Cow Neighborhoods 奶牛的邻居

曼哈顿距离转切比雪夫距离
把坐标变为 \((x+y,x-y)\) 之后,求两点之间距离即为 \(\max(|x_1-x_2|,|y_1-y_2|)\)
之后再通过扫描线,平衡树维护 \(y\) 坐标,每次将前驱后继进行合并

#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define pii pair<int, int>
#define pli pair<ll, int>
#define lp p << 1
#define rp p << 1 | 1
#define mid ((l + r) / 2)
#define lowbit(i) ((i) & (-i))
#define ll long long
#define ull unsigned long long
#define db double
#define rep(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=b-1;i>=a;i--)
#define Edg int ccnt=1,head[N],to[E],ne[E];void addd(int u,int v){to[++ccnt]=v;ne[ccnt]=head[u];head[u]=ccnt;}void add(int u,int v){addd(u,v);addd(v,u);}
#define Edgc int ccnt=1,head[N],to[E],ne[E],c[E];void addd(int u,int v,int w){to[++ccnt]=v;ne[ccnt]=head[u];c[ccnt]=w;head[u]=ccnt;}void add(int u,int v,int w){addd(u,v,w);addd(v,u,w);}
#define es(u,i,v) for(int i=head[u],v=to[i];i;i=ne[i],v=to[i])
const int MOD = 1e9 + 7;
void M(int &x) {if (x >= MOD)x -= MOD; if (x < 0)x += MOD;}
int qp(int a, int b = MOD - 2) {int ans = 1; for (; b; a = 1LL * a * a % MOD, b >>= 1)if (b & 1)ans = 1LL * ans * a % MOD; return ans % MOD;}
template<class T>T gcd(T a, T b) { while (b) { a %= b; std::swap(a, b); } return a; }
template<class T>bool chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; }
template<class T>bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
char buf[1 << 21], *p1 = buf, *p2 = buf;
inline char getc() {
    return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++;
}
inline int _() {
    int x = 0, f = 1; char ch = getc();
    while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getc(); }
    while (ch >= '0' && ch <= '9') { x = x * 10ll + ch - 48; ch = getc(); }
    return x * f;
}

const int N = 1e5 + 7, E = 4e5 + 7;
int n, C, fa[N], que[N], col[N], ans;
inline int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
void merge(int x, int y) {
    x = find(x), y = find(y);
    if (x == y) return;
    fa[x] = y; ans--;
}
struct P {
    int x, y, id;
    P() {}
    P(int x, int y, int id): x(x), y(y), id(id) {}
    void read() { int a = _(), b = _(); this->x = a + b, this->y = a - b; }
} p[N];
bool operator < (const P &a, const P &b) { return a.y < b.y; }
std::multiset<P> st;
std::multiset<P>::iterator it;
bool cmp(const P &a, const P &b) { return a.x < b.x; }

int main() {
#ifdef LOCAL
    freopen("ans.out", "w", stdout);
#endif
    n = _(), C = _();
    ans = n;
    rep (i, 0, n) p[i].read(), p[i].id = fa[i] = i;
    std::sort(p, p + n, cmp);
    int l = 0;
    st.insert(p[0]);
    rep (i, 1, n) {
        while (p[i].x - p[l].x > C) {
            st.erase(st.find(p[l]));
            l++;
        }
        it = st.lower_bound(p[i]);
        if (it != st.end()) {
            if (std::abs(p[i].y - (it->y)) <= C)
                merge(p[i].id, it->id);
        }
        if (it != st.begin()) {
            --it;
            if (std::abs(p[i].y - (it->y)) <= C)
                merge(p[i].id, it->id);
        }
        st.insert(p[i]);
    }
    int mx = 0;
    rep (i, 0, n) col[find(i)]++, chkmax(mx, col[find(i)]);
    printf("%d %d\n", ans, mx);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Mrzdtz220/p/12409936.html