Language: C++

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
#define MAX 3000000
int n,size;
vector<int> A;
unordered_map<int,int> mp;
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(){
  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++){
    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);
    /*
    for(int j=0;j<n;j++){
      if(A[j]<i)cout<<'-';
      else cout<<A[j];
      cout<<' ';
    }
    cout<<endl;
    for(int j=0;j<n;j++)cout<<Get(j)<<' ';cout<<endl;

        cout<<i<<"  p="<<p<<"  target="<<target<<"  res="<<res<<endl;

    cout<<endl;
    */
    Upd(p,target,P(i,target));
    p=target;
  }
  return res;
}

int main(){
  while(cin>>n&&n){
    A.clear();
    A.resize(n);
    for(int i=0;i<n;i++)cin>>A[i];
    cout<<solve()<<endl;
  }
  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:72:3: error: reference to 'size' is ambiguous
   72 |   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:73:10: error: reference to 'size' is ambiguous
   73 |   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:73:23: error: reference to 'size' is ambiguous
   73 |   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:75:14: error: reference to 'size' is ambiguous
   75 |   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:77:15: error: reference to 'size' is ambiguous
   77 |   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;
      |       ^~~~