Language: C

#include<stdio.h>
#include<stdlib.h>
typedef int Key;
typedef int Value;
typedef int Result;
struct Treap {
  Key key;
  Value value;
  int p;
  bool cached;
  Result cache;
  Treap *ch[2]; // LEFT = ch[0], RIGHT = ch[1]
  Treap(const Key &key, const Value &value) : key(key), value(value),
    p(rand()), cached(0) { ch[0] = ch[1] = 0; }
};
Treap *rotate(Treap *t, int b) {
  Treap *s = t->ch[1-b]; t->ch[1-b] = s->ch[b]; s->ch[b] = t;
  s->cached = t->cached = false;
  return s;
}
Treap *find(Treap *t, const Key &key) {
  return !t || key == t->key ? t : find(t->ch[key<t->key], key);
}
Treap *insert(Treap *t, const Key &key, const Value &value) {
  if (!t) return new Treap(key, value);
  else if (key == t->key) return t;
  int b = !(key < t->key);
  t->ch[b] = insert(t->ch[b], key, value);
  if (t->p > t->ch[b]->p) t = rotate(t, 1-b);
  t->cached = false;
  return t;
}
Treap *erase(Treap *t, const Key &key) {
  if (!t) return NULL;
  if (key == t->key) {
    if (!t->ch[0] && !t->ch[1]) return NULL;
    else if (!t->ch[0]) t = rotate(t, 0);
    else if (!t->ch[1]) t = rotate(t, 1);
    else t = rotate(t, t->ch[0]->p<t->ch[1]->p);
    t = erase(t, key);
  } else {
    int b = !(key < t->key);
    t->ch[b] = erase(t->ch[b], key);
  }
  t->cached = false;
  return t;
}
Result eval(Treap *t) {
  if (!t) return 0;
  if (!t->cached)
    t->cache = eval(t->ch[0]) + eval(t->ch[1]) + 1; // CHANGE IT FLEXIBLE
  t->cached = true;
  return t->cache;
}
Treap *nth(Treap *t, int n) { // NTH ELEMENT
  int l = eval(t->ch[0]);
  if (n < l) return nth(t->ch[0], n);
  if (n > l) return nth(t->ch[1], n-l-1);
  return t;
}


Compiler output:
main.c:10:3: error: unknown type name 'bool'
   10 |   bool cached;
      |   ^~~~
main.c:3:1: note: 'bool' is defined in header '<stdbool.h>'; this is probably fixable by adding '#include <stdbool.h>'
    2 | #include<stdlib.h>
  +++ |+#include <stdbool.h>
    3 | typedef int Key;
main.c:12:3: error: unknown type name 'Treap'
   12 |   Treap *ch[2]; // LEFT = ch[0], RIGHT = ch[1]
      |   ^~~~~
main.c:13:3: error: expected specifier-qualifier-list before 'Treap'
   13 |   Treap(const Key &key, const Value &value) : key(key), value(value),
      |   ^~~~~
main.c:16:1: error: unknown type name 'Treap'; use 'struct' keyword to refer to the type
   16 | Treap *rotate(Treap *t, int b) {
      | ^~~~~
      | struct 
main.c:16:15: error: unknown type name 'Treap'
   16 | Treap *rotate(Treap *t, int b) {
      |               ^~~~~
main.c:21:1: error: unknown type name 'Treap'; use 'struct' keyword to refer to the type
   21 | Treap *find(Treap *t, const Key &key) {
      | ^~~~~
      | struct 
main.c:21:13: error: unknown type name 'Treap'
   21 | Treap *find(Treap *t, const Key &key) {
      |             ^~~~~
main.c:21:33: error: expected ';', ',' or ')' before '&' token
   21 | Treap *find(Treap *t, const Key &key) {
      |                                 ^
main.c:24:1: error: unknown type name 'Treap'; use 'struct' keyword to refer to the type
   24 | Treap *insert(Treap *t, const Key &key, const Value &value) {
      | ^~~~~
      | struct 
main.c:24:15: error: unknown type name 'Treap'
   24 | Treap *insert(Treap *t, const Key &key, const Value &value) {
      |               ^~~~~
main.c:24:35: error: expected ';', ',' or ')' before '&' token
   24 | Treap *insert(Treap *t, const Key &key, const Value &value) {
      |                                   ^
main.c:33:1: error: unknown type name 'Treap'; use 'struct' keyword to refer to the type
   33 | Treap *erase(Treap *t, const Key &key) {
      | ^~~~~
      | struct 
main.c:33:14: error: unknown type name 'Treap'
   33 | Treap *erase(Treap *t, const Key &key) {
      |              ^~~~~
main.c:33:34: error: expected ';', ',' or ')' before '&' token
   33 | Treap *erase(Treap *t, const Key &key) {
      |                                  ^
main.c:48:13: error: unknown type name 'Treap'
   48 | Result eval(Treap *t) {
      |             ^~~~~
main.c:55:1: error: unknown type name 'Treap'; use 'struct' keyword to refer to the type
   55 | Treap *nth(Treap *t, int n) { // NTH ELEMENT
      | ^~~~~
      | struct 
main.c:55:12: error: unknown type name 'Treap'
   55 | Treap *nth(Treap *t, int n) { // NTH ELEMENT
      |            ^~~~~