Problem:
Problem I: FIMO sequence
Your task is to simulate the sequence defined in the remaining part of the problem description.
This sequence is empty at first.
i
-th element of this sequence is expressed as
a
i
.
The first element of this sequence is
a
1
if the sequence is not empty.
The operation is given by integer from 0 to 9.
The operation is described below.
0:
This query is given with some integer
x
.
If this query is given, the integer x is inserted into the sequence.
If the sequence is empty,
a
1
=
x
.
If the sequence has
n
elements,
a
n+1
=
x
.
Same integer will not appear more than once as
x
.
1:
If this query is given, one element in the sequence is deleted.
The value in the middle of the sequence is deleted.
If the sequence has
n
elements and
n
is even,
a
n/2
will be deleted.
If
n
is odd,
a
⌈n/2⌉
will be deleted.
This query is not given when the sequence is empty.

Assume that the sequence has
a
1
=1,
a
2
=2,
a
3
=3,
a
4
=4 and
a
5
=5.
In this case,
a
3
will be deleted.
After deletion, the sequence will be
a
1
=1,
a
2
=2,
a
3
=4,
a
4
=5.

Assume that the sequence has
a
1
=1,
a
2
=2,
a
3
=3 and
a
4
=4,
In this case,
a
2
will be deleted.
After deletion, the sequence will be
a
1
=1,
a
2
=3,
a
3
=4.
2:
The first half of the sequence is defined by the index from 1 to ⌈
n
/2⌉ .
If this query is given, you should compute the minimum element of the first half of the sequence.
This query is not given when the sequence is empty.
Let me show an example.
Assume that the sequence is {6,2,3,4,5,1,8}.
In this case, the minimum element of the first half of the sequence, {6,2,3,4} is 2.
3:
The latter half of the sequence is elements that do not belong to the first half of the sequence.
If this query is given, you should compute the minimum element of the latter half of the sequence.
This query is not given when the sequence is empty.
Let me show an example.
Assume that the sequence is {6,2,3,4,5,1,8}.
In this case the answer for this query is 1 from {5,1,8}.
4:
This query is given with an integer
i
.
Assume that deletion is repeated until the sequence is empty.
Some elements in the first half of the sequence will become the answer for query 2.
You should compute the
i
-th minimum element from the answers.
This query is not given when the sequence is empty.
You can assume that
i
-th minimum element exists when this query is given.
Let me show an example.
Assume that deletion will be repeated to the sequence {6,2,3,4,5,1,8}.
{6,2,3,4,5,1,8} The minimum element in the first half of the sequence is 2.
{6,2,3,5,1,8}   The minimum element in the first half of the sequence is 2.
{6,2,5,1,8}     The minimum element in the first half of the sequence is 2.
{6,2,1,8}       The minimum element in the first half of the sequence is 2.
{6,1,8}         The minimum element in the first half of the sequence is 1.
{6,8}           The minimum element in the first half of the sequence is 6.
{8}             The minimum element in the first half of the sequence is 8.
{}          
...(truncated)

Submitted solutions:

code_snippet: /*************************************************************************
Author: ziki
Created Time: 2012-10-5 15:32:35
File Name: I.cpp
Description: 
************************************************************************/
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <string.h>


using namespace std;

typedef long long int64;
typedef unsigned long long uint64;
#define two(X) (1<<(X))
#define twoL(X) (((int64)(1))<<(X))
#define contain(S,X) (((S)&two(X))!=0)
#define containL(S,X) (((S)&twoL(X))!=0)
const double pi=acos(-1.0);
const double eps=1e-11;
const int inf=0x7FFFFFFF;
template<class T> inline void checkmin(T &a,T b){if(b<a) a=b;}
template<class T> inline void checkmax(T &a,T b){if(b>a) a=b;}
template<class T> inline T sqr(T x){return x*x;}
typedef pair<int,int> ipair;
#define SIZE(A) ((int)A.size())
#define LENGTH(A) ((int)A.length())
#define MP(A,B) make_pair(A,B)
#define PB(X) push_back(X)
#define zero(a) memset(a, 0, sizeof(a))
#define out(x) (cout<<#x<<": "<<x<<endl)
template<class T>void show(T a, int n){for(int i=0; i<n; ++i) cout<<a[i]<<' '; cout<<endl;}
template<class T>void show(T a, int r, int l){for(int i=0; i<r; ++i)show(a[i],l);cout<<endl;}

const int N = 2e5+10;
int stk[N],sz, mid;
set<pair<int,int> > head,back;
set<pair<int,int> > poshead,posback;
set<pair<int,int> >:: iterator it, it2;
deque<pair<int,int> > headmax, headmin, backmax, backmin;
void addhead(pair<int,int> add) {
	if (headmax.empty() || headmax[0].first < add.first ) headmax.push_front(add);
	if (headmin.empty() || headmin[0].first > add.first ) headmin.push_front(add);
}
void delhead(pair<int,int> del) {
	if (!headmax.empty() && headmax[0] == del ) headmax.pop_front();
	if (!headmin.empty() && headmin[0] == del ) headmin.pop_front();
}
void addback(pair<int,int> add) {
	while( !backmax.empty() && backmax[backmax.size()-1].first < add.first ) backmax.pop_back();
	backmax.push_back(add);
	while( !backmin.empty() && backmin[backmin.size()-1].first > add.first ) backmin.pop_back();
	backmin.push_back(add);
}
void delback(pair<int,int> del) {
	if (!backmax.empty() && backmax[0] == del ) backmax.pop_front();
	if (!backmin.empty() && backmin[0] == del ) backmin.pop_front();
}
void modify() {
	if(back.size() > head.size()) {
		mid = (sz+1)/2;
		it2 = posback.begin();
		int idx = it2->first, val = it2->second;
		poshead.insert(*it2);
		posback.erase(it2);
		it = back.lower_bound(MP(val,idx));
		head.insert(*it);

		addhead(*it);
		delback(*it);
		
		back.erase(it);
	}
}

void ins(int v) {
	addback(MP(v,sz));
	back.insert(MP(v,sz));
	posback.insert(MP(sz,v));
	sz++;
	modify();
}

int del() {
	it2 = poshead.end();
	if( it2 != poshead.begin() ) it2--;
	else return -1;
	int idx = it->first, val = it->second;
	it = head.lower_bound(MP(val,idx));
	poshead.erase(it2);

	delhead(*it);
	head.erase(it);
	modify();
	return idx;
}

void showset(set<pair<int,int> > & s) {
	for(it = s.begin(); it!=s.end(); it++) 
		cout<<" ("<<it->first <<' '<<it->second<<")";
	cout<<endl;
}
//int s[N];
int main()
{
	int q;
	while (~scanf("%d",&q)) {
		if(!q) break;
		headmax.clear(), headmin.clear(), backmax.clear(), backmin.clear();
		sz = 0;
		head.clear(), back.clear();
		for (int i=0; i<q; i++ ) {
			int tp, v, ans;
			scanf("%d",&tp);
			if(tp == 0 || tp == 4 || tp == 5 || tp == 8 || tp == 9) 
				scanf("%d",&v);
			switch(tp){
				case 0: 
					ins(v);
					break;
				case 1:
					it = poshead.end(); it--;
					ans = it->second;
				   	del();
					break;
				case 2:
					ans = head.begin()->first;
					break;
				case 3:
					ans = back.begin()->first;
					break;
				case 6:
					it = head.end(); it--;
					ans = it->first;
					break;
				case 7:
					it = back.end(); it--;
					ans = it->first;
					break;
				default:
					/*
					int tot = head.size() + back.size();
					int cnt = 0;
					for (int i=0; i<tot; i++) {
						if( tp == 4 )
							s[cnt++] = head.begin()->first;
						else if(tp == 5)
							s[cnt++] = back.begin()->first;
						else if(tp == 8) {
							it = head.end(); it--;
							s[cnt++] = it->first;
						} else {
							it = back.end(); it--;
							s[cnt++] = it->first;
						}
						del();
					}
					sort(s,s+cnt);
					cnt = unique(s,s+cnt) - s;
					ans = s[v-1];
					*/
					if( tp == 4 )
						ans = headmin[v-1].first;
					else if(tp == 5)
						ans = backmin[v-1].first;
					else if(tp == 8) {
						ans = headmax[v-1].first;
					} else {
						ans = backmax[v-1].first;
					}
			}
			//showset(poshead);
			//showset(posback);
			if(tp) {
				printf("%d\n",ans);
			}
		}
		puts("end");
	}
	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 5508
final_rank: 10

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
#define chmin(x,y) x = min(x,y)
#define chmax(x,y) x = max(x,y)
#define BIG_NUM 2000000000
#define HUGE_NUM 4000000000000000000 //オーバーフローに注意
#define MOD 998244353
//#define MOD 1000000007
#define EPS 0.000000001



deque<int> L,min_L,max_L;
deque<int> R,min_R,max_R;


void addL(int x){

	L.push_front(x);
	if(min_L.size() == 0 || min_L.front() > x){

		min_L.push_front(x);
	}
	if(max_L.size() == 0 || max_L.front() < x){

		max_L.push_front(x);
	}
}

void addR(int x){

	R.push_back(x);
	while(!min_R.empty() && min_R.back() > x){

		min_R.pop_back();
	}
	min_R.push_back(x);

	while(!max_R.empty() && max_R.back() < x){

		max_R.pop_back();
	}
	max_R.push_back(x);
}

void RtoL(){

	int r = R.front();
	R.pop_front();

	if(min_R.front() == r){

		min_R.pop_front();
	}
	if(max_R.front() == r){

		max_R.pop_front();
	}
	addL(r);
}


void func(int numQ){

	L.clear();
	min_L.clear();
	max_L.clear();

	R.clear();
	min_R.clear();
	max_R.clear();


	int command,x;

	for(int loop = 0; loop < numQ; loop++){

		scanf("%d",&command);

		if(command == 0){

			scanf("%d",&x);

			if(L.size() == 0){

				addL(x);
				continue;
			}

			if(L.size() == R.size()){ //R.frontがLに移動する

				RtoL();
			}

			addR(x);

		}else if(command == 1){

			x = L.front();
			L.pop_front();

			printf("%d\n",x);

			if(min_L.front() == x){

				min_L.pop_front();
			}
			if(max_L.front() == x){

				max_L.pop_front();
			}

			if(L.size() < R.size()){ //R.frontがLに移動する

				RtoL();
			}

		}else if(command == 2){

			printf("%d\n",min_L.front());

		}else if(command == 3){

			printf("%d\n",min_R.front());

		}else if(command == 4){

			scanf("%d",&x);
			x--;
			printf("%d\n",min_L[x]);

		}else if(command == 5){

			scanf("%d",&x);
			x--;
			printf("%d\n",min_R[x]);

		}else if(command == 6){

			printf("%d\n",max_L.front());

		}else if(command == 7){

			printf("%d\n",max_R.front());

		}else if(command == 8){

			scanf("%d",&x);
			x--;
			printf("%d\n",max_L[x]);

		}else{ //command == 9

			scanf("%d",&x);
			x--;
			printf("%d\n",max_R[x]);
		}
	}
	printf("end\n");
}


int main(){

	int numQ;
	while(true){

		scanf("%d",&numQ);
		if(numQ == 0)break;

		func(numQ);
	}

	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3364
final_rank: 6

code_snippet: #pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline double time() {
    return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int q;
    while (cin >> q, q) {
        deque<int> pre, bac;
        deque<int> pmi, pmx, bmi, bmx;
        while (q--) {
            int t; cin >> t;
            if (t == 0) {
                int x; cin >> x;
                bac.push_front(x); // 前が後ろ
                // 末尾からの累積min
                while (bmi.size() and bmi.front() > x) bmi.pop_front();
                bmi.push_front(x);
                // 末尾からの累積max
                while (bmx.size() and bmx.front() < x) bmx.pop_front();
                bmx.push_front(x);
            }
            else if (t == 1) {
                if (pmi.size() and pmi.back() == pre.back()) pmi.pop_back();
                if (pmx.size() and pmx.back() == pre.back()) pmx.pop_back();
                cout << pre.back() << "\n";
                pre.pop_back();
            }
            else if (t == 2) {
                cout << pmi.back() << "\n";
            }
            else if (t == 3) {
                cout << bmi.back() << "\n";
            }
            else if (t == 4) {
                int x; cin >> x;
                cout << pmi[pmi.size()-x] << "\n";
            }
            else if (t == 5) {
                int x; cin >> x;
                cout << bmi[bmi.size()-x] << "\n";
            }
            else if (t == 6) {
                cout << pmx.back() << "\n";
            }
            else if (t == 7) {
                cout << bmx.back() << "\n";
            }
            else if (t == 8) {
                int x; cin >> x;
                cout << pmx[pmx.size()-x] << "\n";
            }
            else if (t == 9) {
                int x; cin >> x;
                cout << bmx[bmx.size()-x] << "\n";
            }

            while (pre.size() < bac.size()) {
                if (bmi.size() and bmi.back() == bac.back()) bmi.pop_back();
                if (bmx.size() and bmx.back() == bac.back()) bmx.pop_back();
                pre.push_back(bac.back());
                bac.pop_back();
                // 先頭からの累積min
                if (!pmi.size() or pmi.back() > pre.back()) pmi.push_back(pre.back());
                // 先頭からの累積max
                if (!pmx.size() or pmx.back() < pre.back()) pmx.push_back(pre.back());
            }
        }
        cout << "end\n";
    }
}
accuracy: 1
time_ms: 20
memory_kb: 3644
final_rank: 8

code_snippet: #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <utility>

using namespace std;
typedef long long ll;
inline int getint(){
  char c; int res = 0;
  for(c = getchar();c < '0' || c > '9';c = getchar());
  for(;c >= '0' && c <= '9';c = getchar()) res = res * 10 + (c - '0');
  return res;
}
deque <int> deq1,deq2,min1,min2,max1,max2;
int q;
void pushb2(int x){
  deq2.push_back(x);
  while(!min2.empty() && min2.back() > x) min2.pop_back();
  min2.push_back(x);
  while(!max2.empty() && max2.back() < x) max2.pop_back();
  max2.push_back(x);
}
int popf2(){
  int tmp = deq2.front();
  deq2.pop_front();
  if(!min2.empty() && min2.front() == tmp) min2.pop_front();
  if(!max2.empty() && max2.front() == tmp) max2.pop_front();
  return tmp;
}
void pushb1(int x){
  deq1.push_back(x);
  if(min1.empty() || min1.back() > x) min1.push_back(x);
  if(max1.empty() || max1.back() < x) max1.push_back(x);
}
int popb1(){
  int tmp = deq1.back();
  deq1.pop_back();
  if(!min1.empty() && min1.back() == tmp) min1.pop_back();
  if(!max1.empty() && max1.back() == tmp) max1.pop_back();
  return tmp;
}

//void print(deque <int> q){
//  for(int i = 0;i < (int)q.size();i++){
//    cout << q[i] << " ";
//  }
//  cout << endl;
//}

int main(){
  while((q = getint()) && q){
    deq1.clear(); min1.clear(); max1.clear();
    deq2.clear(); min2.clear(); max2.clear();
    int len = 0;
    while(q--){
      int fl = getint(), x = 0;
      if(fl == 0 || fl == 4 || fl == 5 || fl == 8 || fl == 9) x = getint();
      if(fl == 0){
        pushb2(x);
        len++;
        if(len & 1){
          int tmp = popf2();
          pushb1(tmp);
        }
      }else if(fl == 1){
        int tmp = popb1();
        printf("%d\n",tmp);
        len--;
        if(len & 1){
          tmp = popf2();
          pushb1(tmp);
        }
      }else if(fl == 2){
        printf("%d\n",min1.back());
      }else if(fl == 3){
        printf("%d\n",min2.front());
      }else if(fl == 4){
        printf("%d\n",min1.at(min1.size() - x));
      }else if(fl == 5){
        printf("%d\n",min2.at(x - 1));
      }else if(fl == 6){
        printf("%d\n",max1.back());
      }else if(fl == 7){
        printf("%d\n",max2.front());
      }else if(fl == 8){
        printf("%d\n",max1.at(max1.size() - x));
      }else if(fl == 9){
        printf("%d\n",max2.at(x - 1));
      }
    }
    puts("end");
  }
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3416
final_rank: 5

code_snippet: #include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}


struct FastIO{
  FastIO(){
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
}fastio_beet;

//INSERT ABOVE HERE
signed main(){
  int q;
  while(cin>>q,q){
    using D = deque<int>;
    auto mv=
      [&](D &a,D &b,D &ax,D &ay,D &bx,D &by){
	if(!bx.empty()&&bx.back()==b.back()) bx.pop_back();
	if(!by.empty()&&by.back()==b.back()) by.pop_back();	  
	
	a.emplace_back(b.back());
	b.pop_back();
	
	if(ax.empty()||ax.back()>a.back()) ax.emplace_back(a.back());
	if(ay.empty()||ay.back()<a.back()) ay.emplace_back(a.back());
      };

    D a,b;
    D ax,ay,bx,by;
    
    while(q--){
      int t,x=-1;
      cin>>t;
      if(t==0||t==4||t==5||t==8||t==9) cin>>x;     

      if(t==0){	
	while(!bx.empty()&&bx.front()>x) bx.pop_front();
	while(!by.empty()&&by.front()<x) by.pop_front();
	b.emplace_front(x);
	bx.emplace_front(x);
	by.emplace_front(x);
      }
      
      if(t==1){
	if(!ax.empty()&&ax.back()==a.back()) ax.pop_back();
	if(!ay.empty()&&ay.back()==a.back()) ay.pop_back();
	cout<<a.back()<<endl;
	a.pop_back();
      }

      if(t==2) cout<<ax.back()<<endl;
      if(t==3) cout<<bx.back()<<endl;

      if(t==4) cout<<ax[ax.size()-x]<<endl;
      if(t==5) cout<<bx[bx.size()-x]<<endl;
            
      if(t==6) cout<<ay.back()<<endl;
      if(t==7) cout<<by.back()<<endl;
      
      if(t==8) cout<<ay[ay.size()-x]<<endl;
      if(t==9) cout<<by[by.size()-x]<<endl;
      
      while(a.size()<b.size())   mv(a,b,ax,ay,bx,by);
      while(a.size()>b.size()+1) mv(b,a,bx,by,ax,ay);

    }
    cout<<"end"<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 160
memory_kb: 3360
final_rank: 9

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

struct FimoSeq
{
  struct FirstHalf
  {
    deque< int > data;
    deque< int > MAX, MIN;

    void add(int x)
    {
      data.push_back(x);

      if (data.size() == 1)
      {
        MAX.push_back(x);
        MIN.push_back(x);
      }
      else
      {
        if (MAX.back() < x)
          MAX.push_back(x);
        if (MIN.back() > x)
          MIN.push_back(x);
      }
    }

    int remove()
    {
      int x = data.back();
      data.pop_back();

      if (MIN.back() == x)
        MIN.pop_back();
      if (MAX.back() == x)
        MAX.pop_back();

      return x;
    }

    int size() const { return data.size(); }
  } fh;

  struct LatterHalf
  {
    deque< int > data;
    deque< int > MAX, MIN;

    void add(int x)
    {
      data.push_back(x);

      if (data.size() == 1)
      {
        MAX.push_back(x);
        MIN.push_back(x);
      }
      else
      {
        while (!MAX.empty() && MAX.back() < x)
          MAX.pop_back();
        while (!MIN.empty() && MIN.back() > x)
          MIN.pop_back();

        MAX.push_back(x);
        MIN.push_back(x);
      }
    }

    int remove()
    {
      int x = data.front();
      data.pop_front();

      if (MIN.front() == x)
        MIN.pop_front();
      if (MAX.front() == x)
        MAX.pop_front();

      return x;
    }

    int size() const { return data.size(); }
  } lh;

  int size() { return fh.size() + lh.size(); }

  void add(int x)
  {
    lh.add(x);
    if (lh.size() > fh.size())
      fh.add(lh.remove());
  }

  int remove()
  {
    int r = fh.remove();
    if (lh.size() > fh.size())
      fh.add(lh.remove());

    return r;
  }

  int min_first_half(int i) { return *(fh.MIN.end() - i); }

  int min_latter_half(int i) { return *(lh.MIN.begin() + i - 1); }

  int max_first_half(int i) { return *(fh.MAX.end() - i); }

  int max_latter_half(int i) { return *(lh.MAX.begin() + i - 1); }
};

int main()
{
  while (true)
  {
    int Q;
    scanf("%d", &Q);
    if (Q == 0)
      break;

    FimoSeq fimo;

    for (int i = 0; i < Q; i++)
    {
      int q, x;
      scanf("%d", &q);

      switch (q)
      {
        case 0:
          scanf("%d", &x);
          fimo.add(x);
          break;
        case 1:
          printf("%d\n", fimo.remove());
          break;
        case 2:
          printf("%d\n", fimo.min_first_half(1));
          break;
        case 3:
          printf("%d\n", fimo.min_latter_half(1));
          break;
        case 4:
          scanf("%d", &x);
          printf("%d\n", fimo.min_first_half(x));
          break;
        case 5:
          scanf("%d", &x);
          printf("%d\n", fimo.min_latter_half(x));
          break;
        case 6:
          printf("%d\n", fimo.max_first_half(1));
          break;
        case 7:
          printf("%d\n", fimo.max_latter_half(1));
          break;
        case 8:
          scanf("%d", &x);
          printf("%d\n", fimo.max_first_half(x));
          break;
        case 9:
          scanf("%d", &x);
          printf("%d\n", fimo.max_latter_half(x));
          break;
      }
    }

    printf("end\n");
  }
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 2908
final_rank: 3

code_snippet: #include <iostream>
#include <list>
#include <deque>

using namespace std;

int Q;
deque<int> dqmin1, dqmin2;
deque<int> dqmax1, dqmax2;
list<int> all;

int main(void)
{
	
	while(1){
		cin >> Q;
		if(Q == 0) break;
		
		int type, x;
		
		all.clear(); all.push_back(0);
		dqmin1.clear(), dqmin2.clear();
		dqmax1.clear(), dqmax2.clear();
		auto center = all.begin();
		
		for(int q = 0; q < Q; q++){
			cin >> type;
			
			if(type == 0){
				cin >> x;
				all.push_back(x);
				
				while(dqmin2.size() && dqmin2.back() > x) dqmin2.pop_back();
				dqmin2.push_back(x);
				while(dqmax2.size() && dqmax2.back() < x) dqmax2.pop_back();
				dqmax2.push_back(x);
				
				if(all.size() % 2 == 0){
					center++;
					if(dqmin2.size() && dqmin2.front() == *center) dqmin2.pop_front();
					if(dqmin1.empty() || dqmin1.back() > *center) dqmin1.push_back(*center);
					if(dqmax2.size() && dqmax2.front() == *center) dqmax2.pop_front();
					if(dqmax1.empty() || dqmax1.back() < *center) dqmax1.push_back(*center);
				}
			}
			
			if(type == 1){
				cout << *center << endl;
				
				if(dqmin1.size() && dqmin1.back() == *center) dqmin1.pop_back();
				if(dqmax1.size() && dqmax1.back() == *center) dqmax1.pop_back();
				
				auto tmp = center; tmp++;
				all.erase(center);
				center = tmp;
				
				if(all.size() % 2) center--;
				else{
					if(dqmin2.size() && dqmin2.front() == *center) dqmin2.pop_front();
					if(dqmin1.empty() || dqmin1.back() > *center) dqmin1.push_back(*center);
					if(dqmax2.size() && dqmax2.front() == *center) dqmax2.pop_front();
					if(dqmax1.empty() || dqmax1.back() < *center) dqmax1.push_back(*center);
				}
			}
			
			if(type == 2){
				cout << dqmin1.back() << endl;
			}
			
			if(type == 3){
				cout << dqmin2.front() << endl;
			}
			
			if(type == 4){
				cin >> x;
				cout << dqmin1[dqmin1.size()-x] << endl;
			}
			
			if(type == 5){
				cin >> x;
				cout << dqmin2[x-1] << endl;
			}
			
			if(type == 6){
				cout << dqmax1.back() << endl;
			}
			
			if(type == 7){
				cout << dqmax2.front() << endl;
			}
			
			if(type == 8){
				cin >> x;
				cout << dqmax1[dqmax1.size()-x] << endl;
			}
			
			if(type == 9){
				cin >> x;
				cout << dqmax2[x-1] << endl;
			}
		}
		
		cout << "end" << endl;
	}
	
	return 0;
}
accuracy: 1
time_ms: 590
memory_kb: 3896
final_rank: 12

code_snippet: /*
 * AOJ 1070: FIMO sequence
 * ?¢???????????§???????????????????10?§??????????????°??????\???????????°????????°???????????°??????????±?????????????????????§????°??????????????????????????????°?????????????¬????????????????????????????????????§????°??????°????±???¶????¬¬i??§??????
 * ?±???????????????????
 * ???????????´???6???????????????????????????????????°??????????°???????????°?????????????????????????????????????????????°??????????°±?????´?°??????\?????´?°?????????????????????????
 */

#include <cstdio>

const int MAXN = 200010;

int la[MAXN], ls[MAXN], lb[MAXN], ra[MAXN], rs[MAXN], rb[MAXN];
int la1, la2, ls1, ls2, lb1, lb2, ra1, ra2, rs1, rs2, rb1, rb2, m;

void outall() {
  printf("----outall----\n");
  for (int i = la1; i < la2; ++i) {
    printf("%d ", la[i]);
  }
  printf("\n");
  for (int i = ls1; i < ls2; ++i) {
    printf("%d ", ls[i]);
  }
  printf("\n");
  for (int i = lb1; i < lb2; ++i) {
    printf("%d ", lb[i]);
  }
  printf("\n");
  for (int i = ra1; i < ra2; ++i) {
    printf("%d ", ra[i]);
  }
  printf("\n");
  for (int i = rs1; i < rs2; ++i) {
    printf("%d ", rs[i]);
  }
  printf("\n");
  for (int i = rb1; i < rb2; ++i) {
    printf("%d ", rb[i]);
  }
  printf("\n");
  printf("\n");
}

void left_push_back(int x) {
  la[la2++] = x;
  if (ls2 == ls1 || ls[ls2 - 1] > x) {
    ls[ls2++] = x;
  }
  if (lb2 == lb1 || lb[lb2 - 1] < x) {
    lb[lb2++] = x;
  }
}

void right_push_back(int x) {
  ra[ra2++] = x;
  while (rs2 > rs1 && rs[rs2 - 1] > x) {
    --rs2;
  }
  rs[rs2++] = x;
  while (rb2 > rb1 && rb[rb2 - 1] < x) {
    --rb2;
  }
  rb[rb2++] = x;
}

int right_pop_front() {
  if (ra[ra1] == rs[rs1]) ++rs1;
  if (ra[ra1] == rb[rb1]) ++rb1;
  ++ra1;
  return ra[ra1 - 1];
}

int left_pop_back() {
  if (la[la2 - 1] == ls[ls2 - 1]) --ls2;
  if (la[la2 - 1] == lb[lb2 - 1]) --lb2;
  --la2;
  return la[la2];
}

void f0_add() {
  int x;
  scanf("%d", &x);
  if (m == 0) {
    left_push_back(x);
  } else {
    right_push_back(x);
    if (!(m & 1)) {
      int y = right_pop_front();
      left_push_back(y);
    }
  }
  ++m;
//  outall();
}

void f1_del() {
  int x = left_pop_back();
  printf("%d\n", x);
  if (!(m & 1)) {
    int y = right_pop_front();
    left_push_back(y);
  }
  --m;
}

void f2_ls() {
  int x = ls[ls2 - 1];
  printf("%d\n", x);
}

void f3_rs() {
  int x = rs[rs1];
  printf("%d\n", x);
}

void f4_ls_() {
  int i, x;
  scanf("%d", &i);
  x = ls[ls2 - i];
  printf("%d\n", x);
}

void f5_rs_() {
  int i, x;
  scanf("%d", &i);
  x = rs[rs1 + i - 1];
  printf("%d\n", x);
}

void f6_lb() {
  int x = lb[lb2 - 1];
  printf("%d\n", x);
}

void f7_rb() {
  int x = rb[rb1];
  printf("%d\n", x);
}

void f8_lb_() {
  int i, x;
  scanf("%d", &i);
  x = lb[lb2 - i];
  printf("%d\n", x);
}

void f9_rb_() {
  int i, x;
  scanf("%d", &i);
  x = rb[rb1 + i - 1];
  printf("%d\n", x);
}

int main() {
//  freopen("/Users/yogy/acm-challenge-workbook/db.in", "r", stdin);
  int n;
  while (scanf("%d", &n) != EOF && n) {
    m = la1 = la2 = ls1 = ls2 = lb1 = lb2 = ra1 = ra2 = rs1 = rs2 = rb1 = rb2 = 0;
    while (n--) {
      int t;
      scanf("%d", &t);
//      printf("type = %d\n", t);
      switch (t) {
        case 0:
          f0_add();
          break;
        case 1:
          f1_del();
          break;
        case 2:
          f2_ls();
          break;
        case 3:
          f3_rs();
          break;
        case 4:
          f4_ls_();
          break;
        case 5:
          f5_rs_();
          break;
        case 6:
          f6_lb();
          break;
        case 7:
          f7_rb();
          break;
        case 8:
          f8_lb_();
          break;
        case 9:
          f9_rb_();
          break;
      }
    }
    printf("end\n");
  }
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 2988
final_rank: 4

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

#define dump(...) cout<<"# "<<#__VA_ARGS__<<'='<<(__VA_ARGS__)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define peri(i,a,b) for(int i=int(b);i-->int(a);)
#define rep(i,n) repi(i,0,n)
#define per(i,n) peri(i,0,n)
#define all(c) begin(c),end(c)
#define mp make_pair
#define mt make_tuple

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;

template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& p){
	return os<<'('<<p.first<<','<<p.second<<')';
}

template<typename Tuple>
void print_tuple(ostream&,const Tuple&){}
template<typename Car,typename... Cdr,typename Tuple>
void print_tuple(ostream& os,const Tuple& t){
	print_tuple<Cdr...>(os,t);
	os<<(sizeof...(Cdr)?",":"")<<get<sizeof...(Cdr)>(t);
}
template<typename... Args>
ostream& operator<<(ostream& os,const tuple<Args...>& t){
	print_tuple<Args...>(os<<'(',t);
	return os<<')';
}

template<typename Ch,typename Tr,typename C>
basic_ostream<Ch,Tr>& operator<<(basic_ostream<Ch,Tr>& os,const C& c){
	os<<'[';
	for(auto i=begin(c);i!=end(c);++i)
		os<<(i==begin(c)?"":" ")<<*i;
	return os<<']';
}

constexpr int INF=1e9;
constexpr int MOD=1e9+7;
constexpr double EPS=1e-9;

template<typename T,template<typename>class Compare>
struct Queue{
	Compare<T> Comp;
	deque<T> a1,a2,b1,b2;
	void Push(int x){
		a2.push_back(x);
		while(b2.size() && Comp(x,b2.back())) b2.pop_back();
		b2.push_back(x);
		if(a1.size()<a2.size()){
			int y=a2.front();
			a1.push_back(y);
			if(b1.empty() || Comp(y,b1.back())) b1.push_back(y);
			a2.pop_front();
			if(b2.size() && b2.front()==y) b2.pop_front();
		}
	}
	int Pop(){
		int x=a1.back();
		a1.pop_back();
		if(b1.size() && x==b1.back()) b1.pop_back();
		if(a1.size()<a2.size()){
			int y=a2.front();
			a1.push_back(y);
			if(b1.empty() || Comp(y,b1.back())) b1.push_back(y);
			a2.pop_front();
			if(b2.size() && b2.front()==y) b2.pop_front();
		}
		return x;
	}
	int PriorLeft(){
		return KthLeft(0);
	}
	int PriorRight(){
		return KthRight(0);
	}
	int KthLeft(int k){
		return b1[b1.size()-1-k];
	}
	int KthRight(int k){
		return b2[k];
	}
};

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	constexpr char endl='\n';
	
	for(int q;cin>>q && q;){
		Queue<int,less> qmin;
		Queue<int,greater> qmax;
		rep(_,q){
			int t; cin>>t;
			if(t==0){ // push
				int x; cin>>x;
				qmin.Push(x),qmax.Push(x);
			}
			if(t==1) // del
				cout<<(qmin.Pop(),qmax.Pop())<<endl;
			if(t==2) // minl
				cout<<qmin.PriorLeft()<<endl;
			if(t==3) // minr
				cout<<qmin.PriorRight()<<endl;
			if(t==4){ // minl kth
				int k; cin>>k;
				cout<<qmin.KthLeft(k-1)<<endl;
			}
			if(t==5){ // maxl kth
				int k; cin>>k;
				cout<<qmin.KthRight(k-1)<<endl;
			}
			if(t==6) // maxl
				cout<<qmax.PriorLeft()<<endl;
			if(t==7) // maxr
				cout<<qmax.PriorRight()<<endl;
			if(t==8){ // maxl kth
				int k; cin>>k;
				cout<<qmax.KthLeft(k-1)<<endl;
			}
			if(t==9){ // maxr kth
				int k; cin>>k;
				cout<<qmax.KthRight(k-1)<<endl;
			}
		}
		cout<<"end"<<endl;
	}
}
accuracy: 1
time_ms: 40
memory_kb: 1372
final_rank: 2

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

#define dump(...) cout<<"# "<<#__VA_ARGS__<<'='<<(__VA_ARGS__)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define peri(i,a,b) for(int i=int(b);i-->int(a);)
#define rep(i,n) repi(i,0,n)
#define per(i,n) peri(i,0,n)
#define all(c) begin(c),end(c)
#define mp make_pair
#define mt make_tuple

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;

template<typename T1,typename T2>
ostream& operator<<(ostream& os,const pair<T1,T2>& p){
	return os<<'('<<p.first<<','<<p.second<<')';
}

template<typename Tuple>
void print_tuple(ostream&,const Tuple&){}
template<typename Car,typename... Cdr,typename Tuple>
void print_tuple(ostream& os,const Tuple& t){
	print_tuple<Cdr...>(os,t);
	os<<(sizeof...(Cdr)?",":"")<<get<sizeof...(Cdr)>(t);
}
template<typename... Args>
ostream& operator<<(ostream& os,const tuple<Args...>& t){
	print_tuple<Args...>(os<<'(',t);
	return os<<')';
}

template<typename Ch,typename Tr,typename C>
basic_ostream<Ch,Tr>& operator<<(basic_ostream<Ch,Tr>& os,const C& c){
	os<<'[';
	for(auto i=begin(c);i!=end(c);++i)
		os<<(i==begin(c)?"":" ")<<*i;
	return os<<']';
}

constexpr int INF=1e9;
constexpr int MOD=1e9+7;
constexpr double EPS=1e-9;

template<typename T,template<typename>class Compare>
struct Queue{
	Compare<T> Comp;
	deque<T> a1,a2;
	deque<T> b1,b2;
	void Push(int x){
		a2.push_back(x);
		while(b2.size() && Comp(x,b2.back())) b2.pop_back();
		b2.push_back(x);
		if(a1.size()<a2.size()){
			int y=a2.front();
			a1.push_back(y);
			if(b1.empty() || Comp(y,b1.back())) b1.push_back(y);
			a2.pop_front();
			if(b2.size() && b2.front()==y) b2.pop_front();
		}
	}
	int Pop(){
		int x=a1.back();
		a1.pop_back();
		if(b1.size() && x==b1.back()) b1.pop_back();
		if(a1.size()<a2.size()){
			int y=a2.front();
			a1.push_back(y);
			if(b1.empty() || Comp(y,b1.back())) b1.push_back(y);
			a2.pop_front();
			if(b2.size() && b2.front()==y) b2.pop_front();
		}
		return x;
	}
	int PriorLeft(){
		return KthLeft(0);
	}
	int PriorRight(){
		return KthRight(0);
	}
	int KthLeft(int k){
		return b1[b1.size()-1-k];
	}
	int KthRight(int k){
		return b2[k];
	}
};
template<typename T,template<typename>class Compare>
ostream& operator<<(ostream& os,const Queue<T,Compare>& q){
	return os<<q.a1<<q.a2<<','<<q.b1<<q.b2;
}

int main()
{
	for(int q;cin>>q && q;){
		Queue<int,less> qmin;
		Queue<int,greater> qmax;
		rep(_,q){
			int t; cin>>t;
			if(t==0){ // push
				int x; cin>>x;
				qmin.Push(x);
				qmax.Push(x);
			}
			if(t==1){ // del
				cout<<qmin.Pop()<<endl;
				qmax.Pop();
			}
			if(t==2){ // minl
				cout<<qmin.PriorLeft()<<endl;
			}
			if(t==3){ // minr
				cout<<qmin.PriorRight()<<endl;
			}
			if(t==4){ // minl kth
				int k; cin>>k;
				cout<<qmin.KthLeft(k-1)<<endl;
			}
			if(t==5){ // maxl kth
				int k; cin>>k;
				cout<<qmin.KthRight(k-1)<<endl;
			}
			if(t==6){ // maxl
				cout<<qmax.PriorLeft()<<endl;
			}
			if(t==7){ // maxr
				cout<<qmax.PriorRight()<<endl;
			}
			if(t==8){ // maxl kth
				int k; cin>>k;
				cout<<qmax.KthLeft(k-1)<<endl;
			}
			if(t==9){ // maxr kth
				int k; cin>>k;
				cout<<qmax.KthRight(k-1)<<endl;
			}
		}
		cout<<"end"<<endl;
	}
}
accuracy: 1
time_ms: 260
memory_kb: 1360
final_rank: 7

code_snippet: #include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <string>
#include <vector>
#include <algorithm>
#include <complex>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <climits>
#include <queue>
#include <set>
#include <map>
#include <valarray>
#include <bitset>
#include <stack>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
typedef long long ll;
typedef pair<int,int> pii;
const int INF = 1<<29;
const double PI = acos(-1);
const double EPS = 1e-8;

int main() {
  int n;
  while(cin>>n,n) {
    deque<int> a;               // O¼ªðO©ç©½ñ
    deque<int> b;               // ã¼ªðãë©ç©½ñ
    deque<int> min1;            // min1[i] = NG4Ì¦
    deque<int> min2;            // min2[i] = NG5Ì¦
    deque<int> max1;            // max1[i] = NG8Ì¦
    deque<int> max2;            // max2[i] = NG9Ì¦
    int m = 0;
    REP(i, n) {
      int q, x;
      cin >> q;
      if (q == 0) {
        cin >> x;
        if (m % 2) {
          b.push_front(x);
          while(min2.size() && min2[min2.size()-1] > x) min2.pop_back();
          min2.push_back(x);
          while(max2.size() && max2[max2.size()-1] < x) max2.pop_back();
          max2.push_back(x);
        } else {
          int y;
          if (m == 0) {
            a.push_back(x);
            y = x; x = -1;
          } else {
            b.push_front(x);
            y = b[b.size()-1];
            a.push_back(y);
            b.pop_back();
            if (min2.size() && min2[0] == y)
              min2.pop_front();
            if (max2.size() && max2[0] == y)
              max2.pop_front();
            while(min2.size() && min2[min2.size()-1] > x) min2.pop_back();
            min2.push_back(x);
            while(max2.size() && max2[max2.size()-1] < x) max2.pop_back();
            max2.push_back(x);
          }
          if (min1.empty() || y < min1[0])
            min1.push_front(y);
          if (max1.empty() || y > max1[0])
            max1.push_front(y);
        }
        m++;
      } else if (q == 1) {
        x = a[a.size()-1];
        a.pop_back();
        if (min1.size() && min1[0] == x)
          min1.pop_front();
        if (max1.size() && max1[0] == x)
          max1.pop_front();
        cout << x << endl;
        if (m % 2 == 0) {
          int y = b[b.size()-1];
          a.push_back(y);
          b.pop_back();
          if (min2.size() && min2[0] == y)
            min2.pop_front();
          if (max2.size() && max2[0] == y)
            max2.pop_front();
          if (min1.empty() || y < min1[0])
            min1.push_front(y);
          if (max1.empty() || y > max1[0])
            max1.push_front(y);
        }
        m--;
      } else if (q == 2) {
        cout << min1[0] << endl;
      } else if (q == 3) {
        cout << min2[0] << endl;
      } else if (q == 4) {
        cin >> x;x--;
        cout << min1[x] << endl;
      } else if (q == 5) {
        cin >> x;x--;
        cout << min2[x] << endl;
      } else if (q == 6) {
        cout << max1[0] << endl;
      } else if (q == 7) {
        cout << max2[0] << endl;
      } else if (q == 8) {
        cin >> x;x--;
        cout << max1[x] << endl;
      } else {
        cin >> x;x--;
        cout << max2[x] << endl;
      }

      // cout << "a = "; FOR(it, a) cout << *it << " "; cout << endl;
      // cout << "b = "; FOR(it, b) cout << *it << " ";cout << endl;
      // // cout << "min1 = "; FOR(it, min1) cout << *it << " ";cout << endl;
      // // cout << "min2 = "; FOR(it, min2) cout << *it << " ";cout << endl;
      // // cout << "max1 = "; FOR(it, max1) cout << *it << " ";cout << endl;
      // cout << "max2 = "; FOR(it, max2) cout << *it << " ";cout << endl;
      
    }
    cout << "end" << endl;
  }
}
accuracy: 1
time_ms: 630
memory_kb: 1080
final_rank: 11

code_snippet: #include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <assert.h>
#include <vector>
#include <deque>

using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c) for (__typeof((c).begin())it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

deque<int> deq[2];
deque<int> minDeq[2][2];
int q;

void Erase(int lr) {
  assert(!minDeq[0][lr].empty());
  assert(!minDeq[1][lr].empty());
  if (minDeq[0][lr].front() == deq[lr][0]) {
    minDeq[0][lr].pop_front();
  }
  if (minDeq[1][lr].front() == -deq[lr][0]) {
    minDeq[1][lr].pop_front();
  }
  deq[lr].pop_front();
}

void Insert(int lr, int tb, int v) {
  if (tb == 1) {
    assert(lr == 1);
    while (!minDeq[0][lr].empty() && v < minDeq[0][lr].back()) {
      minDeq[0][lr].pop_back();
    }
    while (!minDeq[1][lr].empty() && -v < minDeq[1][lr].back()) {
      minDeq[1][lr].pop_back();
    }
    minDeq[0][lr].push_back(v);
    minDeq[1][lr].push_back(-v);
    deq[lr].push_back(v);
  } else {
    if (minDeq[0][lr].empty() || v < minDeq[0][lr].front()) {
      minDeq[0][lr].push_front(v);
    }
    if (minDeq[1][lr].empty() || -v < minDeq[1][lr].front()) {
      minDeq[1][lr].push_front(-v);
    }
    deq[lr].push_front(v);
  }
}

int main() {
  while (scanf("%d", &q), q) {
    REP(i, 2) {
      deq[i].clear();
      REP(j, 2){
        minDeq[i][j].clear();
      }
    }
    REP(iter, q) {
      int type;
      int v = 0xdeadbeaf;
      scanf("%d", &type);
      if (type == 0 || type == 4 || type == 5 || type == 8 || type == 9) {
        scanf("%d", &v);
        if (type != 0) { v--; }
      }
      switch (type) {
case 0:
        Insert(1, 1, v);
        break;
case 1:
        printf("%d\n", deq[0][0]);
        Erase(0);
        break;
case 2:
        printf("%d\n", minDeq[0][0][0]);
        break;
case 3:
        printf("%d\n", minDeq[0][1][0]);
        break;
case 4:
        printf("%d\n", minDeq[0][0][v]);
        break;
case 5:
        printf("%d\n", minDeq[0][1][v]);
        break;
case 6:
        printf("%d\n", -minDeq[1][0][0]);
        break;
case 7:
        printf("%d\n", -minDeq[1][1][0]);
        break;
case 8:
        printf("%d\n", -minDeq[1][0][v]);
        break;
case 9:
        printf("%d\n", -minDeq[1][1][v]);
        break;
default:
        assert(false);
        break;
      }
      if (deq[0].size() < deq[1].size()) {
        Insert(0, 0, deq[1][0]);
        Erase(1);
      }
    }
    puts("end");
  }
}
accuracy: 1
time_ms: 80
memory_kb: 0
final_rank: 1