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

#include <deque>
#include <iostream>
#include <utility>
using namespace std;
#define F first
#define S second
#define PB push_back
template <class T> struct chain {
  deque<pair<T, chain *>> v;
  chain(void) {}
  void push(T n) {
    if (v.empty() || n <= v.back().F)
      v.PB({n, nullptr});
    else if (n > v.front().F)
      v.push_front({n, nullptr});
    else {
      int l = 0, r = static_cast<int>(v.size()), mid;
      while (r - l > 1) {
        mid = (r + l) >> 1;
        if (v[mid].F < n)
          r = mid;
        else
          l = mid;
      }
      if (!v[l].S)
        v[l].S = new chain<T>();
      v[l].S->push(n);
    }
    return;
  }
  void pop() {
    if (v.size() > 1) {
      if (v[0].S) {
        v[0].F = v[0].S->v[0].F;
        if (v[0].S->v.size() > 1)
          v[0].S->pop();
        else
          v[0].S = nullptr;
      } else
        v.pop_front();
    }
    return;
  }
  T top() { return v[0].F; }
  void print() {
    for (int i = 0; i < v.size(); i++) {
      printf("%d ", v[i].F);
      if (v[i].S) {
        printf("to: ");
        v[i].S->print();
      }
    }
    printf(":end ");
  }
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  chain<int> *root = new chain<int>();
  string s;
  int temp;
  while (true) {
    cin >> s;
    if (s.size() < 5)
      return 0;
    else if (s[0] == 'i') {
      cin >> temp;
      root->push(temp);
    } else {
      cout << root->top() << endl;
      if (root->v.size() == 1)
        root->v.pop_front();
      else
        root->pop();
    }
    // root->print();
    // printf("\\n\\n");
  }
}


Error: TLE

Fixed submission:

#include <deque>
#include <iostream>
#include <utility>
using namespace std;
#define F first
#define S second
#define PB push_back
template <class T> struct chain {
  deque<pair<T, chain *>> v;
  chain(void) {}
  void push(T n) {
    if (v.empty() || n <= v.back().F)
      v.PB({n, nullptr});
    else if (n > v.front().F)
      v.push_front({n, nullptr});
    else {
      int l = 0, r = static_cast<int>(v.size()), mid;
      while (r - l > 1) {
        mid = (r + l) >> 1;
        if (v[mid].F < n)
          r = mid;
        else
          l = mid;
      }
      if (!v[l].S)
        v[l].S = new chain<T>();
      v[l].S->push(n);
    }
    return;
  }
  void pop() {
    if (v.size() > 1) {
      if (v[0].S) {
        v[0].F = v[0].S->v[0].F;
        if (v[0].S->v.size() > 1)
          v[0].S->pop();
        else
          v[0].S = nullptr;
      } else
        v.pop_front();
    }
    return;
  }
  T top() { return v[0].F; }
  void print() {
    for (int i = 0; i < v.size(); i++) {
      printf("%d ", v[i].F);
      if (v[i].S) {
        printf("to: ");
        v[i].S->print();
      }
    }
    printf(":end ");
  }
};
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  chain<int> *root = new chain<int>();
  string s;
  int temp;
  while (true) {
    cin >> s;
    if (s.size() < 0)
      return 0;
    else if (s[0] == 'i') {
      cin >> temp;
      root->push(temp);
    } else {
      cout << root->top() << endl;
      if (root->v.size() == 1)
        root->v.pop_front();
      else
        root->pop();
    }
    // root->print();
    // printf("\\n\\n");
  }
}