Language: C++

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
#define MAX 300000
int n,size;
vector<int> A;
int mp[MAX];
vector<ll> bit;
vector<P> tree;

void update(int a,int b,P x,int k,int l,int r){
  if(b<=l || r<=a)return;
  if(a<=l && r<=b){
    tree[k]=max(tree[k],x);
  }else{
    int m=(l+r)/2;
    update(a,b,x,k*2+1,l,m);
    update(a,b,x,k*2+2,m,r);
  }
}

void Upd(int a,int b,P x){
  a+=2;
  b+=2;
  if(a<b){
    update(a,b+1,x,0,0,size);
  }else{
    update(a,size,x,0,0,size);
    update(0,b+1,x,0,0,size);
  }
}

ll Get(int i){
  i+=2;
  i+=size-1;
  P res=tree[i];
  while(i){
    i=(i-1)/2;
    res=max(res,tree[i]);
  }
  return res.second;
}

void Add(int i,ll x){
  i+=2;
  while( i<(int)bit.size() ){
    bit[i]+=x;
    i+=(i&-i);
  }
}

ll Sum(int i){
  i+=2;
  ll res=0;
  while(i>0){
    res+=bit[i];
    i-=(i&-i);
  }
  return res;
}

ll Sum(int a,int b){
  if(a<b){
    return Sum(b)-Sum(a-1);
  }else{
    return Sum(n)-Sum(a-1)+Sum(b);
  }
}


ll solve(){
  vector<int> X;
  for(int i=0;i<(int)A.size();i++)
    X.push_back(A[i]);
  sort(X.begin(),X.end());
  
  
  size=1;
  while( size < n+5 ) size*=2;
  bit.clear();
  bit.resize(size);
  tree.clear();
  tree.resize(size*2);
  mp.clear();
  
  for(int i=0;i<(int)A.size();i++){
    A[i]= lower_bound( X.begin(), X.end(), A[i]) -X.begin() + 1;
    Upd(i,i+1,P(0,i));
    Add(i,1);
    mp[ A[i] ]=i;
  }
  
  int p=-1;
  ll res=0;
  for(int i=1;i+1<=n;i++){
    int num=mp[i];
    int target=Get(num);
    Add(num,-1);
    res+=Sum(p,target);
    Upd(p,target,P(i,target));
    p=target;
  }
  return res;
}

int main(){
  scanf("%d",&n);
  A.clear();
  A.resize(n);
  for(int i=0;i<n;i++)scanf("%d",&A[i]);
  printf("%lld\n",solve());
  return 0;
}

Compiler output:
a.cc: In function 'void Upd(int, int, P)':
a.cc:27:24: error: reference to 'size' is ambiguous
   27 |     update(a,b+1,x,0,0,size);
      |                        ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:1:
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:6:7: note:                 'int size'
    6 | int n,size;
      |       ^~~~
a.cc:29:14: error: reference to 'size' is ambiguous
   29 |     update(a,size,x,0,0,size);
      |              ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:6:7: note:                 'int size'
    6 | int n,size;
      |       ^~~~
a.cc:29:25: error: reference to 'size' is ambiguous
   29 |     update(a,size,x,0,0,size);
      |                         ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:6:7: note:                 'int size'
    6 | int n,size;
      |       ^~~~
a.cc:30:24: error: reference to 'size' is ambiguous
   30 |     update(0,b+1,x,0,0,size);
      |                        ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:6:7: note:                 'int size'
    6 | int n,size;
      |       ^~~~
a.cc: In function 'll Get(int)':
a.cc:36:6: error: reference to 'size' is ambiguous
   36 |   i+=size-1;
      |      ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:6:7: note:                 'int size'
    6 | int n,size;
      |       ^~~~
a.cc: In function 'll solve()':
a.cc:79:3: error: reference to 'size' is ambiguous
   79 |   size=1;
      |   ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:6:7: note:                 'int size'
    6 | int n,size;
      |       ^~~~
a.cc:80:10: error: reference to 'size' is ambiguous
   80 |   while( size < n+5 ) size*=2;
      |          ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:6:7: note:                 'int size'
    6 | int n,size;
      |       ^~~~
a.cc:80:23: error: reference to 'size' is ambiguous
   80 |   while( size < n+5 ) size*=2;
      |                       ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:6:7: note:                 'int size'
    6 | int n,size;
      |       ^~~~
a.cc:82:14: error: reference to 'size' is ambiguous
   82 |   bit.resize(size);
      |              ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:6:7: note:                 'int size'
    6 | int n,size;
      |       ^~~~
a.cc:84:15: error: reference to 'size' is ambiguous
   84 |   tree.resize(size*2);
      |               ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:6:7: note:                 'int size'
    6 | int n,size;
      |       ^~~~
a.cc:85:6: error: request for member 'clear' in 'mp', which is of non-class type 'int [300000]'
   85 |   mp.clear();
      |      ^~~~~