Problem p02455 (C++) — buggy submission, verdict: Time Limit Exceeded

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

class Treap {
  typedef struct _Node {
    int x, y, cnt;
    _Node *l, *r;
    _Node(int _x)
        : x(_x), y((rand() << 16) ^ rand()), cnt(1), l(NULL), r(NULL) {}
    ~_Node() {
      delete l;
      delete r;
    }
    void recalc() {
      cnt = 1;
      if (l)
        cnt += l->cnt;
      if (r)
        cnt += r->cnt;
    }
  } *Node;
  Node merge(Node l, Node r) {
    if (!l || !r)
      return l ? l : r;
    if (l->y < r->y) {
      l->r = merge(l->r, r);
      l->recalc();
      return l;
    } else {
      r->l = merge(l, r->l);
      r->recalc();
      return r;
    }
  }
  void split(Node v, int x, Node &l, Node &r) {
    l = r = NULL;
    if (!v)
      return;
    if (v->x < x) {
      split(v->r, x, v->r, r);
      l = v;
    } else {
      split(v->l, x, l, v->l);
      r = v;
    }
    v->recalc();
  }
  Node root;

public:
  Treap() : root(NULL) {}
  ~Treap() { delete root; }
  bool search(Node v, int x) {
    if (!v)
      return 0;
    if (v->x == x)
      return 1;
    else if (v->x > x)
      return search(v->l, x);
    else
      return search(v->r, x);
  }
  bool search(int x) { return search(root, x); }
  void insert(int x) {
    if (search(x))
      return;
    Node l, r;
    split(root, x, l, r);
    root = merge(merge(l, new _Node(x)), r);
  }

  void erase(int x) {
    Node l, m, r;
    split(root, x, l, m);
    split(m, x + 1, m, r);
    // assert(m && m->cnt == 1 && m->x == x);
    delete m;
    root = merge(l, r);
  }
  void printTreap(Node v) {
    if (!v)
      return;
    printf("v : %d  p : %d  cnt : %d\\n", v->x, v->y, v->cnt);
    printTreap(v->l);
    printTreap(v->r);
  }
  void printTreap() { printTreap(root); }
  void inorder(Node v) {
    if (!v)
      return;
    inorder(v->l);
    printf("%d ", v->x);
    inorder(v->r);
  }
  void inorder() { inorder(root); }
  /*
  int k_th_node(Node v,int k)
  {
      if()
  }
  int k_th_node(int k)
  {
      return k_th_node(root,k);
  }
  */
  int size() const { return root ? root->cnt : 0; }
};

int main() {
  srand(2333333);
  Treap st;
  int q;
  scanf("%d", &q);
  while (q--) {
    int a, b;
    scanf("%d%d", &a, &b);
    if (a == 0) {
      st.insert(b);
      printf("%d\\n", st.size());
    } else {
      printf("%d\\n", st.search(b));
      // printf("%d\\n",st.find(b)!=st.end());
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

#define INITIAL_CAPACITY 2

void Swap(int *xp, int *yp) {
  int temp = *xp;
  *xp = *yp;
  *yp = temp;
}

static int mSeed;
static int getRand(int num = -1) {
  if (num == -1)
    num = 1000000007;
  mSeed = mSeed * 1103515245 + 37209;
  if (mSeed < 0)
    mSeed *= -1;
  return ((mSeed >> 8) % num);
}

struct Vector {
  int len = 2;
  int curLen = 0;

  int *data = new int[len];

  void push(int val) {
    if (curLen >= len) {
      int *cdata = new int[len << 1];
      for (int i = 0; i < len; i++) {
        cdata[i] = data[i];
      }
      data = cdata;
      len = len << 1;
    }
    data[curLen] = val;
    curLen++;
  }

  void print() {
    for (int i = 0; i < curLen; i++)
      printf("%d ", data[i]);
    printf("\\n");
  }
  void Pop_back() {
    data[curLen - 1] = -1;
    curLen--;
  }
  int length() { return curLen; }
  void QuickSort(int lo, int hi) {
    int pivot = data[(lo + hi) / 2];
    int L = lo;
    int R = hi;
    while (L <= R) {
      while (data[L] < pivot)
        L++;
      while (data[R] > pivot)
        R--;
      if (L <= R) {
        Swap(&data[L], &data[R]);
        L++, R--;
      }
    }
    if (lo < R)
      QuickSort(lo, R);
    if (L < hi)
      QuickSort(L, hi);
  }
  void InsertionSort() {
    int n = curLen;
    for (int i = 1; i < n; i++) {
      int key = data[i];
      int j = i - 1;
      while (j >= 0 && data[j] > key) {
        data[j + 1] = data[j--];
      }
      data[j + 1] = key;
    }
  }

  void Sort(int TYPE = 0) {
    if (TYPE == 0)
      QuickSort(0, curLen - 1);
    else
      InsertionSort();
  }
};

struct Set {
  Vector vc;
  Set() {}
  bool In(int val) {
    int lo = 0;
    int hi = vc.length();
    int cnt = 100, md;

    while (cnt--) {
      md = lo + (hi - lo) / 2;
      if (vc.data[md] == val)
        return 1;
      if (vc.data[md] > val)
        hi = md;
      else
        lo = md;
    }
    if (vc.data[md] == val)
      return 1;
    return 0;
  }
  void Insert(int val) {
    for (int i = 0; i < vc.length(); i++) {
      if (vc.data[i] == val)
        return;
    }
    vc.push(val);
    vc.Sort(1);
  }
  int length() { return vc.curLen; }
};

int main() {
  Set st;
  int a, b, n;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &a, &b);
    if (a == 0) {
      st.Insert(b);
      printf("%d\\n", st.length());
    } else {
      printf("%d\\n", st.In(b));
    }
  }
  return 0;
}