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

#include <bits/stdc++.h>
#define Mashu cout << "UUZ ate it." << endl
#define RE register int
#define ll long long
using namespace std;

int mn[800010][2];
int vis[200010][2];
long long n;
int ans;

int lc(int x) { return x << 1; }

int rc(int x) { return x << 1 | 1; }

void pushup(int k, int ovo) {
  mn[k][ovo] = min(mn[lc(k)][ovo], mn[rc(k)][ovo]);
}

void build(int k, int l, int r) {
  if (l == r) {
    mn[k][1] = n - 1;
    mn[k][0] = n - 1;
    return;
  }
  int mid = (l + r) >> 1;
  build(lc(k), l, mid);
  build(rc(k), mid + 1, r);
  pushup(k, 1);
  pushup(k, 0);
}

void change(int k, int l, int r, int L, int x, int ovo) {
  if (l == r && l == L) {
    mn[k][ovo] = min(mn[k][ovo], x);
    return;
  }
  int mid = (l + r) >> 1;
  if (L <= mid) {
    change(lc(k), l, mid, L, x, ovo);
  } else {
    change(rc(k), mid + 1, r, L, x, ovo);
  }
  pushup(k, ovo);
}

void query(int k, int l, int r, int L, int R, int ovo) {
  if (L <= l && r <= R) {
    ans = min(ans, mn[k][ovo]);
    return;
  }
  int mid = (l + r) >> 1;
  if (L <= mid) {
    query(lc(k), l, mid, L, R, ovo);
  }
  if (R >= mid + 1) {
    query(rc(k), mid + 1, r, L, R, ovo);
  }
}

int main() {
  int q;
  cin >> n >> q;
  build(1, 1, n - 1);
  long long now = (n - 2) * (n - 2);
  for (int i = 1; i <= q; i++) {
    int opt, k;
    cin >> opt >> k;
    ans = INT_MAX;
    if (opt == 1) {
      if (vis[k][0])
        continue;
      query(1, 1, n - 1, k, n, 0);
      change(1, 1, n - 1, ans, k - 1, 1);
      vis[k][0] = 1;
      now -= ans - 1;
    } else {
      if (vis[k][1])
        continue;
      query(1, 1, n - 1, k, n, 1);
      change(1, 1, n - 1, ans, k - 1, 0);
      vis[k][1] = 1;
      now -= ans - 1;
    }
  }
  cout << now << endl;
  return 0;
  Mashu;
}

Error: TLE

Fixed submission:

#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define Mashu cout << "UUZ ate it." << endl
#define RE register int
#define ll long long
using namespace std;

inline int read() {
  char ch = getchar();
  int x = 0, cf = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      cf = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 3) + (x << 1) + (ch ^ 48);
    ch = getchar();
  }
  if (cf < 0)
    return -x;
  else
    return x;
}

int G[55];
template <class T> inline void write(T x) {
  int g = 0;
  do {
    G[++g] = x % 10;
    x /= 10;
  } while (x);
  for (int i = g; i >= 1; --i)
    putchar('0' + G[i]);
  putchar('\\n');
}

int c[200010], l[200010];
int mn[800010][2];
int vis[200010][2];
int ans;

int lc(int x) { return x << 1; }

int rc(int x) { return x << 1 | 1; }

int pushup(int k, int ovo) { mn[k][ovo] = min(mn[lc(k)][ovo], mn[rc(k)][ovo]); }

void change(int k, int l, int r, int L, int x, int ovo) {
  if (l == r && l == L) {
    mn[k][ovo] = min(mn[k][ovo], x);
    return;
  }
  int mid = (l + r) >> 1;
  if (L <= mid) {
    change(lc(k), l, mid, L, x, ovo);
  } else {
    change(rc(k), mid + 1, r, L, x, ovo);
  }
  pushup(k, ovo);
}

void query(int k, int l, int r, int L, int R, int ovo) {
  if (L <= l && r <= R) {
    ans = min(ans, mn[k][ovo]);
    return;
  }
  int mid = (l + r) >> 1;
  if (L <= mid) {
    query(lc(k), l, mid, L, R, ovo);
  }
  if (R >= mid + 1) {
    query(rc(k), mid + 1, r, L, R, ovo);
  }
}

int main() {
  long long n;
  int q;
  cin >> n >> q;
  for (int i = 1; i <= 800000; i++)
    mn[i][0] = n - 1, mn[i][1] = n - 1;
  long long now = (n - 2) * (n - 2);
  for (int i = 1; i <= q; i++) {
    int opt, k;
    opt = read();
    k = read();
    ans = INT_MAX;
    if (opt == 1) {
      if (vis[k][0])
        continue;
      query(1, 1, n - 1, k, n, 0);
      change(1, 1, n - 1, ans, k - 1, 1);
      vis[k][0] = 1;
      now -= ans - 1;
    } else {
      if (vis[k][1])
        continue;
      query(1, 1, n - 1, k, n, 1);
      change(1, 1, n - 1, ans, k - 1, 0);
      vis[k][1] = 1;
      now -= ans - 1;
    }
  }
  cout << now << endl;
  return 0;
  Mashu;
}