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: Wrong Answer)

#![allow(unused_imports)]
#![allow(unused_macros)]
use itertools::Itertools;
use std::cmp::{max, min};
use std::collections::*;
use std::i64;
use std::io::{stdin, Read};
use std::usize;

trait ChMinMax {
    fn chmin(&mut self, other: Self);
    fn chmax(&mut self, other: Self);
}
impl<T> ChMinMax for T
where
    T: PartialOrd,
{
    fn chmin(&mut self, other: Self) {
        if *self > other {
            *self = other
        }
    }
    fn chmax(&mut self, other: Self) {
        if *self < other {
            *self = other
        }
    }
}

#[allow(unused_macros)]
macro_rules! parse {
    ($it: ident ) => {};
    ($it: ident, ) => {};
    ($it: ident, $var:ident : $t:tt $($r:tt)*) => {
        let $var = parse_val!($it, $t);
        parse!($it $($r)*);
    };
    ($it: ident, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = parse_val!($it, $t);
        parse!($it $($r)*);
    };
    ($it: ident, $var:ident $($r:tt)*) => {
        let $var = parse_val!($it, usize);
        parse!($it $($r)*);
    };
}

#[allow(unused_macros)]
macro_rules! parse_val {
    ($it: ident, [$t:tt; $len:expr]) => {
        (0..$len).map(|_| parse_val!($it, $t)).collect::<Vec<_>>();
    };
    ($it: ident, ($($t: tt),*)) => {
        ($(parse_val!($it, $t)),*)
    };
    ($it: ident, u1) => {
        $it.next().unwrap().parse::<usize>().unwrap() -1
    };
    ($it: ident, $t: ty) => {
        $it.next().unwrap().parse::<$t>().unwrap()
    };
}
use num::{Bounded, One, Zero};
use std::ops::{Add, Mul};
pub trait Monoid {
    fn unit() -> Self;
    fn add(lhs: &Self, rhs: &Self) -> Self;
}

#[derive(Copy, Clone, Debug)]
pub struct Sum<T>(T);

impl<T> Monoid for Sum<T>
where
    T: Zero + Add<Output = T> + Copy,
{
    fn unit() -> Self {
        Self(T::zero())
    }
    fn add(lhs: &Self, rhs: &Self) -> Self {
        Self(lhs.0 + rhs.0)
    }
}

impl<T> From<T> for Sum<T> {
    fn from(x: T) -> Self {
        Self(x)
    }
}

#[derive(Clone, Copy, Debug)]
pub struct Product<T>(pub T);

impl<T: Copy + One + Mul<Output = T>> Monoid for Product<T> {
    fn unit() -> Self {
        Self(T::one())
    }

    fn add(l: &Self, r: &Self) -> Self {
        Self(l.0 * r.0)
    }
}

impl<T> From<T> for Product<T> {
    fn from(v: T) -> Self {
        Product(v)
    }
}

#[derive(Copy, Clone, Debug)]
pub struct Max<T>(pub T);

impl<T: Copy + Ord + Bounded> Monoid for Max<T> {
    fn unit() -> Self {
        Self(<T as Bounded>::min_value())
    }

    fn add(l: &Self, r: &Self) -> Self {
        Self(l.0.max(r.0))
    }
}

#[derive(Copy, Clone, Debug)]
pub struct Min<T>(pub T);

impl<T: Copy + Ord + Bounded> Monoid for Min<T> {
    fn unit() -> Self {
        Self(<T as Bounded>::max_value())
    }

    fn add(l: &Self, r: &Self) -> Self {
        Self(l.0.min(r.0))
    }
}

pub struct SegTree<T: Monoid + Clone> {
    dat: Vec<T>,
    n: usize,
}

impl<T: Monoid + Copy> SegTree<T> {
    pub fn new(n: usize) -> Self {
        let dat = vec![T::unit(); n << 1];
        SegTree { dat: dat, n: n }
    }
    pub fn update(&mut self, k: usize, v: T) {
        let mut k = k + self.n;
        self.dat[k] = v;
        while {
            k >>= 1;
            k > 0
        } {
            self.dat[k] = T::add(&self.dat[k << 1], &self.dat[k << 1 | 1]);
        }
    }
    pub fn get(&self, a: usize, b: usize) -> T {
        let mut a = a + self.n;
        let mut b = b + self.n;
        let mut va = T::unit();
        let mut vb = T::unit();
        while a < b {
            if a & 1 != 0 {
                va = T::add(&va, &self.dat[a]);
                a += 1;
            }
            if b & 1 != 0 {
                vb = T::add(&self.dat[b - 1], &vb);
            }
            a >>= 1;
            b >>= 1;
        }
        T::add(&va, &vb)
    }
}

fn solve(s: &str) {
    let mut it = s.split_whitespace();
    parse!(it, n: usize, q: usize);
    let mut ret = (n - 2) * (n - 2);
    let mut d = n - 1;
    let mut r = n - 1;
    let mut xs = vec![n - 1; n];
    let mut ys = vec![n - 1; n];
    for _ in 0..q {
        parse!(it, t: usize, x: u1);
        let (xs, ys, r, d) = if t == 1 {
            (&mut xs, &mut ys, &mut r, &mut d)
        } else {
            (&mut ys, &mut xs, &mut d, &mut r)
        };
        if x < *r {
            *r = x;
            ret -= *d - 1;
        } else {
            ret -= xs[x] - 1;
        }
    }
    println!("{}", ret);
}

fn main() {
    let mut s = String::new();
    stdin().read_to_string(&mut s).unwrap();
    solve(&s);
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_input() {
        let s = "
        ";
        solve(s);
    }
}