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(1≤pu≤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:

lv(1)=0lv(1)=0lv(1)=0
lv(u)=lv(pu)+1foru≠1lv(u)=lv(p_u)+1 for u≠1lv(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 lil_ili and rir_iri, meaning that all the employees in the range [li,ri][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∈[li,ri]v∈[l_i,r_i]v[li,ri], u must be in charge of v. Note, that u is not necessary in the range [li,ri][l_i,r_i][li,ri]. Also, u is always chosen in such a way that lv(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 p2,p3,…,pn(1≤pi≤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 lil_ili and ri(1≤li&lt;ri≤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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值