给出一棵有根树,1为根结点,接下来q次询问,每次给出一个[l,r]区间,现在允许删掉[l,r]区间内任何一个点,使得所有点的最近公共祖先的深度尽可能大,问删掉的点是哪个点,深度最大是多少。

做法:

线段树维护区间dfs序的最大值,最小值。

首先,区间的LCA等价于区间dfs序的最小值和最大值对应的两个点的LCA。

然后,根据dfs序的性质,当删掉的点的dfs序最大或者最小时,对答案贡献最大。

所以接下来只要去查询区间里的LCA(最小值,次大值),以及LCA(次小值,最大值),两个LCA中深度最大的就是答案。

 #include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+;
#define ll long long
int ccnt=;
int n,cnt,f[maxn],d[maxn],siz[maxn],son[maxn],rk[maxn],top[maxn],tid[maxn];//tid:dfs rk:anti-dfs
int d_mx;
struct edge
{
int to,next;
}e[maxn];
int head[maxn];
inline void addedge(int u,int v)
{
e[++ccnt].to=v;
e[ccnt].next=head[u];
head[u]=ccnt;
}
void dfs1(int u,int fa,int depth)
{
f[u]=fa;
d[u]=depth;
d_mx=max(d_mx,d[u]);
siz[u]=;
for(int i=head[u]; i ; i=e[i].next)
{
int v=e[i].to;
if(v==fa)
continue;
dfs1(v,u,depth+);
siz[u]+=siz[v];
if(siz[v]>siz[son[u]])
son[u]=v;
}
}
void dfs2(int u,int t)
{
top[u]=t;
tid[u]=++cnt;
rk[cnt]=u;
if(!son[u])
return;
dfs2(son[u],t);
for(int i=head[u]; i ; i=e[i].next)
{
int v=e[i].to;
if(v!=son[u]&&v!=f[u])
dfs2(v,v);
}
}
int LCA(int x,int y)
{
if(x==y)
return x;
int fx=top[x],fy=top[y];
while(fx!=fy)
{
if(d[fx]>=d[fy])
{
x=f[fx];
}
else
{
y=f[fy];
}
fx=top[x];
fy=top[y];
}
if(tid[x]<=tid[y]) return x;
else return y;
}
#define lson o*2
#define rson o*2+1
#define m (l+r)/2
struct segment
{
int mx,mn;
}tr[*maxn];
inline void pushup(int o)
{
tr[o].mx=max(tr[lson].mx,tr[rson].mx);
tr[o].mn=min(tr[lson].mn,tr[rson].mn);
}
inline int ret(int o,int flag)
{
if(flag) return tr[o].mx;
else return tr[o].mn;
}
void build(int o,int l,int r)
{
if(l==r)
{
tr[o].mx=tr[o].mn=tid[l];
return;
}
build(lson,l,m);
build(rson,m+,r);
pushup(o);
}
int query(int o,int l,int r,int ql,int qr,int flag)//1 mx 0 mn
{
if(ql>qr)
{
if(flag) return ;
else return maxn;
}
if(ql<=l&&qr>=r)
return ret(o,flag);
if(qr<=m)
return query(lson,l,m,ql,qr,flag);
if(ql>m)
return query(rson,m+,r,ql,qr,flag);
if(flag)
return max(query(lson,l,m,ql,qr,flag),query(rson,m+,r,ql,qr,flag));
else
return min(query(lson,l,m,ql,qr,flag),query(rson,m+,r,ql,qr,flag));
}
int main()
{
int n,q;
scanf("%d%d",&n,&q);
for(int i=;i<=n;i++)
{
int j;
scanf("%d",&j);
addedge(j,i);
}
dfs1(,,);
dfs2(,);
build(,,n);
while(q--)
{
int l,r;
scanf("%d%d",&l,&r);
if(l==r) printf("%d %d\n",l,d_mx);
int mx=query(,,n,l,r,);
int mn=query(,,n,l,r,);
mx=rk[mx];
mn=rk[mn];
int cmx=max(query(,,n,l,mx-,),query(,,n,mx+,r,));
int cmn=min(query(,,n,l,mn-,),query(,,n,mn+,r,));
cmx=rk[cmx];
cmn=rk[cmn];
int lca1=LCA(mx,cmn);
int lca2=LCA(mn,cmx);
if(d[lca1]<d[lca2])
printf("%d %d\n",mx,d[lca2]);
else
printf("%d %d\n",mn,d[lca1]);
}
}

Codeforces 1062E 题解的更多相关文章

  1. codeforces#536题解

    CodeForces#536 A. Lunar New Year and Cross Counting Description: Lunar New Year is approaching, and ...

  2. codeforces 1093 题解

    12.18 update:补充了 $ F $ 题的题解 A 题: 题目保证一定有解,就可以考虑用 $ 2 $ 和 $ 3 $ 来凑出这个数 $ n $ 如果 $ n $ 是偶数,我们用 $ n / 2 ...

  3. Codeforces Numbers 题解

    这题只需要会10转P进制就行了. PS:答案需要约分,可以直接用c++自带函数__gcd(x,y). 洛谷网址 Codeforces网址 Code(C++): #include<bits/std ...

  4. Codeforces 691E题解 DP+矩阵快速幂

    题面 传送门:http://codeforces.com/problemset/problem/691/E E. Xor-sequences time limit per test3 seconds ...

  5. Codeforces 833B 题解(DP+线段树)

    题面 传送门:http://codeforces.com/problemset/problem/833/B B. The Bakery time limit per test2.5 seconds m ...

  6. Codeforces 840C 题解(DP+组合数学)

    题面 传送门:http://codeforces.com/problemset/problem/840/C C. On the Bench time limit per test2 seconds m ...

  7. Codeforces 515C 题解(贪心+数论)(思维题)

    题面 传送门:http://codeforces.com/problemset/problem/515/C Drazil is playing a math game with Varda. Let’ ...

  8. Codeforces 475D 题解(二分查找+ST表)

    题面: 传送门:http://codeforces.com/problemset/problem/475/D Given a sequence of integers a1, -, an and q ...

  9. CodeForces CF875C题解

    题解 非常有意思的\(2-SAT\)的题. 听学长讲完之后感觉确实容易想到\(2-SAT\),顺理成章. 显然,对于两个串,对咱们来说有意义的显然是两个串中第一个不同的数字.那么,我们假设两个串分别是 ...

随机推荐

  1. jQuery_3_过滤选择器

    过滤选择器(过滤器)类似于CSS3里的伪类,包含 1. 基本过滤器 2. 内容过滤器 3. 可见性过滤器 4. 子元素过滤器 5. 其他方法 一.  基本过滤器 过滤器名 jQuery语法 注释 :f ...

  2. Sql Server 表的复制

    声名:A,B ,都是表 --B表存在(两表结构一样)insert into B select * from A 若两表只是有部分(字段)相同,则 insert into B(col1,col2,col ...

  3. TFS看板的设计

    列 产品开发的整个流程如下图,将流程配置到看板的列: 需求池-->就绪-->开发-->测试-->待验收 -->待发布 -->已关闭 一般将Bug和需求放在一块看版上 ...

  4. NYOJ-596-谁是最好的Coder

    原题链接 谁是最好的Coder 时间限制:1000 ms  |  内存限制:65535 KB 难度:0 描述 计科班有很多Coder,帅帅想知道自己是不是综合实力最强的coder. 帅帅喜欢帅,所以他 ...

  5. windows phone 8.0 的网络图片异步加载方案

    买了一本林政的8.1UI的书,看到一个使用弱引用对像来解决图片缓存的问题,刚好自已写的应用也遇到这个问题,于是小改动了一下代码,搬到了8.0版本来使用,代码由 zhxilin℃+ 大神提供了部分解决代 ...

  6. iOS 7系列译文:认识 TextKit

    OS 7:终于来了,TextKit.   功能   所以咱们到了.iOS7 带着 TextKit 登陆了.咱们看看它可以做什么!深入之前,我还想提一下,严格来说,这些事情中的大部分以前都可以做.如果你 ...

  7. 【洛谷3232】[HNOI2013] 游走(贪心+高斯消元)

    点此看题面 大致题意: 一个无向连通图,小\(Z\)从\(1\)号顶点出发,每次随机选择某条边走到下一个顶点,并将\(ans\)加上这条边的编号,走到\(N\)号顶点时结束.请你对边进行编号,使总分期 ...

  8. python Scraping

    http://docs.python-guide.org/en/latest/scenarios/scrape/

  9. UNC路径格式

    \\192.168.3.66\c$  访问本地网内计算机

  10. 3170: [Tjoi2013]松鼠聚会

    Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 1804  Solved: 968[Submit][Status][Discuss] Descript ...