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

#include <bits/stdc++.h>
#define IO                                                                     \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0)
#define endl '\\n'
#define MAXN 200005
#define INF 100000000
// #define int long long

using namespace std;

int N, Q;
int a[2][MAXN << 2];

int init(int sid, int l, int r, int id, int v) {
  if (l == r) {
    return a[sid][id] = v;
  }
  int m = (l + r) >> 1;
  return a[sid][id] = min(init(sid, l, m, id << 1, v),
                          init(sid, m + 1, r, id << 1 | 1, v));
}

int query(int sid, int l, int r, int x, int id) {
  // cerr << l << ' ' << r << ' ' << L << ' ' << R << endl;
  if (r < x || x < l) {
    return INF;
  }
  if (l == x && r == x) {
    return a[sid][id];
  }
  int m = (l + r) >> 1;
  return min({a[sid][id], query(sid, l, m, x, id << 1),
              query(sid, m + 1, r, x, id << 1 | 1)});
}

void update(int sid, int l, int r, int L, int R, int id, int v) {
  // cerr << l << ' ' << r << ' ' << L << ' ' << R << endl;
  if (r < L || R < l) {
    return;
  }
  if (L <= l && r <= R) {
    a[sid][id] = min(a[sid][id], v);
    return;
  }
  int m = (l + r) >> 1;
  update(sid, l, m, L, R, id << 1, v);
  update(sid, m + 1, r, L, R, id << 1 | 1, v);
}

signed main() {
  IO;
  cin >> N >> Q;

  init(0, 1, N, 1, N);
  init(1, 1, N, 1, N);

  long long remain = ((long long)N - 2) * ((long long)N - 2);
  while (Q--) {
    int type, x;
    cin >> type >> x;

    if (type == 1) {
      int mn = query(0, 1, N, x, 1);
      remain -= mn - 2;
      update(1, 1, N, 1, mn, 1, x);
    } else {
      int mn = query(1, 1, N, x, 1);
      remain -= mn - 2;
      update(0, 1, N, 1, mn, 1, x);
    }
  }

  cout << remain << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define IO                                                                     \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0)
#define endl '\\n'
#define MAXN 200005
#define INF 100000000
// #define int long long

using namespace std;

int N, Q;
int a[2][MAXN << 2];

int init(int sid, int l, int r, int id, int v) {
  if (l == r) {
    return a[sid][id] = v;
  }
  int m = (l + r) >> 1;
  return a[sid][id] = min(init(sid, l, m, id << 1, v),
                          init(sid, m + 1, r, id << 1 | 1, v));
}

int query(int sid, int l, int r, int L, int R, int id) {
  // cerr << l << ' ' << r << ' ' << L << ' ' << R << endl;
  if (r < L || R < l) {
    return INF;
  }
  if (l == r) {
    return a[sid][id];
  }
  int m = (l + r) >> 1;
  return min({a[sid][id], query(sid, l, m, L, R, id << 1),
              query(sid, m + 1, r, L, R, id << 1 | 1)});
}

void update(int sid, int l, int r, int L, int R, int id, int v) {
  // cerr << l << ' ' << r << ' ' << L << ' ' << R << endl;
  if (r < L || R < l) {
    return;
  }
  if (L <= l && r <= R) {
    cerr << "Update" << endl;
    a[sid][id] = min(a[sid][id], v);
    return;
  }
  int m = (l + r) >> 1;
  update(sid, l, m, L, R, id << 1, v);
  update(sid, m + 1, r, L, R, id << 1 | 1, v);
  // a[sid][id] = min(a[sid][id << 1], a[sid][id << 1 | 1]);
}

signed main() {
  IO;
  cin >> N >> Q;

  init(0, 1, N, 1, N);
  init(1, 1, N, 1, N);

  long long remain = ((long long)N - 2) * ((long long)N - 2);
  while (Q--) {
    int type, x;
    cin >> type >> x;

    if (type == 1) {
      int mn = query(0, 1, N, x, x, 1);
      remain -= mn - 2;
      update(1, 1, N, 1, mn, 1, x);
    } else {
      int mn = query(1, 1, N, x, x, 1);
      remain -= mn - 2;
      update(0, 1, N, 1, mn, 1, x);
    }
  }

  cout << remain << endl;
  return 0;
}