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=0,int l=0,int r=size){
  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){
  if(a<b){
    update(a,b+1,x);
  }else{
    update(a,size,x);
    update(0,b+1,x);
  }
}

ll Get(int i){
  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++){
    update(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:12:51: error: reference to 'size' is ambiguous
   12 | void update(int a,int b,P x,int k=0,int l=0,int r=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: In function 'void Upd(int, int, P)':
a.cc:26:14: error: reference to 'size' is ambiguous
   26 |     update(a,size,x);
      |              ^~~~
/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:32:6: error: reference to 'size' is ambiguous
   32 |   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:68:3: error: reference to 'size' is ambiguous
   68 |   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:69:10: error: reference to 'size' is ambiguous
   69 |   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:69:23: error: reference to 'size' is ambiguous
   69 |   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:71:14: error: reference to 'size' is ambiguous
   71 |   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:73:15: error: reference to 'size' is ambiguous
   73 |   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;
      |       ^~~~