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)

#include <bits/stdc++.h>

using namespace std;

typedef long long i64;
typedef i64 int_t;
typedef vector<int_t> vi;
typedef vector<vi> vvi;

typedef pair<int_t, int_t> pi;

#define tr(c, i) for(auto i = (c).begin(); i != (c).end(); i++)
#define pb push_back
#define sz(a) i64((a).size())
#define all(c) (c).begin(), (c).end()
#define REP(s, e, i) for(i=(s); i < (e); ++i)

struct SegTree {
  ///
  using value_type = long long;
  using index_type = vector<value_type>::size_type;
  static const value_type merge(const value_type &v1, const value_type &v2) {
    return min(v1, v2);
  }
  static const value_type update_elem(const value_type &current_value, const value_type &updated_value) {
    return min(current_value, updated_value);
  }
  static const value_type update_lazy(const value_type &lazy_cur, const value_type &lazy_up) {
    return min(lazy_cur, lazy_up);
  }
  static const value_type apply_lazy(const value_type &value, const value_type &lazy, i64 nl, i64 nr) {
    return min(value, lazy);
  }
  const value_type default_value = (1LL << 31) - 1;
  
  ///

  vector<value_type> data, lazy;
  index_type n;
  
  SegTree(i64 n_) {
    i64 s = 1;
    while(s < n_) {
      s *= 2;
    }
    n = s;
    data.resize(s * 2 - 1, default_value);
    lazy.resize(s * 2 - 1, default_value);
  }

  value_type query(index_type l, index_type r, index_type nid, index_type nl, index_type nr) {
    lazy_eval(nid, nl, nr);
    if(nr <= l || r <= nl) {
      return default_value;
    }
    
    if(l <= nl && nr <= r) {
      return data[nid];
    }
    else {
      value_type vl = query(l, r, nid*2+1, nl, (nl+nr)/2);
      value_type vr = query(l, r, nid*2+2, (nl+nr)/2, nr);
      return merge(vl, vr);
    }
  }  
  value_type query(index_type l, index_type r) {
    return query(l, r, 0, 0, n);
  }

  void update(index_type i, value_type x) {
    i += n-1;
    data[i] = update_elem(data[i], x);
    while(i > 0) {
      i = (i - 1) / 2;
      data[i] = merge(data[i*2+1], data[i*2+2]);
    }
  }

  void lazy_eval(index_type nid, index_type nl, index_type nr) {
    if(lazy[nid] != default_value) {
      data[nid] = apply_lazy(data[nid], lazy[nid], nl, nr);
      if(nr - nl > 1) {
	lazy[nid*2+1] = update_lazy(lazy[nid*2+1], lazy[nid]);
	lazy[nid*2+2] = update_lazy(lazy[nid*2+2], lazy[nid]);
      }
      lazy[nid] = default_value;
      //cout << "lazy " << nid << " " << data[nid] << " " << lazy[nid] << " " <<
      // nl << " " << nr << endl;
    }
  }
  
  void range_update(index_type l, index_type r, value_type x, index_type nid, index_type nl, index_type nr) {    
    lazy_eval(nid, nl, nr);    
    if(nr <= l || r <= nl) {
      return;
    } 

    //cout << nid << " " << nl << " " << nr << endl;
    if(l <= nl && nr <= r) {      
      lazy[nid] = x;
      lazy_eval(nid, nl, nr);
    }
    else {
      range_update(l, r, x, nid*2+1, nl, (nl+nr)/2);
      range_update(l, r, x, nid*2+2, (nl+nr)/2, nr);
      data[nid] = merge(data[nid*2+1], data[nid*2+2]);
    }
  }
  
  void range_update(index_type l, index_type r, value_type x) {
    range_update(l, r, x, 0, 0, n);
  }
};


int main(int argc, char *argv[]) {

  i64 N, Q, i;
  cin >> N >> Q;
  i64 B = (N-2) * (N-2);

  N -= 2;
  SegTree tree1(N), tree2(N);
  tree1.range_update(0, N, N);
  tree2.range_update(0, N, N);
  
  REP(0, Q, i) {
    i64 q, x;
    cin >> q >> x;
    x -= 2;
    if(q == 1) {
      i64 val = tree1.query(x, x+1);
      B -= val;
      tree2.range_update(0, val, x);
    }
    else {
      i64 val = tree2.query(x, x+1);
      B -= val;
      tree1.range_update(0, val, x);
    }

    /*
    i64 j;
    cout << "tree1 ";
    REP(0, N, j) {
      cout << tree1.query(j, j+1) << " ";
    }
    cout << endl;
    cout << "tree2 ";
    REP(0, N, j) {
      cout << tree2.query(j, j+1) << " ";
    }
    cout << endl;
    cout << endl;
    */
    
  }

  cout << B << endl;

  return 0;
}