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)

#![allow(unused_parens)]
#![allow(unused_imports)]
#![allow(non_upper_case_globals)]
#![allow(non_snake_case)]
#![allow(unused_mut)]
#![allow(unused_variables)]
#![allow(dead_code)]

use itertools::Itertools;
use proconio::input;
use proconio::marker::{Chars, Usize1};

#[allow(unused_macros)]
#[cfg(debug_assertions)]
macro_rules! mydbg {
    //($arg:expr) => (dbg!($arg))
    //($arg:expr) => (println!("{:?}",$arg));
      ($($a:expr),*) => {
          eprintln!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
      }
}
#[cfg(not(debug_assertions))]
macro_rules! mydbg {
    ($($arg:expr),*) => {};
}

macro_rules! echo {
    ($($a:expr),*) => {
          $(println!("{}",$a))*
      }
}

use std::cmp::*;
use std::collections::*;
use std::ops::{Add, Div, Mul, Sub};

#[allow(dead_code)]
static INF_I64: i64 = 92233720368547758;
#[allow(dead_code)]
static INF_I32: i32 = 21474836;
#[allow(dead_code)]
static INF_USIZE: usize = 18446744073709551;
#[allow(dead_code)]
static M_O_D: usize = 1000000007;
#[allow(dead_code)]
static PAI: f64 = 3.1415926535897932;

trait IteratorExt: Iterator {
    fn toVec(self) -> Vec<Self::Item>;
}

impl<T: Iterator> IteratorExt for T {
    fn toVec(self) -> Vec<Self::Item> {
        self.collect()
    }
}

trait CharExt {
    fn toNum(&self) -> usize;
    fn toAlphabetIndex(&self) -> usize;
    fn toNumIndex(&self) -> usize;
}
impl CharExt for char {
    fn toNum(&self) -> usize {
        return *self as usize;
    }
    fn toAlphabetIndex(&self) -> usize {
        return self.toNum() - 'a' as usize;
    }
    fn toNumIndex(&self) -> usize {
        return self.toNum() - '0' as usize;
    }
}

trait VectorExt {
    fn joinToString(&self, s: &str) -> String;
}
impl<T: ToString> VectorExt for Vec<T> {
    fn joinToString(&self, s: &str) -> String {
        return self
            .iter()
            .map(|x| x.to_string())
            .collect::<Vec<_>>()
            .join(s);
    }
}

trait StringExt {
    fn get_reverse(&self) -> String;
}
impl StringExt for String {
    fn get_reverse(&self) -> String {
        self.chars().rev().collect::<String>()
    }
}

trait UsizeExt {
    fn pow(&self, n: usize) -> usize;
}
impl UsizeExt for usize {
    fn pow(&self, n: usize) -> usize {
        return ((*self as u64).pow(n as u32)) as usize;
    }
}

pub trait OpMonoid {
    type T: Clone + PartialEq;
    fn id() -> Self::T;
    fn op(a: &Self::T, b: &Self::T) -> Self::T;
    fn op2(a: &Self::T, b: &Self::T) -> Self::T;
    fn op3(a: &Self::T, b: &i64) -> Self::T;
}
pub struct LazySegTree<M: OpMonoid> {
    n: usize,
    data: Vec<M::T>,
    lazy: Vec<M::T>,
}
impl<M: OpMonoid> LazySegTree<M> {
    pub fn new(n_: usize) -> LazySegTree<M> {
        let mut n = 1 as usize;
        while (n < n_) {
            n *= 2;
        }
        LazySegTree {
            n: n,
            data: vec![M::id().clone(); (2 * n)],
            lazy: vec![M::id().clone(); (2 * n)],
        }
    }
    #[macro_use]
    pub fn update(&mut self, index: usize, v: M::T) {
        let mut k = index + self.n - 1;
        self.data[k] = v;
        while (k > 0) {
            k = (k - 1) / 2;
            self.data[k] = M::op(&self.data[2 * k + 1], &self.data[2 * k + 2]);
        }
    }
    pub fn eval(&mut self, k: usize, l: i32, r: i32) {
        if self.lazy[k] != M::id().clone() {
            if (r - l) > 1 {
                self.lazy[2 * k + 1] = M::op2(&self.lazy[2 * k + 1], &self.lazy[k]);
                self.lazy[2 * k + 2] = M::op2(&self.lazy[2 * k + 2], &self.lazy[k]);
            }
            self.data[k] = M::op2(&self.data[k], &M::op3(&self.lazy[k], &((r - l) as i64)));
            self.lazy[k] = M::id().clone();
        }
    }
    pub fn update_range_sub(&mut self, a: usize, b: usize, v: M::T, k: usize, l: i32, r: i32) {
        let mut r = r;
        if r < 0 {
            r = self.n as i32;
        }
        self.eval(k, l, r);
        if b as i32 <= l || r <= a as i32 {
            return;
        }
        if a as i32 <= l && r <= b as i32 {
            self.lazy[k] = v.clone();
            self.eval(k, l, r);
        } else {
            self.update_range_sub(a, b, v.clone(), 2 * k + 1, l, (l + r) / 2);
            self.update_range_sub(a, b, v.clone(), 2 * k + 2, (l + r) / 2, r);
            self.data[k] = M::op(&self.data[2 * k + 1], &self.data[2 * k + 2]);
        }
    }
    pub fn update_range(&mut self, l: usize, r: usize, v: M::T) {
        self.update_range_sub(l, r, v, 0, 0, -1);
    }
    pub fn query_sub(&mut self, a: usize, b: usize, k: usize, l: i32, r: i32) -> M::T {
        let mut r = r;
        if r < 0 {
            r = self.n as i32
        };
        if b as i32 <= l || r <= a as i32 {
            return M::id();
        }
        self.eval(k, l, r);
        if a as i32 <= l && r <= b as i32 {
            return self.data[k].clone();
        }
        let vl = self.query_sub(a, b, 2 * k + 1, l, (l + r) / 2);
        let vr = self.query_sub(a, b, 2 * k + 2, (l + r) / 2, r);
        return M::op(&vl, &vr);
    }
    pub fn query(&mut self, l: usize, r: usize) -> M::T {
        self.query_sub(l, r, 0, 0, -1)
    }
}
struct MIN;
impl OpMonoid for MIN {
    type T = u64;
    fn id() -> Self::T {
        u64::max_value()
    }
    fn op(a: &Self::T, b: &Self::T) -> Self::T {
        min(*a, *b)
    }
    fn op2(a: &Self::T, b: &Self::T) -> Self::T {
        min(*a, *b)
    }
    fn op3(a: &Self::T, b: &i64) -> Self::T {
        *a
    }
}

fn main() {
    input! {
       N: usize,
       Q:usize,
    }

    let mut ans = 0;
    let mut yoko: LazySegTree<MIN> = LazySegTree::new(N);
    let mut tate: LazySegTree<MIN> = LazySegTree::new(N);
    yoko.update_range(0, N, N as u64 - 1);
    tate.update_range(0, N, N as u64 - 1);
    for _ in 0..Q {
        input! {
            t:usize,
            p:usize,
        }
        if t == 1 {
            let a = yoko.query(p - 1, p);
            mydbg!(a);
            ans += a;
            tate.update_range(0, a as usize, p as u64 - 1);
        } else {
            let a = tate.query(p - 1, p);
            mydbg!(a);
            ans += a;
            yoko.update_range(0, a as usize, p as u64 - 1);
        }
    }

    let a = N * N;
    let b = N + N - 1;
    let c = N * 2 - 2;
    let ans = 1 + Q + a - b - ans as usize - c;
    echo!(ans);
}