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)

fn main() {
	let mut s = String::new();
	use std::io::Read;
	std::io::stdin().read_to_string(&mut s).unwrap();
	let mut s = s.split_whitespace();
	let n: i64 = s.next().unwrap().parse().unwrap();
	let q: usize = s.next().unwrap().parse().unwrap();
	let mut ans = (n - 2) * (n - 2);
	let mut left = LazySegmentTree::new(
		n as usize,
		std::i64::MAX,
		|a, b| std::cmp::min(a, b),
		|a, b| std::cmp::min(a, b),
		|a, b| std::cmp::min(a, b),
	);
	left.operate(0, n as usize, n - 1);
	let mut top = LazySegmentTree::new(
		n as usize,
		std::i64::MAX,
		|a, b| std::cmp::min(a, b),
		|a, b| std::cmp::min(a, b),
		|a, b| std::cmp::min(a, b),
	);
	top.operate(0, n as usize, n - 1);
	for _ in 0..q {
		let ty = s.next().unwrap();
		if ty == "1" {
			let x: usize = s.next().unwrap().parse().unwrap();
			let x = x - 1;
			let t = top.get(x).unwrap();
			ans -= t - 1;
			left.operate(0, t as usize, x as i64);
		} else {
			let y: usize = s.next().unwrap().parse().unwrap();
			let y = y - 1;
			let l = left.get(y).unwrap();
			ans -= l - 1;
			top.operate(0, l as usize, y as i64);
		}
	}
	println!("{}", ans);
}

pub struct LazySegmentTree<M, P, F, G, H> {
	n: usize,
	data: Vec<M>,
	cache: Vec<Option<P>>,
	e: M,
	mfold: F,
	op: G,
	opfold: H,
}

impl<M, P, F, G, H> LazySegmentTree<M, P, F, G, H>
where
	M: Clone,
	P: Clone,
	F: Fn(M, M) -> M,
	G: Fn(M, P) -> M,
	H: Fn(P, P) -> P,
{
	pub fn new(n: usize, e: M, mfold: F, op: G, opfold: H) -> Self {
		Self {
			n,
			data: vec![e.clone(); 2 * n],
			cache: vec![None; 2 * n],
			e,
			mfold,
			op,
			opfold,
		}
	}

	pub fn construct(src: Vec<M>, e: M, mfold: F, op: G, opfold: H) -> Self {
		let mut data = vec![e.clone(); src.len()];
		data.extend(src);
		for i in (1..data.len() / 2).rev() {
			data[i] = mfold(data[i << 1].clone(), data[i << 1 | 1].clone());
		}
		let cache = vec![None; data.len()];
		Self {
			n: data.len() / 2,
			data,
			cache,
			e,
			mfold,
			op,
			opfold,
		}
	}

	fn from_ancestors(&mut self, i: usize) {
		let log2 = (i + 1).next_power_of_two().trailing_zeros() - 1;
		for j in (1..=log2).rev() {
			let i = i >> j;
			if let Some(p) = self.cache[i].take() {
				self.data[i] = (self.op)(self.data[i].clone(), p.clone());
				if let Some(child) = self.cache[i << 1].take() {
					self.cache[i << 1] = Some((self.opfold)(child, p.clone()));
				} else {
					self.cache[i << 1] = Some(p.clone());
				}
				if let Some(child) = self.cache[i << 1 | 1].take() {
					self.cache[i << 1 | 1] = Some((self.opfold)(child, p.clone()));
				} else {
					self.cache[i << 1 | 1] = Some(p.clone());
				}
			}
		}
	}

	fn to_ancestors(&mut self, mut i: usize) {
		while {
			i >>= 1;
			i > 0
		} {
			let s = self.operate_at(i << 1);
			let t = self.operate_at(i << 1 | 1);
			self.data[i] = (self.mfold)(s, t);
		}
	}

	fn operate_at(&self, i: usize) -> M {
		if let Some(p) = self.cache[i].as_ref() {
			(self.op)(self.data[i].clone(), p.clone())
		} else {
			self.data[i].clone()
		}
	}

	pub fn operate(&mut self, l: usize, r: usize, x: P) {
		let mut l = l + self.n;
		let mut r = r + self.n;
		let ltop = l >> l.trailing_zeros();
		let rtop = r >> r.trailing_zeros();
		self.from_ancestors(ltop);
		self.from_ancestors(rtop);
		while l < r {
			if l & 1 != 0 {
				if let Some(p) = self.cache[l].take() {
					self.cache[l] = Some((self.opfold)(p, x.clone()));
				} else {
					self.cache[l] = Some(x.clone());
				}
				l += 1;
			}
			if r & 1 != 0 {
				if let Some(p) = self.cache[r - 1].take() {
					self.cache[r - 1] = Some((self.opfold)(p, x.clone()));
				} else {
					self.cache[r - 1] = Some(x.clone());
				}
			}
			l >>= 1;
			r >>= 1;
		}
		self.to_ancestors(ltop);
		self.to_ancestors(rtop);
	}

	pub fn fold(&mut self, l: usize, r: usize) -> M {
		let mut l = l + self.n;
		let mut r = r + self.n;
		let ltop = l >> l.trailing_zeros();
		let rtop = r >> r.trailing_zeros();
		self.from_ancestors(ltop);
		self.from_ancestors(rtop);
		let mut s = self.e.clone();
		let mut t = self.e.clone();
		while l < r {
			if l & 1 != 0 {
				s = (self.mfold)(s, self.operate_at(l));
				l += 1;
			}
			if r & 1 != 0 {
				t = (self.mfold)(self.operate_at(r - 1), t);
			}
			l >>= 1;
			r >>= 1;
		}
		(self.mfold)(s, t)
	}

	pub fn get(&mut self, i: usize) -> Option<M> {
		if i < self.n {
			Some(self.fold(i, i + 1))
		} else {
			None
		}
	}

	pub fn set(&mut self, i: usize, x: M) {
		let i = i + self.n;
		self.from_ancestors(i);
		self.data[i] = x;
		self.cache[i] = None;
		self.to_ancestors(i);
	}

	pub fn len(&self) -> usize {
		self.n
	}
}