We need to solve the problem: Given a permutation a[1..n], construct a binary tree recursively as described: root is maximum element in the whole array; left subtree from left part; right subtree from right part. Then we need to compute the depth d_v (depth of the vertex a_v) for each index v (i.e., for each element). But careful: They denote d_v as depth of vertex a_v, where v is the index in the array? Actually read: "Given the permutation a, for each vertex, find the value of d_v." The notation: earlier: Let us denote by d_v the depth of the vertex a_v, that is, the number of edges on the path from the root to the vertex numbered a_v. Note that the root depth is zero. Given the permutation a, for each vertex, find the value of d_v. So v is index (position) in permutation? They said "vertex a_v", meaning vertex corresponding to the element a_v. So d_v is depth of that element. And we output d_1, d_2, …, d_n.
Thus we need to compute depth of each element in the tree constructed from the permutation. n ≤ 100, t ≤ 100. So naive recursion O(n^2) per test case is fine.
We could either recursively construct the tree and assign depths, or directly compute depth via recursion: For subarray indices [l, r], the maximum element's depth is current depth (passed from parent). Then recursively process left part (l, max_idx-1) with depth+1, and right part (max_idx+1, r) with depth+1.
So algorithm per test case:
- Read n, array a[1..n] (1-indexed).
- We'll create array depth[1..n] to store depth of each element.
- Define recursive function solve(l, r, cur_depth):
if l > r: return.
Find index m in [l, r] such that a[m] is maximum (since it's a permutation, max value is max of that subarray, but we can just find index with max value).
depth[m] = cur_depth.
solve(l, m-1, cur_depth+1)
solve(m+1, r, cur_depth+1)
- Then call solve(1, n, 0).
- Output depths for indices 1..n.
Complexities: O(n^2) if scanning for max each recursion; but n ≤ 100, it's fine.
We must ensure that we handle the base case correctly.
Now I'll produce final answer with code and explanation. However note: The problem is from Codeforces (probably "Binary Tree (easy version)?" or "Constructing a Binary Tree from permutation"). The output format: For each test case, output n values — d_1, d_2, …, d_n. In the example, they have spaces after each value, including trailing space? Actually example: "1 0 2 3 1 " with a space after last. We'll just output values separated by spaces, and after each test case newline; trailing space is okay but not required.
I'll implement accordingly.
Edge Cases: n=1 => depth[1] = 0.
Now, the output for example:
3
5
3 5 2 1 4
1
1
4
4 3 1 2
First test case: They output "1 0 2 3 1 " -> Let's verify: a = [3,5,2,1,4]. Build tree: max is 5 at index2 => root depth0 for index2. left subarray [3]: max 3 at index1 depth1. right subarray [2,1,4]: max is 4 at index5 depth1? Wait careful: Right subarray indices 3..5 = [2,1,4]; max is 4 at index5 => depth = 1 (since root depth0, child depth1). Then left of 4: subarray [2,1] (indices 3..4): max is 2 at index3 depth2? Actually compute: root: 5 depth0. Left: [3] depth1 for 3. Right: [2,1,4] root: 4 depth1. Then left of 4: [2,1] max=2 at index3 depth2; right of 4: none. Then left of 2: none? Actually left of 2 in subarray indices 3..4? subarray [2,1] indices 3 and 4: max at index3 (2). Then left subarray of that max (within [3..4]) is none (since l = 3, m=3, left is empty). Right subarray: [1] at index4 depth3. So depths: index1 (3):1, index2 (5):0, index3 (2):2, index4 (1):3, index5 (4):1. That matches output "1 0 2 3 1". Good.
Second: [1] => depth0.
Third: [4,3,1,2] => Let's compute: max is 4 at index1 depth0. Left: none. Right: [3,1,2] (indices2..4). max in that is 3 at index2 depth1. Left of 3: none? Actually subarray [3,1,2] indices2..4: max=3 at index2, left subarray of that within indices2..4 is empty (since l=2, m=2). Right subarray: [1,2] indices3..4. max = 2 at index4 depth2. left of 2: [1] at index3 depth3. right none. So depths: index1:0, index2:1, index3:3, index4:2. Output "0 1 3 2". matches example.
Thus solution.
Now implement. Possibly can use stack or iterative method, but recursion is simpler.
C++ code:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
void solveSegment(const vector<int>& a, vector<int>& depth, int l, int r, int curDepth) {
if (l > r) return;
int maxIdx = l;
for (int i = l+1; i <= r; ++i) {
if (a[i] > a[maxIdx]) maxIdx = i;
}
depth[maxIdx] = curDepth;
solveSegment(a, depth, l, maxIdx-1, curDepth+1);
solveSegment(a, depth, maxIdx+1, r, curDepth+1);
}
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n+1);
for (int i = 1; i <= n; ++i) cin >> a[i];
vector<int> depth(n+1, 0);
solveSegment(a, depth, 1, n, 0);
for (int i = 1; i <= n; ++i) {
cout << depth[i] << " ";
}
cout << "\n";
}
return 0;
}
But careful: This might have recursion depth up to n=100, fine.
Alternatively, we can find depths directly by using stack of intervals etc.
Given constraints t up to 100, n up to 100, it's fine.
Now produce final answer.
We might need to use 0-indexing? In output they expect d_1... d_n, so index-based.
I think the code above is correct.
Let's write final answer with explanation.
Will also include Python alternative for quick understanding.
But the response should be a full solution (probably in C++), but as the conversation, I'm supposed to give an answer: likely I should produce final answer with code and explanation.
Thus final. ```cpp
#include
#include
using namespace std;
/**
-
Recursively assigns depths to the elements in the subarray a[l..r].
-
@param a permutation (1-indexed)
-
@param depth array to store the depth of each element
-
@param l left index of the subarray (inclusive)
-
@param r right index of the subarray (inclusive)
-
@param curDepth depth of the current root (the maximum of this subarray)
*/
void build(const vector& a, vector& depth, int l, int r, int curDepth) {
if (l > r) return;
// find the index of the maximum element in the current interval
int maxIdx = l;
for (int i = l + 1; i <= r; ++i) {
if (a[i] > a[maxIdx]) {
maxIdx = i;
}
}
depth[maxIdx] = curDepth; // root of this subarray
// recursively process left and right parts with increased depth
build(a, depth, l, maxIdx - 1, curDepth + 1);
build(a, depth, maxIdx + 1, r, curDepth + 1);
}
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n + 1); // 1-indexed for convenience
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
vector<int> depth(n + 1, 0);
build(a, depth, 1, n, 0);
for (int i = 1; i <= n; ++i) {
cout << depth[i] << " ";
}
cout << "\n";
}
return 0;
}