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. RoR - More Active Record

    Active Record Scope: default_scope:   默认的检索方式 #Use unscoped to break out of the default class Hobby ...

  2. mysql 字符集

    mysql -u root -p 输入密码进入mysql show variables like 'character%'; --显示字符集,像这样 \q退出mysql, 更改mysql配置文件 vi ...

  3. linux(centos7)设置tomcat开机启动

    1.在/etc/rc.d/rc.local中加入: #java environment export JAVA_HOME=/usr/java/jdk1.8.0_161 export CLASSPATH ...

  4. Linux基础命令2

    1.修改网络状态: 1).Cd  /etc/sysconfig/network-scripts/network-scripts 2).vi ifcfg-eth0   编辑  onboot=yes: 3 ...

  5. 3种检测页面是否符合amp标准的方法

    AMP的关键优势不仅仅在于它能让你的页面更快,还在于它的快可以被验证.有几种方法可以验证AMP文档,它们都会产生完全相同的结果,选择最适合您的开发风格的方法.除了AMP的有效性,您可能还想确认您的AM ...

  6. 学号 20175313《Java程序设计》 第七周学习总结

    目录 一.教材学习内容总结 二.教材学习中的问题和解决过程 三.代码托管 四.心得体会 五.学习进度条 六.参考资料 一.教材学习内容总结 第八章主要内容 了解String类 常量对象:常量池中的数据 ...

  7. [LeetCode] 132. Palindrome Partitioning II_ Hard tag: Dynamic Programming

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  8. Windows使用tail命令跟踪日志

    我们知道如果是Unix/Linux环境可以直接使用 tail -f xxx.log即可. 但是Windows并没有自带这个命令,不过从网上可以找到tail.zip 实测可以将其解压放在C:\Windo ...

  9. FB面经 Prepare: K closest point to the origin

    Give n points on 2-D plane, find the K closest points to origin Based on bucket sort: package fbPrac ...

  10. node多人聊天室搭建

    1.采用websocket: 为什么不用ajax的http:在用HTTP发送消息时,必须用一个新的TCP/IP连接.打开和关闭连接需要时间.此 外,因为每次请求都要发送HTTP头,所以传输的数据量也比 ...