Language: C++

#include <iostream>
#include <vector>
#define inf 1e9

using namespace std;

// range-add, range-min query

struct SegTree{
	int size;
	vector<int> seg, delay;
	
	SegTree(){}
	SegTree(int size){
		this->size = size;
		seg.resize(1<<(size+1));
		delay.resize(1<<(size+1));
	}
	
	void init()
	{
		for(int i = 0; i < (1<<(size+1)); i++){
			seg[i] = inf;
			delay[i] = 0;
		}
	}
	
	void eval(int l, int r, int k)
	{
		if(delay[k]){
			seg[k] += delay[k];
			if(l < r){
				delay[k*2] += delay[k];
				delay[k*2+1] += delay[k];
			}
			delay[k] = 0;
		}
	}
	
	void update(int i, int val)
	{
		i += (1 << size);
		seg[i] = val;
		while(i > 1){
			i /= 2;
			seg[i] = min(seg[i*2], seg[i*2+1]);
		}
	}
	
	void add(int a, int b, int k, int l, int r, int val)
	{
		eval(l, r, k);
		
		if(b < l || r < a) return;
		if(a <= l && r <= b){
			delay[k] += val;
			eval(l, r, k);
			return;
		}
		add(a, b, k*2, l, (l+r)/2, val);
		add(a, b, k*2+1, (l+r)/2+1, r, val);
		seg[k] = min(seg[k*2], seg[k*2+1]);
	}
	void add(int a, int b, int val){
		if(a > b) return;
		add(a, b, 1, 0, (1<<size)-1, val);
	}

	int query(int a, int b, int k, int l, int r)
	{
		eval(l, r, k);
		
		if(b < l || r < a) return inf;
		if(a <= l && r <= b) return seg[k];
		int lval = query(a, b, k*2, l, (l+r)/2);
		int rval = query(a, b, k*2+1, (l+r)/2+1, r);
		return min(lval, rval);
	}
	int query(int a, int b)
	{
		return query(a, b, 1, 0, (1<<size)-1);
	}
};

int n;
int p[20005];
int rank[20005];
SegTree segA(15), segB(15);

int main(void)
{
	cin >> n;
	for(int i = 1; i <= n; i++) cin >> p[i];
	for(int i = 1; i <= n; i++) rank[p[i]] = i;
	
	segA.init();
	segB.init();
	for(int i = 1; i <= n; i++){
		segB.update(i, n-i+1);
	}
	for(int i = 1; i <= n; i++){
		segA.update(i, rank[i]+n - (n-i+1));
	}
	
	for(int i = 1; i < n; i++){
		int mn = segA.query(i+1, n);
		int val = segA.query(i, i);
		if(mn > val) continue;
		segA.add(i+1, n, val - mn + 1);
		segB.add(1, i, val - mn + 1);
	}
	
	for(int i = 1; i <= n; i++) cout << segA.query(i, i) << " "; cout << endl;
	for(int i = 1; i <= n; i++) cout << segB.query(i, i) << " "; cout << endl;
	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:94:37: error: reference to 'rank' is ambiguous
   94 |         for(int i = 1; i <= n; i++) rank[p[i]] = i;
      |                                     ^~~~
In file included from /usr/include/c++/14/bits/move.h:37,
                 from /usr/include/c++/14/bits/exception_ptr.h:41,
                 from /usr/include/c++/14/exception:166,
                 from /usr/include/c++/14/ios:41,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:87:5: note:                 'int rank [20005]'
   87 | int rank[20005];
      |     ^~~~
a.cc:102:32: error: reference to 'rank' is ambiguous
  102 |                 segA.update(i, rank[i]+n - (n-i+1));
      |                                ^~~~
/usr/include/c++/14/type_traits:1437:12: note: candidates are: 'template<class> struct std::rank'
 1437 |     struct rank
      |            ^~~~
a.cc:87:5: note:                 'int rank [20005]'
   87 | int rank[20005];
      |     ^~~~