题解:P12180 DerrickLo's City (UBC002C)
szh_AK_all · · 题解
这里提供无需树链剖分的赛时抽象做法。
显然这些城市构成一棵树,这里令
首先对于编号在
-
当编号在
l\sim r 内的任意一对点都不成祖先关系时,显然可以将开会地址定在1 号点,答案为Yes。 -
否则,必定有一个编号在
l\sim r 内的点是其他点的公共祖先并且这些点都处于该公共祖先的同一个子树内且互不成祖先关系才可能存在开会地址。
考虑如何判断这些条件。
首先考虑如何判断是否存在编号在
其次考虑如何判断是否存在一个编号在
接着考虑如何判断编号为
最后判断编号为
代码太丑了,见谅。
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";
}
}