首页 > 其他分享 >CF1062E Company

CF1062E Company

时间:2022-10-25 15:35:43浏览次数:66  
标签:pre fr int Company tree CF1062E include ca


题目链接:​​传送门​​ 翻译那边有

要知道
树上一个区间的公共lca是区间dfs序的最小值和最大值对应的两个点的lca
证明可以去网上找
删掉dfs最大或最小的点
然后再通过一次dfs序最大值最小值找出相应的次大和次小
所以只要找出dfs序的最大次大和最小次小就可以了
线段树维护一下
有些小细节

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <complex>
#include <algorithm>
#include <climits>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define
#define

using namespace std;
typedef long long ll;
struct node {
int next, to;
}e[A];
int head[A], num;
void add(int fr, int to) {
e[++num].next = head[fr];
e[num].to = to;
head[fr] = num;
}
int dfn[A], dep[A], siz[A], son[A], fa[A], top[A], cnt, pre[A], dfnn[A], cntt;
int n, m, a, b, c;
namespace Seg {
struct node {
int l, r, w, x, d;
}tree[A];
void up(int k) {
tree[k].x = min(tree[k << 1].x, tree[k << 1 | 1].x);
tree[k].d = max(tree[k << 1].d, tree[k << 1 | 1].d);
}
void build(int k, int l, int r) {
tree[k].l = l; tree[k].r = r; tree[k].x = 0x3f3f3f3f; tree[k].d = -0x3f3f3f3f;
if (l == r) {
tree[k].x = tree[k].d = dfn[l];
return;
}
int m = (l + r) >> 1;
build(k << 1, l, m);
build(k << 1 | 1, m + 1, r);
up(k);
}
int askmax(int k, int a, int b) {
if (tree[k].l >= a and tree[k].r <= b) return tree[k].d;
int m = (tree[k].l + tree[k].r) >> 1, ans = 0;
if (a <= m) ans = max(ans, askmax(k << 1, a, b));
if (b > m) ans = max(ans, askmax(k << 1 | 1, a, b));
return ans;
}
int askmin(int k, int a, int b) {
if (tree[k].l >= a and tree[k].r <= b) return tree[k].x;
int m = (tree[k].l + tree[k].r) >> 1, ans = 0x3f3f3f3f;
if (a <= m) ans = min(ans, askmin(k << 1, a, b));
if (b > m) ans = min(ans, askmin(k << 1 | 1, a, b));
return ans;
}
}
void prepare(int fr) {
siz[fr] = 1; dfn[fr] = ++cnt; pre[cnt] = fr;
for (int i = head[fr]; i; i = e[i].next) {
int ca = e[i].to;
if (ca == fa[fr]) continue;
fa[ca] = fr;
dep[ca] = dep[fr] + 1;
prepare(ca);
siz[fr] += siz[ca];
if (siz[ca] > siz[son[fr]]) son[fr] = ca;
}
}
void dfs(int fr, int tp) {
top[fr] = tp;
if (!son[fr]) return; dfs(son[fr], tp);
for (int i = head[fr]; i; i = e[i].next) {
int ca = e[i].to;
if (ca == fa[fr] or ca == son[fr]) continue;
dfs(ca, ca);
}
}
int lca(int x, int y) {
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) swap(x, y);
x = fa[top[x]];
}
return dep[x] < dep[y] ? x : y;
}

int main(int argc, char const *argv[]) {
scanf("%d%d", &n, &m);
for (int i = 2; i <= n; i++) scanf("%d", &a), add(a, i), add(i, a);
prepare(1); dfs(1, 1); Seg::build(1, 1, n);
while (m--) {
scanf("%d%d", &a, &b);
int dl = Seg::askmin(1, a, b), dr = Seg::askmax(1, a, b);
int dfn1 = min(Seg::askmin(1, a, pre[dl] - 1), Seg::askmin(1, pre[dl] + 1, b)); //删掉这个点
int dfn2 = max(Seg::askmax(1, a, pre[dr] - 1), Seg::askmax(1, pre[dr] + 1, b)); //删掉这个点
int LCA1 = lca(pre[dl], pre[dfn2]), LCA2 = lca(pre[dr], pre[dfn1]); //注意用pre找到对应节点编号
if (dep[LCA1] > dep[LCA2]) printf("%d %d\n", pre[dr], dep[LCA1]);
else printf("%d %d\n", pre[dl], dep[LCA2]);
}
}


标签:pre,fr,int,Company,tree,CF1062E,include,ca
From: https://blog.51cto.com/lyle/5794779

相关文章