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 Rust (1.42.0) (judge verdict: Accepted)

use proconio::input;

const INF: usize = (1 << 31) - 1;

fn main() {
    input! {
        n: usize,
        q: usize,
    }
    let n = n - 2;
    let mut queries = vec![];
    for _ in 0..q {
        input! {
            t: usize,
            x: usize,
        }
        if t == 1 {
            queries.push(Query::Vertical(x - 2))
        } else {
            queries.push(Query::Horizontal(x - 2))
        }
    }

    let mut v_tree = SegmentTree::build(&vec![INF; n], std::cmp::min, INF);
    let mut h_tree = SegmentTree::build(&vec![INF; n], std::cmp::min, INF);
    v_tree.update(n - 1, n);
    h_tree.update(n - 1, n);

    let mut ans = n * n;
    for query in queries {
        match query {
            Query::Vertical(x) => {
                let a = h_tree.query(x, n);
                // dbg!("v", x, a);
                if a >= 1 && x < v_tree.query(a - 1, a) {
                    v_tree.update(a - 1, x);
                }
                // eprintln!("update v: ({}, {})", a, x);
                ans -= a;
            }
            Query::Horizontal(x) => {
                let a = v_tree.query(x, n);
                // dbg!("h", x, a);
                if a >= 1 && x < h_tree.query(a - 1, a) {
                    h_tree.update(a - 1, x);
                }
                // eprintln!("update h: ({}, {})", a, x);
                ans -= a;
            }
        }
        // dbg!(ans);
    }
    println!("{}", ans);
}

enum Query {
    Vertical(usize),
    Horizontal(usize),
}

struct SegmentTree<T, F>
where
    F: Fn(T, T) -> T,
    T: Clone + Copy,
{
    len: usize,
    data: Vec<T>,
    operator: F,
    unit: T,
}

impl<T, F> SegmentTree<T, F>
where
    F: Fn(T, T) -> T,
    T: Clone + Copy,
{
    fn build(v: &Vec<T>, operator: F, unit: T) -> SegmentTree<T, F> {
        let n = v.len();
        let mut len = 1;
        while len < n {
            len *= 2;
        }

        let mut segtree = SegmentTree {
            len,
            data: vec![unit; 2 * len - 1],
            operator,
            unit,
        };

        for i in 0..n {
            segtree.data[i + segtree.len - 1] = v[i];
        }
        for i in (0..(segtree.len - 1)).rev() {
            segtree.data[i] = (segtree.operator)(segtree.data[2 * i + 1], segtree.data[2 * i + 2]);
        }

        segtree
    }

    #[allow(unused)]
    fn update(&mut self, k: usize, a: T) {
        let data = &mut self.data;
        let mut k = k + self.len - 1;
        data[k] = a;
        while k > 0 {
            k = (k - 1) / 2;
            data[k] = (self.operator)(data[k * 2 + 1], data[k * 2 + 2]);
        }
    }

    #[allow(unused)]
    fn get(&self, k: usize) -> T {
        self.data[k + self.len - 1]
    }

    fn query(&self, a: usize, b: usize) -> T {
        self.execute_query(a, b, 0, 0, self.len)
    }

    fn execute_query(&self, a: usize, b: usize, k: usize, l: usize, r: usize) -> T {
        if r <= a || b <= l {
            return self.unit;
        }

        if a <= l && r <= b {
            self.data[k]
        } else {
            let vl = self.execute_query(a, b, k * 2 + 1, l, (l + r) / 2);
            let vr = self.execute_query(a, b, k * 2 + 2, (l + r) / 2, r);
            (self.operator)(vl, vr)
        }
    }
}