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

#include <bits/stdc++.h>
using namespace std;
#define r(i, n) for (auto i = 0; i < n; i++)
#define s(c) static_cast<int>((c).size())

struct Node {
  int key;
  Node *l;
  Node *r;
  Node *p;
};
Node *nil = new Node{INT_MAX};

void insert(Node *T, int key) {
  Node *z = new Node{key, nil, nil, nil};
  if (T->key == INT_MAX) {
    *T = *z;
    return;
  }
  Node *x = T;
  Node *y = x->p;
  while (x != nil) {
    y = x;
    x = (key < x->key) ? x->l : x->r;
  }
  z->p = y;
  if (key < y->key) {
    y->l = z;
  } else {
    y->r = z;
  }
}

bool find(Node *node, int key) {
  if (node == nil)
    return false;
  int cmp = node->key;
  if (key == cmp)
    return true;

  return find((key < cmp) ? node->l : node->r, key);
}

vector<int> pre, in;
void preorder(Node *node) {
  pre.push_back(node->key);
  if (node->l != nil)
    preorder(node->l);
  if (node->r != nil)
    preorder(node->r);
}

void inorder(Node *node) {
  if (node->l != nil)
    inorder(node->l);
  in.push_back(node->key);
  if (node->r != nil)
    inorder(node->r);
}

void print(vector<int> v) {
  r(i, s(v)) cout << ' ' << v[i];
  cout << endl;
}

void printPreorder(Node *node) {
  preorder(node);
  print(pre);
  pre.clear();
}

void printInorder(Node *node) {
  inorder(node);
  print(in);
  in.clear();
}

int main() {
  int m;
  cin >> m;

  Node *root = new Node{INT_MAX};
  string cmd;
  int arg;
  r(i, m) {
    cin >> cmd;
    if (cmd[0] == 'i') {
      cin >> arg;
      insert(root, arg);
    } else if (cmd[0] == 'f') {
      cin >> arg;
      cout << (find(root, arg) ? "yes" : "no") << endl;
    } else {
      printInorder(root);
      printPreorder(root);
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define r(i, n) for (auto i = 0; i < n; i++)
#define s(c) static_cast<int>((c).size())

struct Node {
  int key;
  Node *l;
  Node *r;
  Node *p;
};
Node *nil = new Node{INT_MAX};

void insert(Node *T, int key) {
  Node *z = new Node{key, nil, nil, nil};
  if (T->key == INT_MAX) {
    *T = *z;
    return;
  }
  Node *x = T;
  Node *y = x->p;
  while (x != nil) {
    y = x;
    x = (key < x->key) ? x->l : x->r;
  }
  z->p = y;
  if (key < y->key) {
    y->l = z;
  } else {
    y->r = z;
  }
}

bool find(Node *node, int key) {
  if (node->key == key)
    return true;
  bool b1 = false, b2 = false;
  if (node->l != nil)
    b1 = find(node->l, key);
  if (node->r != nil)
    b2 = find(node->r, key);
  return b1 || b2;
}

vector<int> pre, in;
void preorder(Node *node) {
  pre.push_back(node->key);
  if (node->l != nil)
    preorder(node->l);
  if (node->r != nil)
    preorder(node->r);
}

void inorder(Node *node) {
  if (node->l != nil)
    inorder(node->l);
  in.push_back(node->key);
  if (node->r != nil)
    inorder(node->r);
}

void print(vector<int> v) {
  r(i, s(v)) cout << ' ' << v[i];
  cout << endl;
}

void printPreorder(Node *node) {
  preorder(node);
  print(pre);
  pre.clear();
}

void printInorder(Node *node) {
  inorder(node);
  print(in);
  in.clear();
}

int main() {
  int m;
  cin >> m;

  Node *root = new Node{INT_MAX};
  string cmd;
  int arg;
  r(i, m) {
    cin >> cmd;
    if (cmd[0] == 'i') {
      cin >> arg;
      insert(root, arg);
    } else if (cmd[0] == 'f') {
      cin >> arg;
      cout << (find(root, arg) ? "yes" : "no") << endl;
    } else {
      printInorder(root);
      printPreorder(root);
    }
  }

  return 0;
}