CF1062E Company

链接

cf

luogu

题目大意

给定一颗树,有若干个询问,每个询问给出 l,r,要求编号为 ll~rr 的点任意删去一个之后剩余点的 LCA 深度最大,输出删去点的编号和 LCA 的最大深度

思路

一堆点的lca就是dfs序列的最大和最小的lca

因为只能删除一个点,那就看看删除最大的优秀还是删除最小的优秀。

修改其他的lca是不变的。查询次大线段树麻烦,主席树还能短一点。

代码

#include <bits/stdc++.h>
using namespace std;
const int N=1e5+7;
int read() {
int x=0,f=1;char s=getchar();
for(;s>'9'||s<'0';s=getchar()) if(s=='-') f=-1;
for(;s>='0'&&s<='9';s=getchar()) x=x*10+s-'0';
return x*f;
}
int n,q,a[N];
vector<int> G[N];
namespace seg {
struct node {
int ls,rs,siz,nb;
}e[N*30];
int cnt,rt[N];
void insert(int &rt,int old,int l,int r,int id,int nb) {
rt=++cnt;
e[rt]=e[old];
e[rt].siz++;
if(l==r) return e[rt].nb=nb,void();
int mid=(l+r)>>1;
if(id<=mid) insert(e[rt].ls,e[old].ls,l,mid,id,nb);
else insert(e[rt].rs,e[old].rs,mid+1,r,id,nb);
}
int k_th(int rt,int old,int l,int r,int k) {
if(l==r) return e[rt].nb;
int now=e[e[rt].ls].siz-e[e[old].ls].siz;
int mid=(l+r)>>1;
if(now>=k) return k_th(e[rt].ls,e[old].ls,l,mid,k);
else return k_th(e[rt].rs,e[old].rs,mid+1,r,k-now);
}
}
namespace diss_tree {
int dep[N],siz[N],fa[N];
int top[N],son[N],cnt;
void dfs1(int u,int f) {
a[u]=++cnt;
fa[u]=f;
siz[u]=1;
for(auto v:G[u]) {
if(f==v) continue;
dep[v]=dep[u]+1;
dfs1(v,u);
siz[u]+=siz[v];
if(siz[son[u]]<siz[v]) son[u]=v;
}
}
void dfs2(int u,int topf) {
top[u]=topf;
if(!son[u]) return;
dfs2(son[u],topf);
for(auto v:G[u])
if(!top[v]) dfs2(v,v);
}
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() {
n=read(),q=read();
for(int i=2;i<=n;++i) {
int x=read();
G[x].push_back(i);
G[i].push_back(x);
}
diss_tree::dfs1(1,0);
diss_tree::dfs2(1,1);
for(int i=1;i<=n;++i) seg::insert(seg::rt[i],seg::rt[i-1],1,n,a[i],i);
for(int i=1;i<=q;++i) {
int x=read(),y=read();
int first_max=seg::k_th(seg::rt[y],seg::rt[x-1],1,n,1);
int second_max=seg::k_th(seg::rt[y],seg::rt[x-1],1,n,2);
int first_min=seg::k_th(seg::rt[y],seg::rt[x-1],1,n,y-x+1);
int second_min=seg::k_th(seg::rt[y],seg::rt[x-1],1,n,y-x);
int tmp_x=diss_tree::dep[diss_tree::lca(first_max,second_min)];
int tmp_y=diss_tree::dep[diss_tree::lca(second_max,first_min)];
if(tmp_x > tmp_y) printf("%d %d\n",first_min,tmp_x);
else printf("%d %d\n",first_max,tmp_y);
}
return 0;
}

CF1062E Company的更多相关文章

  1. 题解 CF1062E Company

    \(\texttt{Solution}\) 数据结构学傻的蒟蒻来写一个新思路 这题的正解是利用多个结点的 \(lca\) 是 \(dfs\) 序最大的结点和 \(dfs\) 序最小的结点的 \(lca ...

  2. Elasticsearch索引(company)_Centos下CURL增删改

    目录 返回目录:http://www.cnblogs.com/hanyinglong/p/5464604.html 1.Elasticsearch索引说明 a. 通过上面几篇博客已经将Elastics ...

  3. Microsoft Dynamics AX 2012: How to get Company,Customer and Vendor address in AX 2012

    Scenario:  “How to get Addresses of “Customer, Vendor and Company” 1)      First we need to identify ...

  4. poj1416 Shredding Company

    Shredding Company Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5379   Accepted: 3023 ...

  5. CodeForces 125E MST Company

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

  6. CF 321B Kefa and Company(贪心)

    题目链接: 传送门 Kefa and Company time limit per test:2 second     memory limit per test:256 megabytes Desc ...

  7. 搜索+剪枝 POJ 1416 Shredding Company

    POJ 1416 Shredding Company Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5231   Accep ...

  8. 二分+动态规划 POJ 1973 Software Company

    Software Company Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 1112   Accepted: 482 D ...

  9. 【Moqui业务逻辑翻译系列】Story of Online Retail Company 在线零售公司的故事

    h1. Story of Online Retail Company 在线零售公司的故事 Someone decides to sell a product. [Product Marketer Ma ...

随机推荐

  1. MFC 修改标题

    1. Overwrite CMainFrame::PreCreateWindow. Clear the style FWS_ADDTOTITLE cs.style &= ~(LONG)FWS_ ...

  2. shiro使用redis作为缓存,出现要清除缓存时报错 java.lang.Exception: Failed to deserialize at org.crazycake.shiro.SerializeUtils.deserialize(SerializeUtils.java:41) ~[shiro-redis-2.4.2.1-RELEASE.jar:na]

    shiro使用redis作为缓存,出现要清除缓存时报错 java.lang.Exception: Failed to deserialize at org.crazycake.shiro.Serial ...

  3. ASM检查RAC是否成功

    [grid@asm ~]$ crsctl status resourceNAME=ora.DATA.dgTYPE=ora.diskgroup.typeTARGET=ONLINESTATE=ONLINE ...

  4. Redis Sentinel实现的机制与原理详解

    序言 Redis-Sentinel是Redis官方推荐的高可用性(HA)解决方案.实际上这意味着你可以使用Sentinel模式创建一个可以不用人为干预而应对各种故障的Redis部署. 它的主要功能有以 ...

  5. [LeetCode] 97. Interleaving String_ Hard tag: Dynamic Programming

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Example 1: Input: s1 = ...

  6. xamarin.forms 动态条件更换数据模板

    解决方案1:  https://oren.codes/2014/12/31/datatemplateselector-for-xamarin-forms/ 解决方案2:  https://docs.m ...

  7. localforage调用setItem时出现DOMException错误的解决方法

    今天使用localforage时出现下面的错误: Uncaught (in promise) DOMException transaction.onabort.transaction.onerror ...

  8. flask下载文件---文件流

    html: <a name="downloadbtn" class="btn btn-success pull-right" href="/do ...

  9. php中双$符 及一些基础知识

    双$$符号表示可变变量 如 $a = "b", $b = 'c'; echo $$a    此时 $$a=>$($a) =>$b 输出的值就应该为c; 变量传应用值$b ...

  10. Linux批量结束、杀死进程

    ps aux|grep python|grep -v grep|cut -c 9-15|xargs kill -15 管道符“|”用来隔开两个命令,管道符左边命令的输出会作为管道符右边命令的输入.下面 ...