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

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll, ll> P;
typedef pair<int, int> pii;
typedef vector<P> vpl;
typedef tuple<ll, ll, ll> tapu;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1 << 30;
const ll linf = 1LL << 62;
const int MAX = 2020000;
ll dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};
ll dx[8] = {0, 0, 1, -1, 1, -1, -1, 1};
const double pi = acos(-1);
const double eps = 1e-7;
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T> inline void print(T &a) {
  for (auto itr = a.begin(); itr != a.end(); itr++) {
    cout << *itr << " ";
  }
  cout << "\\n";
}
template <typename T1, typename T2> inline void print2(T1 a, T2 b) {
  cout << "debug: " << a << " " << b << "\\n";
}
template <typename T1, typename T2, typename T3>
inline void print3(T1 a, T2 b, T3 c) {
  cout << "debug: " << a << " " << b << " " << c << "\\n";
}
void mark() {
  cout << "#"
       << "\\n";
}
ll pcount(ll x) { return __builtin_popcountll(x); }
// const int mod = 1e9 + 7;
const int mod = 998244353;

int main() {
  ll n, q;
  cin >> n >> q;
  ll ans = (n - 2) * (n - 2);
  set<P> y, x;
  y.emplace(n, n);
  x.emplace(n, n);
  rep(i, q) {
    ll t, p;
    cin >> t >> p;
    if (t == 1) {
      auto itr = x.upper_bound(P(p, inf));
      ans -= (*itr).second - 2;
      y.emplace((*itr).second, p);
    } else {
      auto itr = y.upper_bound(P(p, inf));
      ans -= (*itr).second - 2;
      x.emplace((*itr).second, p);
    }
  }
  cout << ans << "\\n";
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll, ll> P;
typedef pair<int, int> pii;
typedef vector<P> vpl;
typedef tuple<ll, ll, ll> tapu;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1 << 30;
const ll linf = 1LL << 62;
const int MAX = 2020000;
ll dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};
ll dx[8] = {0, 0, 1, -1, 1, -1, -1, 1};
const double pi = acos(-1);
const double eps = 1e-7;
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}
template <typename T> inline void print(T &a) {
  for (auto itr = a.begin(); itr != a.end(); itr++) {
    cout << *itr << " ";
  }
  cout << "\\n";
}
template <typename T1, typename T2> inline void print2(T1 a, T2 b) {
  cout << "debug: " << a << " " << b << "\\n";
}
template <typename T1, typename T2, typename T3>
inline void print3(T1 a, T2 b, T3 c) {
  cout << "debug: " << a << " " << b << " " << c << "\\n";
}
void mark() {
  cout << "#"
       << "\\n";
}
ll pcount(ll x) { return __builtin_popcountll(x); }
// const int mod = 1e9 + 7;
const int mod = 998244353;

int main() {
  ll n, q;
  cin >> n >> q;
  ll ans = (n - 2) * (n - 2);
  set<P> y, x;
  y.emplace(n, n);
  x.emplace(n, n);
  while (q--) {
    int t, p;
    cin >> t >> p;
    if (t == 1) {
      auto itr = upper_bound(all(x), P(p, inf));
      ans -= (*itr).second - 2;
      y.emplace((*itr).second, p);
    } else {
      auto itr = upper_bound(all(y), P(p, inf));
      ans -= (*itr).second - 2;
      x.emplace((*itr).second, p);
    }
  }
  cout << ans << "\\n";
}