507.ans1
======================
2

=================
507.ans2
======================
3

=================
507.ans3
======================
3 3 8

=================
507.ans4
======================
2147483647 

=================
507.cpp
======================
#include <bits/stdc++.h>

using namespace std;

template<typename T1, typename T2>
ostream &operator<<(ostream &out, const pair<T1, T2> &x) {
    return out << x.first << ' ' << x.second;
}

template<typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &x) {
    return in >> x.first >> x.second;
}

template<typename T>
istream &operator>>(istream &in, vector<T> &a) {
    for(auto &x: a) {
        in >> x;
    }
    return in;
};

template<typename T>
ostream &operator<<(ostream &out, const vector<T> &a) {
    for(auto x: a) {
        out << x << ' ';
    }
    return out;
};

const int INF = 2147483647;

int n, m;
vector<int> a;
vector<vector<int>> adj;

void read() {
    cin >> n >> m;
    a.resize(n);
    adj.assign(n, vector<int>());
    for(int i = 1; i < n; i++) {
        int par;
        cin >> par;
        adj[par - 1].push_back(i);
    }

    for(int i = 0; i < m; i++) {
        cin >> a[n - m + i];
    }
}

set<int> dfs(int u, vector<int> &answer) {
    if(adj[u].empty()) {
        return {a[u]};
    }

    set<int> s;
    for(auto v: adj[u]) {
        set<int> t = dfs(v, answer);

        answer[u] = min(answer[u], answer[v]);
        if(s.size() < t.size()) {
            swap(s, t);
        }

        for(auto x: t) {
            auto it = s.lower_bound(x);
            if(it != s.end()) {
                answer[u] = min(answer[u], *it - x);
            } 
            if(it != s.begin()) {
                answer[u] = min(answer[u], x - *prev(it));
            }
            s.insert(x);
        }
    }

    return s;
}

void solve() {
    vector<int> answer(n, INF);
    dfs(0, answer);
    for(int i = 0; i < n - m; i++) {
        cout << answer[i] << ' ';
    }
    cout << '\n';
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int T = 1;
    // cin >> T;
    for(int test = 1; test <= T; test++) {
        read();
        // cout << "Case #" << test << ": ";
        solve();
    }

    return 0;
}

=================
507.in1
======================
5 4
1
1
1
1
1 
4 
7 
9

=================
507.in2
======================
5 4
1
1
1
1
1 
4 
7 
10

=================
507.in3
======================
7 4
1
2
1
2
3
3
2 
10 
7 
15

=================
507.in4
======================
2 1
1
100

=================
p507.ans1
======================
2

=================
p507.ans2
======================
3

=================
p507.ans3
======================
3 3 8

=================
p507.ans4
======================
2147483647

=================
p507.in1
======================
5 4
1
1
1
1
1
4
7
9

=================
p507.in2
======================
5 4
1
1
1
1
1
4
7
10

=================
p507.in3
======================
7 4
1
2
1
2
3
3
2
10
7
15

=================
p507.in4
======================
2 1
1
100

=================
statement.txt
======================
507. Treediff
Time limit per test: 0.25 second(s)
Memory limit: 262144 kilobytes
input: standard
output: standard

Andrew has just made a breakthrough in complexity theory: he thinks that he can prove P=NP if he can get a data structure which allows to perform the following operation quickly. Naturally, you should help him complete his brilliant research. Consider a rooted tree with integers written in the leaves. For each internal (non-leaf) node v of the tree you must compute the minimum absolute difference between all pairs of numbers written in the leaves of the subtree rooted at v.
Input
The first line of the input file contains two integers n and m — overall number of nodes in the tree and number of leaves in the tree respectively. . All nodes are numbered from 1 to n. Node number 1 is always the root of the tree. Each of the other nodes has a unique parent in the tree. Each of the next n - 1 lines of the input file contains one integer — the number of the parent node for nodes 2, 3,..., n respectively. Each of the last m lines of the input file contains one integer ranging from  to  — the value of the corresponding leaf. Leaves of the tree have numbers from n - m + 1 to n.
Output
Output one line with n - m integers: for each internal node of the tree output the minimum absolute difference between pairs of values written in the leaves of its subtree. If there is only one leaf in the subtree of some internal node, output number 231 - 1 for that node. Output the answers for the nodes in order from node number 1 to n - m.
Example(s)
sample input
sample output
5 4
1
1
1
1
1 
4 
7 
9
2

sample input
sample output
5 4
1
1
1
1
1 
4 
7 
10
3

sample input
sample output
7 4
1
2
1
2
3
3
2 
10 
7 
15
3 3 8

sample input
sample output
2 1
1
100
2147483647 

=================
