Codeforces-1062E:Company(LCA+线段树)

E. Company
time limit per test 2 seconds
memory limit per test 256 megabytes
inputstandard input
outputstandard output
The company X has n employees numbered from 1 through n. Each employee u has a direct boss p u ( 1 ≤ p u ≤ n ) p_u (1≤p_u≤n) pu(1pun), except for the employee 1 who has no boss. It is guaranteed, that values pi form a tree. Employee u is said to be in charge of employee v if u is the direct boss of v or there is an employee w such that w is in charge of v and u is the direct boss of w. Also, any employee is considered to be in charge of himself.

In addition, for each employee u we define it’s level lv(u) as follow:

l v ( 1 ) = 0 lv(1)=0 lv(1)=0
l v ( u ) = l v ( p u ) + 1 f o r u ≠ 1 lv(u)=lv(p_u)+1 for u≠1 lv(u)=lv(pu)+1foru̸=1
In the near future, there are q possible plans for the company to operate. The i-th plan consists of two integers l i l_i li and r i r_i ri, meaning that all the employees in the range [ l i , r i ] [l_i,r_i] [li,ri], and only they, are involved in this plan. To operate the plan smoothly, there must be a project manager who is an employee in charge of all the involved employees. To be precise, if an employee u is chosen as the project manager for the i-th plan then for every employee v ∈ [ l i , r i ] v∈[l_i,r_i] v[li,ri], u must be in charge of v. Note, that u is not necessary in the range [ l i , r i ] [l_i,r_i] [li,ri]. Also, u is always chosen in such a way that l v ( u ) lv(u) lv(u) is as large as possible (the higher the level is, the lower the salary that the company has to pay the employee).

Before any plan is operated, the company has JATC take a look at their plans. After a glance, he tells the company that for every plan, it’s possible to reduce the number of the involved employees exactly by one without affecting the plan. Being greedy, the company asks JATC which employee they should kick out of the plan so that the level of the project manager required is as large as possible. JATC has already figured out the answer and challenges you to do the same.

Input
The first line contains two integers n and q ( 2 ≤ n ≤ 100000 , 1 ≤ q ≤ 100000 ) (2≤n≤100000, 1≤q≤100000) (2n100000,1q100000) — the number of employees and the number of plans, respectively.

The second line contains n−1 integers p 2 , p 3 , … , p n ( 1 ≤ p i ≤ n ) p_2,p_3,…,p_n (1≤p_i≤n) p2,p3,,pn(1pin) meaning pi is the direct boss of employee i.

It is guaranteed, that values pi form a directed tree with the root of 1.

Each of the following q lines contains two integers l i l_i li and r i ( 1 ≤ l i &lt; r i ≤ n ) r_i (1≤l_i&lt;r_i≤n) ri(1li<rin) — the range of the employees, involved in the corresponding plan.

Output
Print q lines, each containing two integers — the number of the employee which should be kicked from the corresponding plan and the maximum possible level of the project manager in that case.

If there are more than one way to choose that employee, print any of them.

Example
input
11 5
1 1 3 3 3 4 2 7 7 6
4 6
4 8
1 11
9 11
8 11
output
4 1
8 1
1 0
11 3
8 1

思路:对于这课树进行DFS序标记,对于每个询问 [ L , R ] [L,R] [L,R],只有删掉这个区间内标记最小或最大的节点,才能使得LCA的深度变深。
然后可以利用线段树记录区间标记最大/最小的点,以及区间的LCA。

#include<bits/stdc++.h>
using namespace std;
const int MAX=1e5+10;
typedef long long ll;
vector<int>e[MAX];
int in[MAX],d[MAX],all=0;
int nex[MAX][21];
void dfs(int k,int fa,int dep)
{
    in[k]=++all;
    d[k]=dep;
    nex[k][0]=fa;
    for(int i=1;i<=20;i++)nex[k][i]=nex[nex[k][i-1]][i-1];
    for(int i=0;i<e[k].size();i++)dfs(e[k][i],k,dep+1);
}
int LCA(int x,int y)
{
    if(x==0)return y;
    if(y==0)return x;
    if(x==y)return x;
    if(d[x]>d[y])swap(x,y);
    for(int i=20;i>=0;i--)
    {
        if(nex[y][i]==0)continue;
        if(d[x]<d[y]&&d[nex[y][i]]>=d[x])y=nex[y][i];
    }
    for(int i=20;i>=0;i--)
    {
        if(nex[x][i]!=nex[y][i])
        {
            x=nex[x][i];
            y=nex[y][i];
        }
    }
    if(x!=y)return nex[x][0];
    return x;
}
struct lenka
{
    int l,r;
    int a,b;
    int LCA;
}A[MAX<<2];
void build(int k,int l,int r)
{
    A[k].l=l,A[k].r=r;
    A[k].a=A[k].b=r;
    A[k].LCA=r;
    if(l==r)return;
    build(2*k,l,(l+r)/2);
    build(2*k+1,(l+r)/2+1,r);
    A[k].a=(in[A[2*k].a]<in[A[2*k+1].a])?A[2*k].a:A[2*k+1].a;
    A[k].b=(in[A[2*k].b]<in[A[2*k+1].b])?A[2*k+1].b:A[2*k].b;
    A[k].LCA=LCA(A[2*k].LCA,A[2*k+1].LCA);
}
int askLCA(int k,int x,int y)
{
    if(x>y)return 0;
    if(x==A[k].l&&y==A[k].r)return A[k].LCA;
    if(y<=A[2*k].r)return askLCA(2*k,x,y);
    if(x>=A[2*k+1].l)return askLCA(2*k+1,x,y);
    return LCA(askLCA(2*k,x,A[2*k].r),askLCA(2*k+1,A[2*k+1].l,y));
}
pair<int,int> ask(int k,int x,int y)
{
    if(A[k].l==x&&A[k].r==y)return {A[k].a,A[k].b};
    if(y<=A[2*k].r)return ask(2*k,x,y);
    if(x>=A[2*k+1].l)return ask(2*k+1,x,y);
    pair<int,int> L=ask(2*k,x,A[2*k].r);
    pair<int,int> R=ask(2*k+1,A[2*k+1].l,y);
    L.first=in[L.first]<in[R.first]?L.first:R.first;
    L.second=in[L.second]<in[R.second]?R.second:L.second;
    return L;
}
int main()
{
    int n,m;
    cin>>n>>m;
    for(int i=2;i<=n;i++)
    {
        int x;
        scanf("%d",&x);
        e[x].push_back(i);
    }
    dfs(1,0,0);
    build(1,1,n);
    while(m--)
    {
        int l,r;
        scanf("%d%d",&l,&r);
        pair<int,int>pp=ask(1,l,r);
        int L=LCA(askLCA(1,l,pp.first-1),askLCA(1,pp.first+1,r));
        int R=LCA(askLCA(1,l,pp.second-1),askLCA(1,pp.second+1,r));
        if(d[L]>d[R])printf("%d %d\n",pp.first,d[L]);
        else printf("%d %d\n",pp.second,d[R]);
    }
    return 0;
}

引用\[1\]中提到了一种树形动态规划的方法来解决CodeForces - 982C问题。在这个问题中,subtree指的是子连通块,而不是子树。为了使cnt_white - cnt_black尽可能大,可以使用两次树形动态规划来求解。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。如果ans\[u\]为正,则减去dp\[v\]就是树链所代表的子树的权值。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] 问题: CodeForces - 982C 树形DP是什么问题?如何解决? 回答: CodeForces - 982C是一个树形动态规划问题。在这个问题中,需要求解子连通块的最大权值和,使得cnt_white - cnt_black尽可能大。解决这个问题的方法是使用两次树形动态规划。第一次是自底向上的过程,维护一个dp数组,表示以每个节点为根的子树中的最大连通块。第二次是自顶向下的过程,处理自底向上过程中无法包含的树链所代表的子树。在第二次遍历中,需要维护一个sum变量,用于存储树链所代表的子树的贡献。根据ans\[u\]的正负,决定是否能对相邻的子节点做出贡献。最终,ans\[u\]代表包含节点u在内的子连通块的最大权值。\[1\] #### 引用[.reference_title] - *1* *2* [CodeForces - 1324F Maximum White Subtree(树形dp)](https://blog.csdn.net/qq_45458915/article/details/104831678)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值