---
id: 2202
lang: en
translation_of: index.md
source_sha256: 2dc9621b6c2248d1c049f81af9789d58c75b66e0e81ceeb6d291b7b64378f13d
title: "P4577: [FJOI2018] Leadership Group Problem — Solution"
description: "Since I grabbed this problem from yyb's blog, I knew it could be done with a segment tree."
tags: ["OI","DP","Segment Tree Merging","LIS"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "Problem name 领导集团问题 rendered literally as “Leadership Group Problem”."
  - "套路 in the heading read as OI usage for a standard technique (“standard trick”), so no chip."
  - "差分的方式 rendered as “with differences” (difference-array style); 标记永久化 as “permanent lazy tags”."
---

## One of the Standard Tricks for LIS on a Tree

Since I grabbed this problem from yyb's blog, I knew it could be done with a segment tree. After thinking about it roughly, my initial idea was to use a segment tree to maintain the longest subsequence length for different heights(?). This actually works: when reading in the heights, first stuff each one into that node's dynamically allocated segment tree, and then the children's trees can be merged. When merging the children, maintain the longest subsequence length obtainable when it is at least $x$, so we can do it with differences (of course, you can also do a range add with permanent lazy tags): add a one starting from position $w_u$, and then delete the previous $1$.

<!--more-->

## Code

```cpp
// P4577.cpp
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 2e5 + 200;

int n, wi[MAX_N], head[MAX_N], current, ptot, roots[MAX_N];
vector<int> mp;

struct edge
{
    int to, nxt;
} edges[MAX_N << 1];

void addpath(int src, int dst)
{
    edges[current].to = dst, edges[current].nxt = head[src];
    head[src] = current++;
}

struct node
{
    int lson, rson, sum;
} nodes[MAX_N * 50];

int ripe(int x) { return lower_bound(mp.begin(), mp.end(), x) - mp.begin() + 1; }

int merge(int x, int y)
{
    if (x == 0 || y == 0)
        return x + y;
    nodes[x].sum += nodes[y].sum;
    nodes[x].lson = merge(nodes[x].lson, nodes[y].lson);
    nodes[x].rson = merge(nodes[x].rson, nodes[y].rson);
    return x;
}

int update(int qx, int l, int r, int p)
{
    if (p == 0)
        p = ++ptot;
    nodes[p].sum++;
    if (l == r)
        return p;
    int mid = (l + r) >> 1;
    if (qx <= mid)
        nodes[p].lson = update(qx, l, mid, nodes[p].lson);
    else
        nodes[p].rson = update(qx, mid + 1, r, nodes[p].rson);
    return p;
}

void eliminate(int p)
{
    if (p == 0)
        return;
    nodes[p].sum--;
    if (nodes[nodes[p].rson].sum > 0)
        eliminate(nodes[p].rson);
    else
        eliminate(nodes[p].lson);
}

bool modify(int qx, int l, int r, int p)
{
    if (l == r)
        return false;
    int mid = (l + r) >> 1;
    bool ret = false;
    if (qx <= mid)
        ret |= modify(qx, l, mid, nodes[p].lson);
    else
    {
        ret |= modify(qx, mid + 1, r, nodes[p].rson);
        if (nodes[nodes[p].lson].sum && ret == false)
            ret = true, eliminate(nodes[p].lson);
    }
    if (ret)
        nodes[p].sum--;
    return ret;
}

void dfs(int u, int fa)
{
    for (int i = head[u]; i != -1; i = edges[i].nxt)
        dfs(edges[i].to, u), merge(roots[u], roots[edges[i].to]);
    modify(wi[u], 1, n, roots[u]);
}

int main()
{
    memset(head, -1, sizeof(head));
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
        scanf("%d", &wi[i]), mp.push_back(wi[i]);
    sort(mp.begin(), mp.end()), mp.erase(unique(mp.begin(), mp.end()), mp.end());
    for (int i = 1; i <= n; i++)
        wi[i] = ripe(wi[i]), roots[i] = update(wi[i], 1, n, roots[i]);
    for (int i = 2, fat; i <= n; i++)
        scanf("%d", &fat), addpath(fat, i);
    dfs(1, 0), printf("%d\n", nodes[roots[1]].sum);
    return 0;
}
```
