Problem:
Score : 600 points

Problem Statement

There is a grid with N rows and N columns of squares. Let (i, j) be the square at the i-th row from the top and the j-th column from the left.

Each of the central (N-2) \times (N-2) squares in the grid has a black stone on it.
Each of the 2N - 1 squares on the bottom side and the right side has a white stone on it.

Q queries are given. We ask you to process them in order.
There are two kinds of queries. Their input format and description are as follows:

1 x: Place a white stone on (1, x). After that, for each black stone between (1, x) and the first white stone you hit if you go down from (1, x), replace it with a white stone.

2 x: Place a white stone on (x, 1). After that, for each black stone between (x, 1) and the first white stone you hit if you go right from (x, 1), replace it with a white stone.

How many black stones are there on the grid after processing all Q queries?

Constraints

3 \leq N \leq 2\times 10^5

0 \leq Q \leq \min(2N-4,2\times 10^5)

2 \leq x \leq N-1

Queries are pairwise distinct.

Input

Input is given from Standard Input in the following format:

N Q
Query_1
\vdots
Query_Q

Output

Print how many black stones there are on the grid after processing all Q queries.

Sample Input 1

5 5
1 3
2 3
1 4
2 2
1 2

Sample Output 1

1

After each query, the grid changes in the following way:

Sample Input 2

200000 0

Sample Output 2

39999200004

Sample Input 3

176527 15
1 81279
2 22308
2 133061
1 80744
2 44603
1 170938
2 139754
2 15220
1 172794
1 159290
2 156968
1 56426
2 77429
1 97459
2 71282

Sample Output 3

31159505795

Solution in C++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdio.h>
#include<cstring>
#include<math.h>
#include<map>
#include<bitset>
#include<iomanip>
#include<queue>
#include<functional>
#include<stack>
#include<tuple>
#include<cassert>
using namespace std;

#define rep(i, n) for (long long i = 0; i < (long long)(n); ++i)
using ll = long long;
using P = pair<long long, long long>;
using MATRIX = vector< vector<ll> >;
template <class T> inline bool chmin(T &x, const T &y) {if (x > y) {x = y; return 1;} return 0;}
template <class T> inline bool chmax(T &x, const T &y) {if (x < y) {x = y; return 1;} return 0;}

const long long INF = 100000000000;

/* RMQ：[0,n-1] について、区間ごとの最小値を管理する構造体
    update(i,x): i 番目の要素を x に更新。O(log(n))
    find(a,b): [a,b) での最小の要素を取得。O(log(n))
*/
template <typename T>
struct RMQ {
    //const T INF = numeric_limits<T>::max();
    long long n;         // 葉の数
    vector<T> dat, lazy; // 完全二分木の配列
    RMQ(long long n_) : n(), dat(n_ * 4, INF), lazy(n_*4, INF) { // 葉の数は 2^x の形
        long long x = 1;
        while (n_ > x) {
            x *= 2;
        }
        n = x;
    }
 
    void update(long long i, T x) {
        i += n - 1;
        dat[i] = x;
        while (i > 0) {
            i = (i - 1) / 2;  // parent
            dat[i] = min(dat[i * 2 + 1], dat[i * 2 + 2]);
        }
    }
 
    // the minimum element of [a,b)
    T query(long long a, long long b) { return query_sub(a, b, 0, 0, n); }
    T query_sub(long long a, long long b, long long k, long long l, long long r) {
        eval(k);
        if (r <= a || b <= l) {
            return INF;
        } else if (a <= l && r <= b) {
            return dat[k];
        } else {
            T vl = query_sub(a, b, k * 2 + 1, l, (l + r) / 2);
            T vr = query_sub(a, b, k * 2 + 2, (l + r) / 2, r);
            return min(vl, vr);
        }
    }

    void eval(long long k) {
        if (lazy[k] == INF) return;  // 更新するものが無ければ終了
        if (k < n - 1) {             // 葉でなければ子に伝搬
            lazy[k * 2 + 1] = lazy[k];
            lazy[k * 2 + 2] = lazy[k];
        }
        // 自身を更新(今の値より小さい時のみ更新する処理に変更)
        if(dat[k] > lazy[k]) {
          dat[k] = lazy[k];
        } 
        lazy[k] = INF;
    }
    void update(long long a, long long b, T x, long long k, long long l, long long r) {
        eval(k);
        if (a <= l && r <= b) {  // 完全に内側の時
            lazy[k] = x;
            eval(k);
        } else if (a < r && l < b) {                     // 一部区間が被る時
            update(a, b, x, k * 2 + 1, l, (l + r) / 2);  // 左の子
            update(a, b, x, k * 2 + 2, (l + r) / 2, r);  // 右の子
            dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
        }
    }
    void update(long long a, long long b, T x) { update(a, b, x, 0, 0, n); }
};

int main() {
  long long N, Q;
  cin >> N >> Q;
  RMQ<long long> tree1(N);
  RMQ<long long> tree2(N);
  for(int i = 1; i < N-1; i++) {
    if(i == 0) continue;
    tree1.update(i, N-1);
    tree2.update(i, N-1);
  }
  long long temp = 0;
  rep(i, Q) {
    long long t;
    cin >> t;
    if(t == 1) {
      long long x;
      cin >> x;
      x--;
      temp += tree1.query(x, x+1);
      tree2.update(1, tree1.query(x, x+1), x);
      tree1.update(x, 0);
    } else {
      long long x;
      cin >> x;
      x--;
      temp += tree2.query(x, x+1);
      tree1.update(1, tree2.query(x, x+1), x);
      tree2.update(x, 0);
    }
  }
  long long ans;
  ans = (N-2) * (N-2) + Q;
  ans -= temp;
  cout << ans << endl;
  return 0;
}