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: Accepted)

// Template
#include <bits/stdc++.h>
#define rep_override(x, y, z, name, ...) name
#define rep2(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep3(i, l, r) for (int i = (int)(l); i < (int)(r); ++i)
#define rep(...) rep_override(__VA_ARGS__, rep3, rep2)(__VA_ARGS__)
#define per(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
constexpr int inf = 1001001001;
constexpr ll INF = 3003003003003003003LL;
template <typename T> inline bool chmin(T &x, const T &y) {if (x > y) {x = y; return 1;} return 0;}
template <typename T> inline bool chmax(T &x, const T &y) {if (x < y) {x = y; return 1;} return 0;}
struct IOSET {IOSET() {cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(10);}} ioset;

// Lazy Segment Tree
template <typename Operator>
class LazySegmentTree {
    Operator OP;
    using NodeType = decltype(OP.NodeE);
    using FuncType = decltype(OP.FuncE);
    int length, height;
    vector<NodeType> node;
    vector<FuncType> lazy;
    vector<int> width;
    void eval(int n) {
        node[n] = OP.NodeFunc(node[n], lazy[n], width[n]);
        if (n < length) {
            lazy[(n << 1) | 0] = OP.MergeFunc(lazy[(n << 1) | 0], lazy[n]);
            lazy[(n << 1) | 1] = OP.MergeFunc(lazy[(n << 1) | 1], lazy[n]);
        }
        lazy[n] = OP.FuncE;
    }
public:
    LazySegmentTree(int n) {
        length = 1;
        height = 0;
        while (length < n) {
            length <<= 1;
            ++height;
        }
        node.resize(length << 1, OP.NodeE);
        lazy.resize(length << 1, OP.FuncE);
        width.resize(length << 1, 1);
        for (int i = length - 1; i > 0; --i) width[i] = width[i << 1] << 1;
    }
    LazySegmentTree(vector<NodeType> &vec) {
        length = 1;
        height = 0;
        while (length < vec.size()) {
            length <<= 1;
            ++height;
        }
        node.resize(length << 1, OP.NodeE);
        rep(i, vec.size()) node[i + length] = vec[i];
        for (int i = length - 1; i > 0; --i) node[i] = OP.MergeNode(node[(i << 1) | 0], node[(i << 1) | 1]);
        lazy.resize(length << 1, OP.FuncE);
        width.resize(length << 1, 1);
        for (int i = length - 1; i > 0; --i) width[i] = width[i << 1] << 1;
    }
    void update(int a, int b, FuncType x) {
        int l = a + length, r = b + length - 1;
        for (int i = height; i > 0; --i) {
            eval(l >> i);
            eval(r >> i);
        }
        ++r;
        while (r > l) {
            if (l & 1) {
                lazy[l] = OP.MergeFunc(lazy[l], x);
                eval(l);
                ++l;
            }
            if (r & 1) {
                --r;
                lazy[r] = OP.MergeFunc(lazy[r], x);
                eval(r);
            }
            l >>= 1; r >>= 1;
        }
        l = a + length; r = b + length - 1;
        while (l >>= 1) node[l] = OP.MergeNode(OP.NodeFunc(node[(l << 1) | 0], lazy[(l << 1) | 0], width[(l << 1) | 0]), OP.NodeFunc(node[(l << 1) | 1], lazy[(l << 1) | 1], width[(l << 1) | 1]));
        while (r >>= 1) node[r] = OP.MergeNode(OP.NodeFunc(node[(r << 1) | 0], lazy[(r << 1) | 0], width[(r << 1) | 0]), OP.NodeFunc(node[(r << 1) | 1], lazy[(r << 1) | 1], width[(r << 1) | 1]));
    }
    NodeType get(int a, int b) {
        int l = a + length, r = b + length - 1;
        for (int i = height; i >= 0; --i) {
            eval(l >> i);
            eval(r >> i);
        }
        ++r;
        NodeType vl = OP.NodeE, vr = OP.NodeE;
        while (l < r) {
            if (l & 1) {
                vl = OP.MergeNode(vl, OP.NodeFunc(node[l], lazy[l], width[l]));
                ++l;
            }
            if (r & 1) {
                --r;
                vr = OP.MergeNode(OP.NodeFunc(node[r], lazy[r], width[r]), vr);
            }
            l >>= 1; r >>= 1;
        }
        return OP.MergeNode(vl, vr);
    }
};
struct hoge {
    using NodeType = int;
    using FuncType = int;
    NodeType NodeE = inf;
    FuncType FuncE = inf;
    NodeType MergeNode(NodeType x, NodeType y) {
        return min(x, y);
    }
    FuncType MergeFunc(FuncType x, FuncType y) {
        if (y == inf) return x;
        return min(x, y);
    }
    NodeType NodeFunc(NodeType x, FuncType y, int len) {
        if (y == inf) return x;
        return min(x, y);
    }
};

int main() {
    int n, q;
    cin >> n >> q;
    vector<int> migi(n, n - 1), sita(n, n - 1);
    LazySegmentTree<hoge> st0(n), st1(n);
    st0.update(0, n, n - 1);
    st1.update(0, n, n - 1);
    ll ans = (ll)(n - 2) * (ll)(n - 2);
    rep(i, q) {
        int num, x;
        cin >> num >> x;
        --x;
        if (num == 1) {
            int y = st1.get(x, x + 1);
            st0.update(1, y + 1, x);
            ans -= (y - 1);
            //cout << y << "\n";
        } else {
            int y = st0.get(x, x + 1);
            st1.update(1, y + 1, x);
            ans -= (y - 1);
            //cout << y << "\n";
        }
        
        //rep(i, n) cout << st0.get(i, i + 1) << " \n"[i == n - 1];
        //rep(i, n) cout << st1.get(i, i + 1) << " \n"[i == n - 1];
        //cout << "\n";
    }
    
    cout<<ans << "\n";
}