E - Company

思路:

首先,求出每个点的dfs序

然后求一些点的公共lca, 就是求lca(u, v), 其中u是dfs序最大的点, v是dfs序最小的大点

证明: 假设o是这些点的公共lca, in[o]是o的入时间戳, out[o]是o的出时间戳,

那么对于任意一点x, in[o] <= in[v] <= in[x] <= in[u] <= out[o]

所以o是x的祖先

所以o是这些点的公共lca

所以只要求出每段区间的dfs序最大的和最小的, 删除的要么是最大的, 要么是最小的

可以删除后分成两段区间来考虑或者再求一下次大的和次小的来考虑

求次大次小可以用运算符重载写线段树, 要方便很多

代码:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize(4)
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pi acos(-1.0)
#define LL long long
//#define mp make_pair
#define pb push_back
#define ls rt<<1, l, m
#define rs rt<<1|1, m+1, r
#define ULL unsigned LL
#define pll pair<LL, LL>
#define pli pair<LL, int>
#define pii pair<int, int>
#define piii pair<pii, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fopen freopen("in.txt", "r", stdin);freopen("out.txt", "w", stout);
//head const int N = 1e5 + ;
vector<int> g[N];
int anc[N][], deep[N], dfn[N], tot = ;
struct node {
int mx, mn, _mx, _mn;
node operator ^ (const node & rhs) {
node res = rhs;
if(dfn[mx] >= dfn[res.mx]) {
res._mx = res.mx;
res.mx = mx;
}
else if(dfn[mx] > dfn[res._mx]) {
res._mx = mx;
} if(dfn[mn] <= dfn[res.mn]) {
res._mn = res.mn;
res.mn = mn;
}
else if(dfn[mn] < dfn[res._mn]) {
res._mn = mn;
} if(dfn[_mx] >= dfn[res.mx]) {
res._mx = res.mx;
res.mx = _mx;
}
else if(dfn[_mx] > dfn[res._mx]) {
res._mx = _mx;
} if(dfn[_mn] <= dfn[res.mn]) {
res._mn = res.mn;
res.mn = _mn;
}
else if(dfn[_mn] < dfn[res._mn]) {
res._mn = _mn;
}
return res;
}
}tree[N<<];
void dfs(int u, int o) {
anc[u][] = o;
for (int i = ; i < ; i++) anc[u][i] = anc[anc[u][i-]][i-];
deep[u] = deep[o] + ;
dfn[u] = ++tot;
for (int v : g[u]) {
if(v != o) {
dfs(v, u);
}
}
}
int lca(int u, int v) {
if(deep[u] < deep[v]) swap(u, v);
for (int i = ; i >= ; i--) if(deep[anc[u][i]] >= deep[v]) u = anc[u][i];
if(u == v) return u;
for (int i = ; i >= ; i--) if(anc[u][i] != anc[v][i]) u = anc[u][i], v = anc[v][i];
return anc[u][];
}
void push_up(int rt) {
tree[rt] = tree[rt<<] ^ tree[rt<<|];
}
void build(int rt, int l, int r) {
if(l == r) {
tree[rt] = node{l, l, , };
return ;
}
int m = l+r >> ;
build(ls);
build(rs);
push_up(rt);
}
node query(int L, int R, int rt, int l, int r) {
if(L <= l && r <= R) return tree[rt];
int m = l+r >> ;
node res = {, , , };
if(L <= m) res = query(L, R, ls);
if(R > m) {
if(res.mn == ) res = query(L, R, rs);
else res = res ^ query(L, R, rs);
}
return res;
}
int main() {
int n, p, q, l, r;
scanf("%d %d", &n, &q);
for (int i = ; i <= n; i++) {
scanf("%d", &p);
g[p].pb(i);
}
dfs(, );
dfn[] = ;
build(, , n);
for (int i = ; i <= q; i++) {
scanf("%d %d", &l, &r);
node tmp = query(l, r, , , n);
int l1 = lca(tmp.mx, tmp._mn), l2 = lca(tmp._mx, tmp.mn);
// cout << tmp.mx << " " << tmp._mx << " " << tmp.mn << " " << tmp._mn << endl;
// cout << dfn[tmp.mx] << " " << dfn[tmp._mx] << " " << dfn[tmp.mn] << " " << dfn[tmp._mn] << endl;
// cout << l1 << " " << l2 << endl;
if(deep[l1] < deep[l2]) printf("%d %d\n", tmp.mx, deep[l2]-);
else printf("%d %d\n", tmp.mn, deep[l1]-);
}
return ;
}

Codeforces 1062 E - Company的更多相关文章

  1. Codeforces 1062 - A/B/C/D/E - (Undone)

    链接:http://codeforces.com/contest/1062 A - Prank - [二分] 题意: 给出长度为 $n(1 \le n \le 100)$ 的数组 $a[1 \sim ...

  2. Codeforces 556D Restructuring Company

    传送门 D. Restructuring Company time limit per test 2 seconds memory limit per test 256 megabytes input ...

  3. [刷题]Codeforces 794C - Naming Company

    http://codeforces.com/contest/794/problem/C Description Oleg the client and Igor the analyst are goo ...

  4. CodeForces 125E MST Company

    E. MST Company time limit per test 8 seconds memory limit per test 256 megabytes input standard inpu ...

  5. CodeForces - 794C:Naming Company(博弈&简单贪心)

    Oleg the client and Igor the analyst are good friends. However, sometimes they argue over little thi ...

  6. CodeForces - 566D Restructuring Company 并查集的区间合并

    Restructuring Company Even the most successful company can go through a crisis period when you have ...

  7. CODEFORCES 125E MST Company 巧用Kruskal算法

    题意:给定一个带权边无向图,求最小生成树,且满足第一个节点的度为固定的k 无解则输出-1 数据规模: 节点数n和限制k<=5000 边数m<=10^5 时限8sec 思路: 首先时限比较宽 ...

  8. CodeForces 566D Restructuring Company (并查集+链表)

    题意:给定 3 种操作, 第一种 1 u v 把 u 和 v 合并 第二种 2 l r 把 l - r 这一段区间合并 第三种 3 u v 判断 u 和 v 是不是在同一集合中. 析:很容易知道是用并 ...

  9. Codeforces Round #321 (Div. 2) B. Kefa and Company 二分

    B. Kefa and Company Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/pr ...

随机推荐

  1. oracle 12.2 linux/solaris正式发布

    oracle 12.2 linux/solaris正式发布,可以从http://www.oracle.com/technetwork/database/enterprise-edition/downl ...

  2. Linear Regression with PyTorch

    Linear Regression with PyTorch Problem Description 初始化一组数据 \((x,y)\),使其满足这样的线性关系 \(y = w x + b\) .然后 ...

  3. echarts自定义图例legend文字和样式

    话不多说,先上效果图. 要完成这个图并不难,主要是下面那个图例比较难,需要定制. 让我们从官方文档找找思路,官方文档关于legend.formatter是这样的:链接在这 难点在于: 1.这里的图例文 ...

  4. bzoj 1014 火星人prefix - 链表 - 分块

    Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...

  5. shell 变量相关的命令

    变量="变量" readonly 变量名="变量"  表示设置该变量为只读变量 ,这个变量不能别改变 echo $变量名 set  显示本地所有的变量 unse ...

  6. WebSocket、Socket

    https://www.cnblogs.com/jingmoxukong/p/7755643.html#undefined WebSocket 详解教程 WebSocket介绍,与Socket的区别 ...

  7. error C4996: Function call with parameters that may be unsafe – this call relies on the caller to ch

    在加入QCustomplot时有如题的错误 1>c:\program files (x86)\microsoft visual studio11.0\vc\include\xutility(21 ...

  8. EGIT

    https://jingyan.baidu.com/article/64d05a0262f013de55f73bcc.html

  9. Bootstrap3基础 bg-danger/info... 辅助类样式 背景文本颜色

      内容 参数   OS   Windows 10 x64   browser   Firefox 65.0.2   framework     Bootstrap 3.3.7   editor    ...

  10. javascript中的高阶函数, 和 类定义Function, 和apply的使用

    参考: http://www.cnblogs.com/delin/archive/2010/06/17/1759695.html js中的类, 也是用function关键字来定义的: function ...