题解:P12180 DerrickLo's City (UBC002C)

· · 题解

这里提供无需树链剖分的赛时抽象做法。

显然这些城市构成一棵树,这里令 1 为根。

首先对于编号在 l\sim r 内的某一对点,如果它们成祖先关系,那么就相当于是将开会地址限制在这两个点互相到达的最简路径上,那么就有如下分讨:

考虑如何判断这些条件。

首先考虑如何判断是否存在编号在 l\sim r 内的某对点成祖先关系,我们可以对于树上的每个点 x,即为其所有祖先中编号最大的且比 x 小的点的编号,记为 pre_x,以及编号最小的且比 x 大的点的编号,记为 nxt_x,这个可以用在遍历树时用 set 维护。那么判断这个条件等价于判断是否存在一个 i,满足 l\le i\le r 且 pre_i\ge l 或者 nxt_i\le r。用 st 表维护区间最值即可。

其次考虑如何判断是否存在一个编号在 l\sim r 内的点是其他点的公共祖先。我们可以求出编号在 l\sim r 内的所有点的最近公共祖先,记为 lc,然后 lc 是否满足 l\le lc\le r 即可。

接着考虑如何判断编号为 l\sim r 的点(不包括 lc)是否都处于 lc 的某一个子树中。那么可以记录树上每个点的 dfn 序以及子树的大小,以此来确定 lc 的每个儿子的子树覆盖的点的 dfn 序的范围。然后求出编号为 l\sim lc-1 以及 lc+1\sim r 内的点的 dfn 序的最大值 kr 及最小值 kl,然后判断 lc 是否存在某个子树使得其覆盖的点的 dfn 序的范围包含 kl,kr。可以使用二分与 st 表判断。

最后判断编号为 l\sim r 的点(不包括 lc)是否互不成祖先关系,这个和第一点类似。

代码太丑了,见谅。

Code

#include <bits/stdc++.h>
using namespace std;
vector<int>g[100005];
int de[100005], fa[100005][25];

void dfs(int x, int f) {
    fa[x][0] = f;
    de[x] = de[f] + 1;
    for (int i = 1; i <= 20; i++)
        fa[x][i] = fa[fa[x][i - 1]][i - 1];
    for (int y : g[x])
        if (y != f)
            dfs(y, x);
}

int lca(int x, int y) {
    if (de[x] < de[y])
        swap(x, y);
    for (int i = 20; i >= 0; i--)
        if (de[fa[x][i]] >= de[y])
            x = fa[x][i];
    if (x == y)
        return x;
    for (int i = 20; i >= 0; i--)
        if (fa[x][i] != fa[y][i])
            x = fa[x][i], y = fa[y][i];
    return fa[x][0];
}
int st1[25][100005], pos[25][100005];
int n, q;

int ask1(int l, int r) {
    r--;
    if (l > r)
        return 1000000000;
    int x = r - l + 1;
    int lgg = log2(x);
    int ans = 0;
    ans = min(st1[lgg][l], st1[lgg][r - (1 << lgg) + 1]);
    if (ans == st1[lgg][l])
        return pos[lgg][l];
    else
        return pos[lgg][r - (1 << lgg) + 1];
}
set<int>now;
int nxt[100005], pre[100005], st2[25][100005], st3[25][100005]; //nxt 最大的比他小的 pre 最小的比他大的
int dfn[100005], cnt, st4[25][100005], sz[100005], st5[25][100005]; //st4 区间最小dfn序 st5 最大
vector<int>w[100005];

void df(int x, int f) {
    dfn[x] = ++cnt;
    sz[x] = 1;
    auto it = now.lower_bound(x);
    if (it != now.begin()) {
        it--;
        nxt[x] = *it;
    }
    it = now.upper_bound(x);
    if (it != now.end())
        pre[x] = *it;
    now.insert(x);
    for (int y : g[x]) {
        if (y != f) {
            df(y, x);
            sz[x] += sz[y];
            w[x].push_back(y);
        }
    }
    now.erase(x);
}

int ask2(int l, int r) {
    if (l > r)
        return -1000000000;
    int x = r - l + 1;
    int lgg = log2(x);
    int ans = 0;
    ans = max(st2[lgg][l], st2[lgg][r - (1 << lgg) + 1]);
    return ans;
}

int ask3(int l, int r) {
    if (l > r)
        return 1000000000;
    int x = r - l + 1;
    int lgg = log2(x);
    int ans = 0;
    ans = min(st3[lgg][l], st3[lgg][r - (1 << lgg) + 1]);
    return ans;
}

int ask4(int l, int r) {
    if (l > r)
        return -1000000000;
    int x = r - l + 1;
    int lgg = log2(x);
    int ans = 0;
    ans = max(st4[lgg][l], st4[lgg][r - (1 << lgg) + 1]);
    return ans;
}

int ask5(int l, int r) {
    if (l > r)
        return 1000000000;
    int x = r - l + 1;
    int lgg = log2(x);
    int ans = 0;
    ans = min(st5[lgg][l], st5[lgg][r - (1 << lgg) + 1]);
    return ans;
}

int main() {
    cin >> n >> q;
    for (int i = 1; i < n; i++) {
        int x, y;
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    dfs(1, 0);
    df(1, 0);
    memset(st1, 0x3f, sizeof(st1));
    memset(st3, 0x3f, sizeof(st3));
    memset(st2, -0x3f, sizeof(st2));
    memset(st4, 0x3f, sizeof(st4));
    memset(st5, -0x3f, sizeof(st5));
    for (int i = 1; i <= n; i++) {
        pos[0][i] = lca(i, i + 1);
        st1[0][i] = de[pos[0][i]];
        st2[0][i] = nxt[i];
        st3[0][i] = pre[i];
        st4[0][i] = st5[0][i] = dfn[i];
    }
    for (int i = 1; i <= 20; i++)
        for (int j = 1; j + (1 << i) - 1 <= n; j++) {
            st1[i][j] = min(st1[i - 1][j], st1[i - 1][j + (1 << i - 1)]);
            if (st1[i][j] == st1[i - 1][j])
                pos[i][j] = pos[i - 1][j];
            else
                pos[i][j] = pos[i - 1][j + (1 << i - 1)];
            st2[i][j] = max(st2[i - 1][j], st2[i - 1][j + (1 << i - 1)]);
            st3[i][j] = min(st3[i - 1][j], st3[i - 1][j + (1 << i - 1)]);
            st4[i][j] = min(st4[i - 1][j], st4[i - 1][j + (1 << i - 1)]);
            st5[i][j] = max(st5[i - 1][j], st5[i - 1][j + (1 << i - 1)]);
        }
    while (q--) {
        int l, r;
        cin >> l >> r;
        int lc = ask1(l, r);
        if (lc < l || lc > r) {
            int k1 = ask2(l, r), k2 = ask3(l, r);
            if ((l <= k1 && k1 <= r) || (l <= k2 && k2 <= r))
                cout << "No";
            else
                cout << "Yes";
        } else {
            int kl = max(ask4(l, lc - 1), ask4(lc + 1, r)), kr = min(ask5(l, lc - 1), ask5(lc + 1, r));
            int l = 0, r = w[lc].size() - 1, ans = 0;
            while (l <= r) {
                int mid = (l + r)
                          / 2;
                if (dfn[w[lc][mid]] > kr)
                    r = mid - 1;
                else if (dfn[w[lc][mid]] + sz[w[lc][mid]] - 1 < kl)
                    l = mid + 1;
                else {
                    ans = mid;
                    break;
                }
            }
            if (!ans)
                cout << "No";
            else {
                int k1 = max(ask2(l, lc - 1), ask2(lc + 1, r)), k2 = min(ask3(l, lc - 1), ask3(lc + 1, r));
                if ((l <= k1 && k1 <= r) || (l <= k2 && k2 <= r))
                    cout << "No";
                else
                    cout << "Yes";
            }
        }
        cout << "\n";
    }
}