【ARC069F】Flags(二分,2-SAT,线段树优化建边)

版权声明:湘 ICP 证 66666666 号 | Copyright © 2002-2019, ♂Hany01♂, All Rights Reserved 难道会有人转蒟蒻的博文?? https://blog.csdn.net/hhaannyyii/article/details/82263423

Description

n 面flag,每个flag可以立在两个地方,求出一种方案使得flag之间的最小距离最大。


Solution

首先二分这个距离,然后用2-SAT检查方案是否可行。
这样最坏情况建出来的边是 n 2 的,用线段树优化建边即可。

所谓线段树优化建边,就是如果我要将序列的一个区间内的点全部连向另一个点时,我们在序列上建一棵线段树,父节点连向子节点。我们将区间在线段树查询,如果当前节点完全被区间包含,则从该节点向另一个点连边即可。


Code

/************************************************
 * Au: Hany01
 * Date: Aug 31st, 2018
 * Prob: ARC069F Flags
 * Email: [email protected] & [email protected]
 * Inst: Yali High School
************************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef long double LD;
typedef pair<int, int> PII;
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define SZ(a) ((int)(a).size())
#define ALL(a) a.begin(), a.end()
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia

template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline int read() {
    static int _, __; static char c_;
    for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

const int maxn = 8e4 + 5, maxm = 1e6;

int n, tot, v[maxm], nex[maxm], beg[maxn], e, nt, dfn[maxn], low[maxn], isin[maxn], cocnt, co[maxn], stk[maxn], top, tot_, clk, pos[maxn], lc[maxn], rc[maxn], rt;
PII p[maxn];

#define mid ((l + r) >> 1)

inline void add(int uu, int vv) { if (uu != vv) v[++ e] = vv, nex[e] = beg[uu], beg[uu] = e; }

void build(int& rt, int l, int r) {
    if (l == r) { rt = l; return; }
    rt = ++ tot, build(lc[rt], l, mid), build(rc[rt], mid + 1, r);
    add(lc[rt], rt), add(rc[rt], rt);
}

void Add(int t, int l, int r, int x, int y) {
    if (x <= p[l].x && p[r].x <= y && (pos[p[nt].y ^ 1] < l || pos[p[nt].y ^ 1] > r)) add(t, nt);
    else {
        if (l == r) return;
        if (p[mid].x >= x) Add(lc[t], l, mid, x, y);
        if (p[mid + 1].x <= y) Add(rc[t], mid + 1, r, x, y);
    }
}

void DFS(int u) {
    dfn[u] = low[u] = ++ clk, isin[u] = 1, stk[++ top] = u;
    for (register int i = beg[u]; i; i = nex[i]) {
        if (!dfn[v[i]]) DFS(v[i]), chkmin(low[u], low[v[i]]);
        else if (isin[v[i]]) chkmin(low[u], dfn[v[i]]);
    }
    if (dfn[u] == low[u]) {
        ++ cocnt;
        do isin[stk[top]] = 0, co[stk[top]] = cocnt; while (stk[top --] != u);
    }
}

int viss[maxn];
void Debug(int u, int pa = 0) {
    viss[u] = 1;
    for (register int i = beg[u]; i; i = nex[i])
        if (!viss[v[i]]) Debug(v[i], u);
}

inline bool check(int maxd) {
    e = 0, Set(beg, 0), tot = tot_, build(rt, 2, tot_);
    For(i, 2, tot_) {
        int l = p[i].x - maxd + 1, r = p[i].x + maxd - 1;
        nt = pos[p[i].y ^ 1];
        if (l <= r) Add(rt, 2, tot_, l, r);
    }
    Set(dfn, 0), clk = cocnt = 0;
    For(i, 2, tot_) if (!dfn[i]) DFS(i);
    Debug(2);
    For(i, 1, n) if (co[pos[i << 1]] == co[pos[i << 1 | 1]]) return 0;
    return 1;
}

#undef mid

int main()
{
#ifdef hany01
    freopen("arc069f.in", "r", stdin);
    freopen("arc069f.out", "w", stdout);
#endif

    tot = (n = read()) << 1 | 1;
    For(i, 1, n) p[i << 1] = mp(read(), i << 1), p[i << 1 | 1] = mp(read(), i << 1 | 1);
    sort(p + 2, p + 1 + tot), tot_ = tot;
    For(i, 2, tot) pos[p[i].y] = i;

    static int l = 0, r = p[tot].x, mid;
    while (l < r) if (check(mid = (l + r + 1) >> 1)) l = mid; else r = mid - 1;
    printf("%d\n", l);

    return 0;
}

猜你喜欢

转载自blog.csdn.net/hhaannyyii/article/details/82263423