Problem:
ソート
高校入学後、プログラミング部に入部したタケ子さんは、次第にアルゴリズムの面白さにのめりこんでいきました。いまでは、２年生になったらプログラミング甲子園に出場してみたいと考えています。
あるとき、ソート・アルゴリズムについて学んだタケ子さんは、ソート・アルゴリズムを自分で設計してみました。タケ子さんが作ったソート・アルゴリズムでは、入力として要素の間に重複のない、１個以上の自然数からなる列が与えられたとき、以下の処理を実行します。
はじめに、列の先頭の要素を選ぶ。
選んだ要素の直前に要素があるとき、選んだ要素とその直前の要素を比べる。直前の要素のほうが大きいなら、それを列の末尾の直後に移動させる(図）。この操作を、選んだ要素が列の先頭になるか、選んだ要素よりその直前の要素の方が小さくなるまで続ける。
選んだ要素が列の末尾なら終了。そうでなければ、選んだ要素の直後の要素を新たに選び、2 へ戻る。
タケ子さんはこのアルゴリズムがどのくらいの計算時間を必要とするか見積もるために、要素を列の末尾の直後に移動させる操作の回数を数えることにしました。
列の情報を入力とし、要素を列の末尾の直後に移動させる操作の回数を報告するプログラムを作成せよ。
Input
入力は以下の形式で与えられる。
N
a
1
a
2
...
a
N
１行目に列に含まれる要素の個数
N
(1 ≤
N
≤ 200000) が与えられる。２行目に列の要素
a
i
(1 ≤
a
i
≤ 10
9
) が先頭から順番に与えられる。要素
a
i
に重複はない。
Output
要素を列の末尾の直後に移動させる操作の回数を１行に出力する。
Sample Input 1
6
1 3 6 5 8 2
Sample Output 1
10
入力例１では、要素の移動が行われるたびに、以下のように列が変化していく。
0回目： 1 3 6 5 8 2
 1回目： 1 3 5 8 2 6
 2回目： 1 3 5 2 6 8
 3回目： 1 3 2 6 8 5
 4回目： 1 2 6 8 5 3
 5回目： 1 2 6 5 3 8
 6回目： 1 2 5 3 8 6
 7回目： 1 2 3 8 6 5
 8回目： 1 2 3 6 5 8
 9回目： 1 2 3 5 8 6
10回目： 1 2 3 5 6 8
Sample Input 2
4
4 3 2 1
Sample Output 2
6

Submitted solutions:

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
#include <deque>
using namespace std;

int find_parent(vector<int>& parent, int x) {
    if (parent[x] < 0) return x;
    return parent[x] = find_parent(parent, parent[x]);
}

void union_sets(vector<int>& parent, int x, int y) {
    x = find_parent(parent, x);
    y = find_parent(parent, y);
    if (x == y) return;
    
    if (parent[x] > parent[y]) swap(x, y);
    parent[x] += parent[y];
    parent[y] = x;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    
    int n;
    cin >> n;
    
    vector<pair<int, int>> elements(n);
    for (int i = 0; i < n; i++) {
        cin >> elements[i].first;
        elements[i].second = i;
    }
    
    sort(elements.begin(), elements.end());
    
    vector<int> parent(n, -1);
    deque<int> dq;
    for (int i = 0; i < n; i++) {
        dq.push_back(i);
    }
    
    long long result = 0;
    
    for (int i = 0; i < n; i++) {
        while (true) {
            int u = find_parent(parent, elements[i].second);
            int v = find_parent(parent, dq.front());
            dq.pop_front();
            
            if (u == v) break;
            
            union_sets(parent, u, v);
        }
        
        int u = find_parent(parent, elements[i].second);
        result += -parent[u] - 1;
        parent[u]++;
        
        if (parent[u] < 0) {
            dq.push_back(u);
        }
    }
    
    cout << result << endl;
    
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 6236
final_rank: 1

code_snippet: #include<iostream>
#include<stack>
#include<queue>
#include<algorithm>
#include<set>
using namespace std;
int N;
long ans;
int A[2<<17],V[2<<17];
queue<set<int> >P;
stack<int>S;
void ad(set<int>&s)
{
	if(!P.empty()&&*P.back().begin()>*s.rbegin())
	{
		if(P.back().size()<s.size())P.back().swap(s);
		P.back().merge(s);
	}
	else
	{
		P.push(set<int>());
		P.back().swap(s);
	}
}
int main()
{
	cin>>N;
	for(int i=0;i<N;i++)
	{
		cin>>A[i];
		V[i]=A[i];
	}
	sort(V,V+N);
	for(int i=0;i<N;i++)
	{
		int a=lower_bound(V,V+N,A[i])-V;
		set<int>s={a};
		ad(s);
	}
	while(!P.empty())
	{
		set<int>p;
		p.swap(P.front());
		P.pop();
		int mn=*p.begin();
		while(!S.empty()&&S.top()>mn)
		{
			p.insert(S.top());
			S.pop();
		}
		S.push(mn);
		p.erase(p.begin());
		ans+=p.size();
		if(!p.empty())ad(p);
	}
	cout<<ans<<endl;
}
accuracy: 1
time_ms: 1240
memory_kb: 24092
final_rank: 16

code_snippet: #include<iostream>
#include<stack>
#include<queue>
#include<algorithm>
#include<set>
using namespace std;
int N;
long ans;
int A[2<<17],V[2<<17];
queue<set<int> >P;
stack<int>S;
void ad(set<int>&s)
{
	if(!P.empty()&&*P.back().begin()>*s.rbegin())
	{
		P.back().merge(s);
	}
	else
	{
		P.push(set<int>());
		P.back().swap(s);
	}
}
int main()
{
	cin>>N;
	for(int i=0;i<N;i++)
	{
		cin>>A[i];
		V[i]=A[i];
	}
	sort(V,V+N);
	for(int i=0;i<N;i++)
	{
		int a=lower_bound(V,V+N,A[i])-V;
		set<int>s={a};
		ad(s);
	}
	while(!P.empty())
	{
		set<int>p;
		p.swap(P.front());
		P.pop();
		int mn=*p.begin();
		while(!S.empty()&&S.top()>mn)
		{
			p.insert(S.top());
			S.pop();
		}
		S.push(mn);
		p.erase(p.begin());
		ans+=p.size();
		if(!p.empty())ad(p);
	}
	cout<<ans<<endl;
}
accuracy: 1
time_ms: 1230
memory_kb: 24024
final_rank: 15

code_snippet: #include<deque>
#include<queue>
#include<vector>
#include<algorithm>
#include<iostream>
#include<set>
#include<cmath>
#include<tuple>
#include<string>
#include<chrono>
#include<functional>
#include<iterator>
#include<random>
#include<unordered_set>
#include<array>
#include<map>
#include<iomanip>
#include<assert.h>
#include<list>
#include<bitset>
#include<stack>
#include<memory>
#include<numeric>
using namespace std;
using namespace std::chrono;
typedef long long int llint;
typedef long double lldo;
#define mp make_pair
#define mt make_tuple
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define fir first
#define sec second
#define res resize
#define ins insert
#define era erase
#define REP(i, n) for(int i = 0;i < (n);i++)
/*cout<<fixed<<setprecision(20);cin.tie(0);ios::sync_with_stdio(false);*/
const llint mod=1000000007;
const llint inf=2.19e18+1;
const long double pai=3.141592653589793238462643383279502884197;
const long double eps=1e-10;
template <class T,class U>bool chmin(T& a,U b){if(a>b){a=b;return true;}return false;}
template <class T,class U>bool chmax(T& a,U b){if(a<b){a=b;return true;}return false;}
llint gcd(llint a,llint b){if(a%b==0){return b;}else return gcd(b,a%b);}
llint lcm(llint a,llint b){if(a==0){return b;}return a/gcd(a,b)*b;}
template<class T> void SO(T& ve){sort(ve.begin(),ve.end());}
template<class T> void REV(T& ve){reverse(ve.begin(),ve.end());}
template<class T>llint LBI(const vector<T>&ar,T in){return lower_bound(ar.begin(),ar.end(),in)-ar.begin();}
template<class T>llint UBI(const vector<T>&ar,T in){return upper_bound(ar.begin(),ar.end(),in)-ar.begin();}

int main(void){
	int i,n;cin>>n;
	vector<int>a(n);
	for(i=0;i<n;i++){cin>>a[i];}
	auto sa=a;SO(sa);
	set<int>kara;
	deque<set<int>>b(n);
	for(i=0;i<n;i++){b[i].ins(LBI(sa,a[i]));}
	llint ans=0;
	for(i=0;i<n;i++){
		while(b[0].count(i)==0){
			auto c=move(b[0]);
			b.pop_front();
			if(b[0].size()<c.size()){swap(b[0],c);}
			for(auto it:c){b[0].ins(it);}
		}
		b[0].erase(i);
		ans+=b[0].size();
		b.push_back(kara);
		swap(b[0],b.back());
		b.pop_front();
	}
	cout<<ans<<endl;
}
accuracy: 1
time_ms: 180
memory_kb: 23752
final_rank: 9

code_snippet: #include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

template <typename T>
using GPQ = priority_queue<T, vector<T>, greater<T>>;

int main() {
    int n;
    cin >> n;
    vector<int> v(n);
    for (auto &&i : v) scanf("%d", &i);
    auto z = v;
    sort(z.begin(), z.end());
    z.erase(unique(z.begin(), z.end()), z.end());
    for (auto &&j : v) j = lower_bound(z.begin(),z.end(), j) - z.begin();

    auto merge = [&](GPQ<int> &a, GPQ<int> &b){
        if(a.size() < b.size()) swap(a, b);
        while(!b.empty()){
            a.emplace(b.top());
            b.pop();
        }
    };
    ll ans = 0;
    queue<int> Q;
    vector<GPQ<int>> a(n);
    for (int i = 0; i < n; ++i) {
        a[i].emplace(i);
        Q.emplace(v[i]);
    }
    for (int i = 0; i < n; ++i) {
        int x = Q.front(); Q.pop();
        while(a[x].top() != i) merge(a[x], a[Q.front()]), Q.pop();
        a[x].pop();
        ans += a[x].size();
        if(!a[x].empty()) Q.emplace(x);
    }
    cout << ans << "\n";
    return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 19400
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
long long kq,f[200005],n,u,v;
deque<int> dq;
pair<int,int> a[200005];
int phin(int u)
{
    return f[u]<=0 ? u : f[u]=phin(f[u]);
}
void uni(int u,int v)
{
    if (f[u]>f[v]) swap(u,v);
    f[u]+=f[v];
    f[v]=u;
}
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin>>n;
    for (int i=1;i<=n;++i)
    {
        cin>>a[i].first;
        a[i].second=i;
        dq.push_back(i);
    }

    sort(a+1,a+n+1);
    memset(f,-1,sizeof(f));
    for (int i=1;i<=n;++i)
    {
        while (1)
        {
            u=phin(a[i].second);
            v=phin(dq.front());dq.pop_front();
            if (u==v) break;
            uni(u,v);
        }
        u=phin(a[i].second);
        kq=kq-f[u]-1;
        f[u]++;
        if (f[u]<0) dq.push_back(u);
    }
    cout<<kq<<endl;
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 6808
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;

using S = set<int>;

S* merge(S* a, S* b){
    if(a->size() < b->size()) swap(a, b);
    for(auto x : *b){
        a->insert(x);
    }
    return a;
}

void compress(vector<int> &v){
    auto tmp = v;
    sort(tmp.begin(), tmp.end());
    map<int,int> ord;
    for(int i=0;i<int(tmp.size());i++){
        ord[tmp[i]] = i;
    }
    for(auto& x : v) x = ord[x];
}

int main() {
    int n;
    cin >> n;
    vector<int> a(n);
    for(int i=0;i<n;i++){
        cin >> a[i];
    }
    compress(a);
    vector<S> sv(n);
    queue<S(*)> que;
    for(int i=0;i<n;i++){
        sv[i].insert(a[i]);
        que.push(&sv[i]);
    }
    long long ans = 0;
    for(int i=0;i<n;i++){
        while(1){
            auto s = que.front();
            que.pop();
            if(s->find(i) != s->end()){
                s->erase(i);
                ans += s->size();
                if(!s->empty()) que.push(s);
                break;
            }else{
                que.front() = merge(que.front(), s);
            }
        }
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 230
memory_kb: 36220
final_rank: 12

code_snippet: #include <bits/stdc++.h>

using namespace std;
typedef set<int> si;
const int N=2e5+10;
int n; si a[N];
queue<si*> q;
priority_queue<int, vector<int>, greater<int> > vals;

si* smer (si* s, si* t) {
    if (s->size()<t->size()) swap(s,t);
    while (!t->empty()) {
        s->insert(*(t->begin()));
        t->erase(t->begin());
    }
    return s;
}

int main()
{/*
    freopen("input.txt","r",stdin); //*/
    scanf("%d",&n);
    for (int i=1; i<=n; ++i) {
        int u; scanf("%d",&u);
        a[i].insert(u); vals.push(u);
    }
    for (int i=1; i<=n; ++i) {
        q.push(&a[i]);
    }
    long long ans = 0ll;
    while (!vals.empty()) {
        int u = vals.top(); vals.pop();
        si* dummy = q.front(); q.pop();
        while (!dummy->count(u)) {
            dummy = smer(dummy, q.front());
            q.pop();
        }
        dummy->erase(u);
        ans += dummy->size();
        if (!dummy->empty())
            q.push(dummy);
    }
    printf("%lld\n",ans);
    return 0;
}
accuracy: 1
time_ms: 130
memory_kb: 24084
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ll = long long;

int main() {
    int n; cin >> n;
    vector<int> a(n);
    for(int i = 0; i < n; ++i) {
        cin >> a[i];
    }

    { // compress
        auto b = a;
        sort(begin(b), end(b));
        for(int i = 0; i < n; ++i) {
            a[i] = lower_bound(begin(b), end(b), a[i]) - begin(b);
        }
    }

    using S = priority_queue<int, vector<int>, greater<>>;
    function<void(S&, S&)> merge = [] (S& s1, S& s2) {
        if(s1.size() < s2.size()) swap(s1, s2);
        while(!s2.empty()) {
            s1.push(s2.top()); s2.pop();
        }
    };

    queue<shared_ptr<S>> que;
    for(int i = 0; i < n; ++i) {
        auto q = make_shared<S>(); q->push(a[i]);
        que.push(move(q));
    }

    ll ans = 0;
    for(int i = 0; i < n; ++i) {
        auto s = que.front(); que.pop();
        while(s->top() != i) {
            auto v = que.front(); que.pop();
            merge(*s, *v);
        }
        s->pop();
        ans += s->size();
        que.push(s);
    }

    cout << ans << endl;
}
accuracy: 1
time_ms: 100
memory_kb: 25720
final_rank: 8

code_snippet: #include <stdio.h>
#include <queue>
#include <stack>
using namespace std;
int main(){
    int n,temp,last;
    bool up=true;
    long long int ans=0;
    queue<priority_queue<int,vector<int>,greater<int>>*>q;
    stack<int> s;
    priority_queue<int,vector<int>,greater<int>>* pq;
    scanf("%d",&n);
    while(n--){
        scanf("%d",&temp);
        if(!s.empty()){
            if(up){
                if(temp<s.top())up=false;
            }
            else{
                if(temp>s.top()){
                    last=temp;
                    temp=s.top();
                    s.pop();
                    pq=new priority_queue<int,vector<int>,greater<int>>;
                    pq->push(s.top());
                    s.pop();
                    while(!s.empty()){
                        if(s.top()>temp){
                            pq->push(s.top());
                            s.pop();
                        }
                        else break;
                    }
                    ans+=pq->size();
                    q.push(pq);
                    up=true;
                    s.push(temp);
                    temp=last;
                }
            }
        }
        s.push(temp);
    }
    if(!up){
        temp=s.top();
        s.pop();
        pq=new priority_queue<int,vector<int>,greater<int>>;
        pq->push(s.top());
        s.pop();
        while(!s.empty()){
            if(s.top()>temp){
                pq->push(s.top());
                s.pop();
            }
            else break;
        }
        ans+=pq->size();
        q.push(pq);
        s.push(temp);
    }
    while(!q.empty()){
        pq=q.front();
        q.pop();
        if(pq->empty())continue;
        temp=pq->top();
        pq->pop();
        while(!s.empty()){
            if(s.top()>temp){
                pq->push(s.top());
                s.pop();
            }
            else break;
        }
        s.push(temp);
        ans+=pq->size();
        q.push(pq);
    }
    printf("%lld\n",ans);
}
accuracy: 1
time_ms: 1170
memory_kb: 13748
final_rank: 13

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


typedef set<int> Group;

Group* make_group(int num){
	Group *new_group = new Group();
	new_group->insert(num);
	return new_group;
}

Group* merge(Group* left,Group* right){
	if(left->size() < right->size()){
		swap(left,right);
	}
	for(Group::iterator it = right->begin(); it != right->end(); it++)left->insert(*it);

	return left;
}

int main(){

	int N;
	scanf("%d",&N);
	queue<Group*> Q;

	vector<int> V,WORK;

	int tmp;
	map<int,int> MAP;

	for(int i = 0; i < N; i++){
		scanf("%d",&tmp);
		V.push_back(tmp);
		WORK.push_back(tmp);
	}

	sort(WORK.begin(),WORK.end());

	for(int i = 0; i < N; i++){
		MAP[WORK[i]] = i+1;
	}
	for(int i = 0; i < N; i++){
		V[i] = MAP[V[i]];
	}

	for(int i = 0; i < N; i++){
		Q.push(make_group(V[i]));
	}

	ll ans = 0;
	for(int num = 1; num <= N; num++){
		Group* group = Q.front();
		Q.pop();

		while(true){
			if(*(group->begin()) == num){
				group->erase(group->begin());
				ans += group->size();
				if(group->size() > 0){
					Q.push(group);
				}
				break;
			}
			Group* next_group = Q.front();
			Q.pop();
			group = merge(group,next_group);
		}
	}

	printf("%lld\n",ans);

	return 0;
}
accuracy: 1
time_ms: 190
memory_kb: 49808
final_rank: 14

code_snippet: #include <bits/stdc++.h>
#define FOR(i, a, b)    for (int i = a; i <= b; ++i)
#define ii              pair <int, int>
using namespace std;
const int N = 2e5 + 3;
int n, dsu[N];
long long res;
vector <ii> V;
deque <int> dq;

void init() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    //freopen("main.inp","r",stdin);
    //freopen("main.out","w",stdout);
}

int root(int x) { return dsu[x] <= 0 ? x : dsu[x] = root(dsu[x]); }
void merge(int u, int v) {
    if (dsu[u] > dsu[v]) swap(u, v);
    dsu[u] += dsu[v], dsu[v] = u;
}

void solve() {
    cin >> n;
    FOR(i, 1, n) { int x; cin >> x; V.push_back({ x, i }); dq.push_back(i); }

    memset(dsu, -1, sizeof dsu);
    sort(V.begin(), V.end());
    for (ii it : V) {
        while (1) {
            int id1 = root(it.second);
            int id2 = root(dq.front()); dq.pop_front();
            if (id2 == id1) break;
            merge(id1, id2);
        }
        int id1 = root(it.second);
        res += 0LL - dsu[id1] - 1; dsu[id1]++;
        if (dsu[id1] < 0) dq.push_back(id1);
    }

    cout << res << '\n';
}

main() {
    init(); solve();
}
accuracy: 1
time_ms: 30
memory_kb: 6224
final_rank: 2

code_snippet: #include <bits/stdc++.h>

#define fto(i, x, y) for(int i = (x); i <= (y); ++i)
#define fdto(i, x, y) for(int i = (x); i >= (y); --i)
#define ftoa(i, x, y, a) for(int i = (x); i <= (y); i += a)
#define ftoit(it, var) for (__typeof(var.begin()) it = var.begin(); it != var.end(); ++it)

#define ii pair<int, int>
#define mp make_pair
#define FF first
#define SS second

#define vi vector<int>
#define vii vector<ii>
#define ll long long
#define vll vector<ll>

#define eb emplace_back
#define ef emplace_front
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()

#define oo 1000000007
#define OO 1000000000000000007LL

using namespace std;

#define Pro "tmp"
#define maxN 300007

int a[maxN], b[maxN];
set <int> st[maxN];

int mergeSet(int a, int b) {
    if ((int)st[a].size() < (int)st[b].size()) swap(a, b);
    ftoit (it, st[b]) st[a].insert(*it);
    st[b].clear();
    return a;
}

int main() {

    int n;
    scanf("%d", &n);

    fto (i, 1, n) {
        scanf("%d", &a[i]);
        b[i] = a[i];
    }
    sort(b+1, b+n+1);

    queue <int> q;
    fto (i, 1, n) {
        st[i].insert(a[i]);
        q.push(i);
    }

    ll sol = 0LL;
    fto (i, 1, n) {
        int ans = 0;
        set <int> cur_set;

        int new_idx = 0;
        while (!q.empty()) {
            int idx = q.front();
            q.pop();

            ans += (int)st[idx].size();
            if (st[idx].count(b[i])) {
                st[idx].erase(b[i]);
                new_idx = mergeSet(idx, new_idx);

                if ((int)st[new_idx].size()) q.push(new_idx);
                break;
            }

            new_idx = mergeSet(idx, new_idx);
        }

        sol += ans-1;
    }

    printf("%lld\n", sol);
    return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 32804
final_rank: 11

code_snippet: #include    <bits/stdc++.h>
#define fo(i,a,b) for(int i=a;i<=b;++i)
#define fd(i,a,b) for(int i=a;i>=b;--i)
#define rep(i,a,b)  for(int i=a;i<b;++i)
#define fore(i,a)  for(__typeof(a.begin()) i=a.begin();i!=a.end();++i)
#define __unique(V) V.resize(unique(V.begin(),V.end())-V.begin())
#define cntbit(X)   __builtin_popcount(X)
#define fi  first
#define se  second
#define LL  long long
#define ii  pair<int,int>
#define iii pair<int,ii>
#define TASK "sort"
using   namespace   std;
LL  ans,n;
int dd;
ii  a[200005];
///--------------------------
void    readf(){
    cin>>n;
    fo(i,1,n) cin>>a[i].fi;
    fo(i,1,n) a[i].se=i;
}
///--------------------------
void    solve(){
    sort(a+1,a+n+1);
    a[0].se=0;
    ans=0;dd=n;
    fo(i,1,n){
        ans+=a[i].se-a[i-1].se-1;
        if (a[i].se>a[i+1].se){
            ans+=((LL)(n-i-1)*(n-i))/2;
            break;
        }
        if (a[i-1].se<a[i].se-1)
        fo(j,i+2,n) if (a[i].se>a[j].se){
            fo(t,j+1,n) if (a[i].se>a[t].se)
                a[t].se=++dd;
            a[j].se=++dd;
            break;
        }
    }
    cout<<ans<<'\n';
}
///--------------------------
int     main(){
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    readf();
    solve();
}
accuracy: 0.875
time_ms: 2130
memory_kb: 4804
final_rank: 18

code_snippet: #include    <bits/stdc++.h>
#define fo(i,a,b) for(int i=a;i<=b;++i)
#define fd(i,a,b) for(int i=a;i>=b;--i)
#define rep(i,a,b)  for(int i=a;i<b;++i)
#define fore(i,a)  for(__typeof(a.begin()) i=a.begin();i!=a.end();++i)
#define __unique(V) V.resize(unique(V.begin(),V.end())-V.begin())
#define cntbit(X)   __builtin_popcount(X)
#define fi  first
#define se  second
#define LL  long long
#define ii  pair<int,int>
#define iii pair<int,ii>
#define TASK "sort"
using   namespace   std;
LL  ans,n;
int dd;
ii  a[200005];
///--------------------------
void    readf(){
    cin>>n;
    fo(i,1,n) cin>>a[i].fi;
    fo(i,1,n) a[i].se=i;
}
///--------------------------
void    solve(){
    sort(a+1,a+n+1);
    a[0].se=0;
    ans=0;dd=n;
    fo(i,1,n){
        ans+=a[i].se-a[i-1].se-1;
        if (a[i].se>a[i+1].se){
            ans+=((n-i-1)*(n-i))/2;
            break;
        }
        if (a[i-1].se<a[i].se-1)
        fo(j,i+2,n) if (a[i].se>a[j].se){
            fo(t,j+1,n) if (a[i].se>a[t].se)
                a[t].se=++dd;
            a[j].se=++dd;
            break;
        }
    }
    cout<<ans<<'\n';
}
///--------------------------
int     main(){
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    readf();
    solve();
}
accuracy: 0.875
time_ms: 2140
memory_kb: 4756
final_rank: 19

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define removeDuplicate(a) sort(a.begin(), a.end()); a.resize(distance(a.begin(), unique(a.begin(), a.end())));


const int N = 6e5 + 10; 

int n;
int a[N];  
set<int> *q[N]; 

void merge (set<int> *&a, set<int> *&b) {
	if (a -> size() > b -> size()) { 
		for (auto value : *b) { 
			a -> insert(value); 
		}
		b -> clear(); 
	}
	else { 
		for (auto value : *a) { 
			b -> insert(value); 
		}
		a -> clear(); 
		a = b; 
	}
}

void zip () { 
	vector<int> rar; 
	for (int i = 1; i <= n; i++) { 
		rar.push_back(a[i]); 
	}
	removeDuplicate(rar); 
	for (int i = 1; i <= n; i++) { 
		a[i] = lower_bound(rar.begin(), rar.end(), a[i]) - rar.begin() + 1; 
	}
}

int main()
{
    #define file "9"
    //freopen(file".inp", "r", stdin); freopen(file".out", "w", stdout);

    cin >> n; 
    for (int i = 1; i <= 3 * n; i++) { 
    	q[i] = new set<int>; 
    }
    for (int i = 1; i <= n; i++) { 
    	int value; 
    	cin >> value;
    	a[i] = value;  
    }
    zip(); 
    for (int i = 1; i <= n; i++) { 
    	q[i] -> insert(a[i]); 
    }
    long long ret = 0; 
    int front = 1, back = n; 
    for (int value = 1; value <= n; value++) { 
    	for (int i = front; i <= back; i++) { 
    		merge(q[back + 1], q[i]);
    		front++;  
    		if (q[back + 1] -> find(value) != q[back + 1] -> end()) { 
    			break; 
    		}
    	}
    	back++; 
    	q[back] -> erase(value); 
    	if (q[back] -> empty()) { 
    		back--; 
    		continue; 	
    	}
    	ret += ((int)(q[back] -> size()));
    }
    cout << ret << endl; 
    return 0;
}
accuracy: 1
time_ms: 170
memory_kb: 60148
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define removeDuplicate(a) sort(a.begin(), a.end()); a.resize(distance(a.begin(), unique(a.begin(), a.end())));


const int N = 2e5 + 10; 

int n;
int a[N];  
set<int> *q[N]; 

void merge (set<int> *&a, set<int> *&b) {
	if (a -> size() > b -> size()) { 
		for (auto value : *b) { 
			a -> insert(value); 
		}
		b -> clear(); 
	}
	else { 
		for (auto value : *a) { 
			b -> insert(value); 
		}
		a -> clear(); 
		a = b; 
	}
}

void zip () { 
	vector<int> rar; 
	for (int i = 1; i <= n; i++) { 
		rar.push_back(a[i]); 
	}
	removeDuplicate(rar); 
	for (int i = 1; i <= n; i++) { 
		a[i] = lower_bound(rar.begin(), rar.end(), a[i]) - rar.begin() + 1; 
	}
}

int main()
{
    #define file "9"
    //freopen(file".inp", "r", stdin); freopen(file".out", "w", stdout);

    cin >> n; 
    for (int i = 1; i <= 3 * n; i++) { 
    	q[i] = new set<int>; 
    }
    for (int i = 1; i <= n; i++) { 
    	int value; 
    	cin >> value;
    	a[i] = value;  
    }
    zip(); 
    for (int i = 1; i <= n; i++) { 
    	q[i] -> insert(a[i]); 
    }
    long long ret = 0; 
    int front = 1, back = n; 
    for (int value = 1; value <= n; value++) { 
    	for (int i = front; i <= back; i++) { 
    		merge(q[back + 1], q[i]);
    		front++;  
    		if (q[back + 1] -> find(value) != q[back + 1] -> end()) { 
    			break; 
    		}
    	}
    	back++; 
    	q[back] -> erase(value); 
    	if (q[back] -> empty()) { 
    		back--; 
    		continue; 	
    	}
    	ret += ((int)(q[back] -> size()));
    }
    cout << ret << endl; 
    return 0;
}
accuracy: 0.625
time_ms: 10
memory_kb: 8768
final_rank: 20

code_snippet: #include <bits/stdc++.h>
#define up(i, a, b) for (int i = a, _b = b; i <= _b; i++)
#define down(i, a, b) for (int i = a, _b = b; i >= _b; i--)
#define long long long
#define endl '\n'
#define X first
#define Y second
#define II pair<int, int>
#define debug(X) cerr << #X << " = " << X << endl
#define debug2(X, Y) cerr << #X << " = " << X << " , " << #Y << " = " << Y << endl
#define show(X, a, b) { cerr << #X << " = "; up(_, a, b) cerr << X[_] << " "; cerr << endl; }
#define gc getchar
#define pc putchar
using namespace std;
inline void read(int &x) {
        register int c = gc();
        x = 0;
        int neg = 0;
        for (; ((c<48 || c>57) && c != '-'); c = gc());
        if(c=='-') {neg=1; c=gc();}
        for(; c>47 && c<58; c = gc()) {x = (x<<1) + (x<<3) + c - 48;}
        if(neg) x=-x;
}
inline void writeln(int x) {

        char buffor[21];
        register int i=0;
        int neg=0; if (x<0) {neg=1; x= -x;}
        do {
                buffor[i++]=(x%10)+'0';
                x/=10;
        } while(x);
        i--;
        if (neg) pc('-');
        while(i>=0) pc(buffor[i--]);
        pc('\n');
}
int n, a[200010];
map<int, int> rr;
priority_queue<int, vector<int>, greater<int> > pq[200010];
deque<int> dq;
void input() {
        cin >> n;
        up(i, 1, n) cin >> a[i], rr[a[i]] = 1;
        int cnt = 0;
        for (auto u: rr) rr[u.X] = ++cnt;
        up(i, 1, n) a[i] = rr[a[i]];
}
void merge(int u, int v) {
        if (pq[u].size() > pq[v].size()) pq[u].swap(pq[v]);
        while (!pq[u].empty()) pq[v].push(pq[u].top()), pq[u].pop();
}
void solve() {
        up(i, 1, n) pq[i].push(a[i]), dq.push_back(i);
        long res = 0;
        up(i, 1, n) {
            while (dq.size() > 1 && pq[dq.front()].top() != i) {
                int u = dq.front(); dq.pop_front();
                int v = dq.front(); merge(u, v);
            }
            int v = dq.front(); dq.pop_front();
            pq[v].pop(); res += pq[v].size();
            if (!pq[v].empty()) dq.push_back(v);
        }
        cout << res << endl;
}
int main() {
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);

  #ifdef I_Love_Pork
  #define TASK "tmp"
        freopen(TASK ".inp","r",stdin);
        freopen(TASK ".out","w",stdout);
  #endif


        input();
        solve();



        return 0;
}
accuracy: 1
time_ms: 180
memory_kb: 27840
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
int n;
int a[200007],b[200007],pset[200007];
long long ans=0;
queue <int> q;
int fset(int x){
	if(pset[x]<=0) return x;
	return pset[x]=fset(pset[x]);
}
void unite(int x,int y){
	x=fset(x);
	y=fset(y);
	if(x==y) return;
	if(pset[x]<=pset[y]){
		pset[x]+=pset[y];
		pset[y]=x;
	}
	else{
		pset[y]+=pset[x];
		pset[x]=y;
	}
}
bool cmp(int x,int y){
	return (a[x]<a[y] || (a[x]==a[y] && x<y));
}
int main(){
	ios_base::sync_with_stdio(0);
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		pset[i]=-1;
		b[i]=i;
	}
	sort(b+1,b+1+n,cmp);
	for(int i=1;i<=n;i++){
		a[b[i]]=i;
	}
	for(int i=1;i<=n;i++) q.push(a[i]);
	for(int i=1;i<=n;i++){
		//cout<<b[i]<<endl;
		while(1){
			int x=q.front();
			q.pop();
			if(fset(x)!=fset(i)){
				unite(x,i);
			}
			else break;
		}
		ans-=pset[fset(i)]+1;
		pset[fset(i)]++;
		q.push(fset(i));
	}
	cout<<ans<<"\n";
}
accuracy: 1
time_ms: 40
memory_kb: 6080
final_rank: 3

code_snippet: #include<bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
#define mk make_pair
#define f first
#define s second

const int N = 2e5 + 7;
int t, n, m;
int a[N], dad[N];
ll num[N], res;
deque<int> now;
ii val[N];

int root(int u){ return (u == dad[u] ? u : dad[u] = root(dad[u])); }

main()
{
#define file "r"
//    freopen(file".inp", "r", stdin);
//  freopen(file".out", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin >> n;
    for(int i = 1; i <= n; i ++)
    {
        cin >> a[i];
        val[i] = mk(a[i], i);
        dad[i] = i; now.push_back(i); num[i] = 1;
    }
    sort(val + 1, val + n + 1);
    for(int i = 1; i <= n; i ++)
    {
        t = root(val[i].s);
        while(1)
        {
            int tmp = root(now.front()); now.pop_front();
            if(t == tmp) break;
            if(num[t] > num[tmp])
            {
                num[t] += num[tmp]; num[tmp] = 0; dad[tmp] = t;
            }
            else
            {
                swap(t, tmp);
                num[t] += num[tmp]; num[tmp] = 0; dad[tmp] = t;
            }
        }
        res = res + 1LL*num[t] - 1LL; num[t] --;
//        cout << i << "  " << t << " " << num[t] << "\n";
        if(num[t] > 0) now.push_back(t);
//        cout << i << "  " << res << "  " << t << "\n";
    }
    cout << res << "\n";
    return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 8340
final_rank: 5