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

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
#define MAXN 100000 + 2
int S[MAXN], Q[MAXN];
int n, m;
int gg = 0;
void input() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> S[i];
  }
  cin >> m;
  for (int i = 0; i < m; ++i) {
    cin >> Q[i];
  }
}
bool pd(int x) {
  int l = 0, r = n - 1;
  while (r > l) {
    int mid = (r + l) / 2;
    if (S[mid] == x)
      return true;
    if (S[mid] > x)
      r = mid;
    else
      l = mid + 1;
  }
  if (S[r] != x && S[l] != x)
    return false;
  else
    return true;
}
void output() { cout << gg << endl; }
int main() {
  input();
  for (int i = 0; i < m; ++i) {
    if (pd(Q[i]))
      gg++;
  }
  output();
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
using namespace std;
struct Node {
  int left_child, right_child;
  int sui, val;
  int fa;
} num[100010];
int n, len;
int sav[100010], zong;
void left_ch(int t) {
  int f = num[t].fa;
  num[f].right_child = num[t].left_child;
  num[num[t].left_child].fa = f;
  num[t].left_child = f;
  num[t].fa = num[f].fa;
  num[f].fa = t;
}

void right_ch(int t) {
  int f = num[t].fa;
  num[f].left_child = num[t].right_child;
  num[num[t].right_child].fa = f;
  num[t].right_child = f;
  num[t].fa = num[f].fa;
  num[f].fa = t;
}

int find_min() {
  int wz = 0;
  while (num[wz].left_child != -1) {
    wz = num[wz].left_child;
  }
  return num[wz].val;
}

int find_max() {
  int wz = 0;
  while (num[wz].right_child != -1) {
    wz = num[wz].right_child;
  }
  return num[wz].val;
}

int find_flag(int w) {
  int wz = 0;
  while (1) {
    if (num[wz].val > w) {
      if (num[wz].left_child == -1)
        break;
      wz = num[wz].left_child;
    } else if (num[wz].val < w) {
      if (num[wz].right_child == -1)
        break;
      wz = num[wz].right_child;
    } else if (num[wz].val == w)
      return wz;
  }
  return -1;
}

void insert(int w) {
  int o = rand() * rand(), wz = 0;
  int pla = len;
  if (zong != 0)
    pla = sav[--zong];
  else
    len++;
  num[pla].sui = o, num[pla].val = w, num[pla].left_child = -1,
  num[pla].right_child = -1;
  if (len == 0) {
    len = 1;
    return;
  }
  while (1) {
    if (num[wz].val > w) {
      if (num[wz].left_child == -1) {
        num[wz].left_child = len;
        break;
      }
      wz = num[wz].left_child;
    } else {
      if (num[wz].right_child == -1) {
        num[wz].right_child = len;
        break;
      }
      wz = num[wz].right_child;
    }
  }
  num[pla].fa = wz;
  while (1) {

    if (num[wz].right_child != -1 &&
        num[num[wz].right_child].sui < num[wz].sui) {
      left_ch(num[wz].right_child);
      wz = num[num[wz].fa].fa;
    } else if (num[wz].left_child != -1 &&
               num[num[wz].left_child].sui < num[wz].sui) {
      left_ch(num[wz].left_child);
      wz = num[num[wz].fa].fa;
    } else
      break;
  }
}

void delet(int w) {
  int wz = find_flag(w);
  while (num[wz].left_child != -1 || num[wz].right_child != -1) {
    if (num[wz].left_child == -1)
      right_ch(num[wz].right_child);
    else if (num[wz].right_child == -1)
      left_ch(num[wz].left_child);
    else {
      if (num[num[wz].left_child].sui < num[num[wz].right_child].sui)
        left_ch(num[wz].left_child);
      else
        right_ch(num[wz].right_child);
    }
  }
  if (num[num[wz].fa].left_child == wz)
    num[num[wz].fa].left_child = -1;
  else
    num[num[wz].fa].right_child = -1;
  sav[zong++] = wz;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    int www;
    cin >> www;
    insert(www);
  }
  cin >> n;
  int tot = 0;
  for (int i = 0; i < n; i++) {
    int www;
    cin >> www;
    if (find_flag(www) != -1)
      tot++;
  }
  cout << tot << "\\n";
  return 0;
}