1. 程式人生 > >Codeforces-1062E:Company(LCA+線段樹)

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 pu(1pun)p_u (1≤p_u≤n), 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:

lv(1)=0lv(1)=0
lv(u)=lv(pu)+1foru1lv(u)=lv(p_u)+1 for u≠1
In the near future, there are q possible plans for the company to operate. The i-th plan consists of two integers lil_i and rir_i, meaning that all the employees in the range [

li,ri][l_i,r_i], 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[
li,ri]v∈[l_i,r_i]
, u must be in charge of v. Note, that u is not necessary in the range [li,ri][l_i,r_i]. Also, u is always chosen in such a way that 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 (2n100000,1q100000)(2≤n≤100000, 1≤q≤100000) — the number of employees and the number of plans, respectively.

The second line contains n−1 integers p2,p3,,pn(1pin)p_2,p_3,…,p_n (1≤p_i≤n) 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 lil_i and ri(1li<rin)r_i (1≤l_i<r_i≤n) — 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],只有刪掉這個區間內標記最小或最大的節點,才能使得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;
}