import java.io.*;
import java.util.*;
class Main{
int N
= Integer.
parseInt(br.
readLine()); AVLTree tree = new AVLTree();
for(int i=0; i<N; i++){
int m
= Integer.
parseInt(br.
readLine()); tree.insert(m);
int r = tree.greaterThan(m)+1;
}
}
}
class AVLTree{
static class Node{
int key, height, size;
Node left, right;
Node(int k){
key = k;
height = size = 1;
}
void update(){
height
= Math.
max(getHeight
(left
), getHeight
(right
)) + 1; size = getSize(left) + getSize(right) + 1;
}
static int getHeight(Node n){
return n==null?0:n.height;
}
static int getSize(Node n){
return n==null?0:n.size;
}
}
Node root;
Node rotateRight(Node p){
Node c = p.left;
Node g = c.right;
p.left = g;
c.right = p;
p.update();
c.update();
return c;
}
Node rotateLeft(Node p){
Node c = p.right;
Node g = c.left;
p.right = g;
c.left = p;
p.update();
c.update();
return c;
}
Node insert(Node n, int key){
if(n==null)
return new Node(key);
if(key<n.key)
n.left = insert(n.left, key);
else if(key>n.key)
n.right = insert(n.right, key);
else
return n;
n.update();
int bal = Node.getHeight(n.left) - Node.getHeight(n.right);
if(bal>1 && key<n.left.key)
return rotateRight(n);
if(bal<-1 && key>n.right.key)
return rotateLeft(n);
if(bal>1 && key>n.left.key){
n.left = rotateLeft(n.left);
return rotateRight(n);
}
if(bal<-1 && key<n.right.key){
n.right = rotateRight(n.right);
return rotateLeft(n);
}
return n;
}
void insert(int key){
root = insert(root, key);
}
int greaterThan(int key){
Node n = root;
int count = 0;
while(n!=null){
if(key<n.key){
count += 1 + Node.getSize(n.right);
n = n.left;
}
else if(key==n.key){
count += Node.getSize(n.right);
break;
}
else
n = n.right;
}
return count;
}
}
aW1wb3J0IGphdmEuaW8uKjsKaW1wb3J0IGphdmEudXRpbC4qOwoKY2xhc3MgTWFpbnsKICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZyBhcmdzW10pIHRocm93cyBFeGNlcHRpb257CiAgICAgICAgQnVmZmVyZWRSZWFkZXIgYnIgPSBuZXcgQnVmZmVyZWRSZWFkZXIobmV3IElucHV0U3RyZWFtUmVhZGVyKFN5c3RlbS5pbikpOwogICAgICAgIGludCBOID0gSW50ZWdlci5wYXJzZUludChici5yZWFkTGluZSgpKTsKICAgICAgICBBVkxUcmVlIHRyZWUgPSBuZXcgQVZMVHJlZSgpOwogICAgICAgIGZvcihpbnQgaT0wOyBpPE47IGkrKyl7CiAgICAgICAgICAgIGludCBtID0gSW50ZWdlci5wYXJzZUludChici5yZWFkTGluZSgpKTsKICAgICAgICAgICAgdHJlZS5pbnNlcnQobSk7CiAgICAgICAgICAgIGludCByID0gdHJlZS5ncmVhdGVyVGhhbihtKSsxOwogICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4ocik7CiAgICAgICAgfQogICAgfQp9CgpjbGFzcyBBVkxUcmVlewogICAgc3RhdGljIGNsYXNzIE5vZGV7CiAgICAgICAgaW50IGtleSwgaGVpZ2h0LCBzaXplOwogICAgICAgIE5vZGUgbGVmdCwgcmlnaHQ7CgogICAgICAgIE5vZGUoaW50IGspewogICAgICAgICAgICBrZXkgPSBrOwogICAgICAgICAgICBoZWlnaHQgPSBzaXplID0gMTsKICAgICAgICB9CgogICAgICAgIHZvaWQgdXBkYXRlKCl7CiAgICAgICAgICAgIGhlaWdodCA9IE1hdGgubWF4KGdldEhlaWdodChsZWZ0KSwgZ2V0SGVpZ2h0KHJpZ2h0KSkgKyAxOwogICAgICAgICAgICBzaXplID0gZ2V0U2l6ZShsZWZ0KSArIGdldFNpemUocmlnaHQpICsgMTsKICAgICAgICB9CgogICAgICAgIHN0YXRpYyBpbnQgZ2V0SGVpZ2h0KE5vZGUgbil7CiAgICAgICAgICAgIHJldHVybiBuPT1udWxsPzA6bi5oZWlnaHQ7CiAgICAgICAgfQoKICAgICAgICBzdGF0aWMgaW50IGdldFNpemUoTm9kZSBuKXsKICAgICAgICAgICAgcmV0dXJuIG49PW51bGw/MDpuLnNpemU7CiAgICAgICAgfQogICAgfQoKICAgIE5vZGUgcm9vdDsKCiAgICBOb2RlIHJvdGF0ZVJpZ2h0KE5vZGUgcCl7CiAgICAgICAgTm9kZSBjID0gcC5sZWZ0OwogICAgICAgIE5vZGUgZyA9IGMucmlnaHQ7CiAgICAgICAgcC5sZWZ0ID0gZzsKICAgICAgICBjLnJpZ2h0ID0gcDsKICAgICAgICBwLnVwZGF0ZSgpOwogICAgICAgIGMudXBkYXRlKCk7CgogICAgICAgIHJldHVybiBjOwogICAgfQoKICAgIE5vZGUgcm90YXRlTGVmdChOb2RlIHApewogICAgICAgIE5vZGUgYyA9IHAucmlnaHQ7CiAgICAgICAgTm9kZSBnID0gYy5sZWZ0OwogICAgICAgIHAucmlnaHQgPSBnOwogICAgICAgIGMubGVmdCA9IHA7CiAgICAgICAgcC51cGRhdGUoKTsKICAgICAgICBjLnVwZGF0ZSgpOwoKICAgICAgICByZXR1cm4gYzsKICAgIH0KCiAgICBOb2RlIGluc2VydChOb2RlIG4sIGludCBrZXkpewoKICAgICAgICBpZihuPT1udWxsKQogICAgICAgICAgICByZXR1cm4gbmV3IE5vZGUoa2V5KTsKCiAgICAgICAgaWYoa2V5PG4ua2V5KQogICAgICAgICAgICBuLmxlZnQgPSBpbnNlcnQobi5sZWZ0LCBrZXkpOwogICAgICAgIGVsc2UgaWYoa2V5Pm4ua2V5KQogICAgICAgICAgICBuLnJpZ2h0ID0gaW5zZXJ0KG4ucmlnaHQsIGtleSk7CiAgICAgICAgZWxzZQogICAgICAgICAgICByZXR1cm4gbjsKICAgICAgICBuLnVwZGF0ZSgpOwoKICAgICAgICBpbnQgYmFsID0gTm9kZS5nZXRIZWlnaHQobi5sZWZ0KSAtIE5vZGUuZ2V0SGVpZ2h0KG4ucmlnaHQpOwoKICAgICAgICBpZihiYWw+MSAmJiBrZXk8bi5sZWZ0LmtleSkKICAgICAgICAgICAgcmV0dXJuIHJvdGF0ZVJpZ2h0KG4pOwogICAgICAgIGlmKGJhbDwtMSAmJiBrZXk+bi5yaWdodC5rZXkpCiAgICAgICAgICAgIHJldHVybiByb3RhdGVMZWZ0KG4pOwogICAgICAgIGlmKGJhbD4xICYmIGtleT5uLmxlZnQua2V5KXsKICAgICAgICAgICAgbi5sZWZ0ID0gcm90YXRlTGVmdChuLmxlZnQpOwogICAgICAgICAgICByZXR1cm4gcm90YXRlUmlnaHQobik7CiAgICAgICAgfQogICAgICAgIGlmKGJhbDwtMSAmJiBrZXk8bi5yaWdodC5rZXkpewogICAgICAgICAgICBuLnJpZ2h0ID0gcm90YXRlUmlnaHQobi5yaWdodCk7CiAgICAgICAgICAgIHJldHVybiByb3RhdGVMZWZ0KG4pOwogICAgICAgIH0KCiAgICAgICAgcmV0dXJuIG47CiAgICB9CgogICAgdm9pZCBpbnNlcnQoaW50IGtleSl7CiAgICAgICAgcm9vdCA9IGluc2VydChyb290LCBrZXkpOwogICAgfQoKICAgIGludCBncmVhdGVyVGhhbihpbnQga2V5KXsKICAgICAgICBOb2RlIG4gPSByb290OwogICAgICAgIGludCBjb3VudCA9IDA7CiAgICAgICAgd2hpbGUobiE9bnVsbCl7CiAgICAgICAgICAgIGlmKGtleTxuLmtleSl7CiAgICAgICAgICAgICAgICBjb3VudCArPSAxICsgTm9kZS5nZXRTaXplKG4ucmlnaHQpOwogICAgICAgICAgICAgICAgbiA9IG4ubGVmdDsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlIGlmKGtleT09bi5rZXkpewogICAgICAgICAgICAgICAgY291bnQgKz0gTm9kZS5nZXRTaXplKG4ucmlnaHQpOwogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZQogICAgICAgICAgICAgICAgbiA9IG4ucmlnaHQ7CiAgICAgICAgfQogICAgICAgIHJldHVybiBjb3VudDsKICAgIH0KfQ==