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 Go (1.14.1) (judge verdict: Accepted)

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var sc, wr = bufio.NewScanner(os.Stdin), bufio.NewWriter(os.Stdout)

func scanString() string   { sc.Scan(); return sc.Text() }
func scanRunes() []rune    { return []rune(scanString()) }
func scanInt() int         { a, _ := strconv.Atoi(scanString()); return a }
func scanInt64() int64     { a, _ := strconv.ParseInt(scanString(), 10, 64); return a }
func scanFloat64() float64 { a, _ := strconv.ParseFloat(scanString(), 64); return a }

func scanInts(n int) []int {
	a := make([]int, n)
	for i := 0; i < n; i++ {
		a[i] = scanInt()
	}
	return a
}

func debug(a ...interface{}) {
	if os.Getenv("ONLINE_JUDGE") == "false" {
		fmt.Fprintln(os.Stderr, a...)
	}
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

//•*¨*•.¸¸♪main•*¨*•.¸¸♪(　-ω-)ノ　(　・ω・)
func main() {
	defer wr.Flush()
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 10000), 1001001)

	n, q := scanInt(), scanInt()
	sega := newLazySegmentTree(n, 0, 0, f, g, h)
	segb := newLazySegmentTree(n, 0, 0, f, g, h)

	for i := 0; i < q; i++ {
		t, x := scanInt(), scanInt()-1
		if t == 1 {
			a := sega.get(x)
			debug(t, a)
			sega.update(x, x+1, nodeL(n-2))
			segb.update(0, n-int(a), nodeL(n-1-x))
		} else {
			b := segb.get(x)
			segb.update(x, x+1, nodeL(n-2))
			sega.update(0, n-int(b), nodeL(n-1-x))
		}
	}

	ans := 0
	for i := 1; i < n-1; i++ {
		x := int(sega.get(i))
		ans += n - 2 - x
		segb.update(0, n-x, nodeL(n-1-i))
	}
	for i := 1; i < n-1; i++ {
		x := int(segb.get(i))
		ans += n - 2 - x
		// sega.update(0, n-x, nodeL(n-1-i))
	}
	fmt.Fprintln(wr, ans)
}

func f(a, b node) node {
	if a > b {
		return a
	}
	return b
}

func g(a node, b nodeL) node {
	if a > node(b) {
		return a
	}
	return node(b)
}

func h(a, b nodeL) nodeL {
	if a > b {
		return a
	}
	return b
}

type node int
type nodeL int

type op func(a, b node) node
type mp func(a node, b nodeL) node
type cp func(a, b nodeL) nodeL
type lazySegmentTree struct {
	d  []node
	lz []nodeL
	n  int
	sz int
	lg int
	e  node
	el nodeL
	f  op
	g  mp
	h  cp
}

func newLazySegmentTree(n int, e node, el nodeL, f op, g mp, h cp) *lazySegmentTree {
	sz, lg := 1, 0
	for sz < n {
		sz <<= 1
		lg++
	}
	d := make([]node, 2*sz)
	lz := make([]nodeL, sz)
	for i := 0; i < 2*sz; i++ {
		d[i] = e
	}
	for i := 0; i < sz; i++ {
		lz[i] = el
	}
	s := lazySegmentTree{d, lz, n, sz, lg, e, el, f, g, h}
	return &s
}

func (s *lazySegmentTree) set(i int, a node) {
	s.d[i+s.sz] = a
}

func (s *lazySegmentTree) build() {
	for i := s.sz - 1; i > 0; i-- {
		s.d[i] = s.f(s.d[2*i], s.d[2*i+1])
	}
}

func (s *lazySegmentTree) get(k int) node {
	k += s.sz
	for i := s.lg; i > 0; i-- {
		s.prop(k >> i)
	}
	return s.d[k]
}

func (s *lazySegmentTree) update(l, r int, x nodeL) {
	l += s.sz
	r += s.sz
	for i := s.lg; i > 0; i-- {
		if l>>i<<i != l {
			s.prop(l >> i)
		}
		if r>>i<<i != r {
			s.prop((r - 1) >> i)
		}
	}

	for ll, rr := l, r; ll < rr; ll, rr = ll>>1, rr>>1 {
		if ll&1 == 1 {
			s.apply(ll, x)
			ll++
		}
		if rr&1 == 1 {
			rr--
			s.apply(rr, x)
		}
	}

	for i := 1; i <= s.lg; i++ {
		if l>>i<<i != l {
			k := l >> i
			s.d[k] = s.f(s.d[2*k], s.d[2*k+1])
		}
		if r>>i<<i != r {
			k := (r - 1) >> i
			s.d[k] = s.f(s.d[2*k], s.d[2*k+1])
		}
	}
}

func (s *lazySegmentTree) query(l, r int) node {
	vl, vr := s.e, s.e
	l += s.sz
	r += s.sz

	for i := s.lg; i > 0; i-- {
		if l>>i<<i != l {
			s.prop(l >> i)
		}
		if r>>i<<i != r {
			s.prop(r >> i)
		}
	}

	for ; l < r; l, r = l>>1, r>>1 {
		if l&1 == 1 {
			vl = s.f(vl, s.d[l])
			l++
		}
		if r&1 == 1 {
			r--
			vr = s.f(s.d[r], vr)
		}
	}
	return s.f(vl, vr)
}

func (s *lazySegmentTree) maxRight(l int, f func(v node) bool) int {
	if l == s.n {
		return s.n
	}
	v := s.e
	l += s.sz
	for i := s.lg; i > 0; i-- {
		s.prop(l >> i)
	}

	for {
		for l&1 == 0 {
			l >>= 1
		}
		if !f(s.f(v, s.d[l])) {
			for l < s.sz {
				l <<= 1
				if f(s.f(v, s.d[l])) {
					v = s.f(v, s.d[l])
					l++
				}
			}
			return l - s.sz
		}
		v = s.f(v, s.d[l])
		l++
		if l&-l == l {
			break
		}
	}

	return s.n
}

func (s *lazySegmentTree) minLeft(r int, f func(v node) bool) int {
	if r == 0 {
		return 0
	}
	v := s.e
	r += s.sz
	for i := s.lg; i > 0; i-- {
		s.prop((r - 1) >> i)
	}

	for {
		r--
		for r > 1 && r&1 == 1 {
			r >>= 1
		}
		if !f(s.f(v, s.d[r])) {
			for r < s.sz {
				r = r<<1 + 1
				if f(s.f(v, s.d[r])) {
					v = s.f(v, s.d[r])
					r--
				}
			}
			return r + 1 - s.sz
		}
		v = s.f(v, s.d[r])
		if r&-r == r {
			break
		}
	}

	return 0
}

func (s *lazySegmentTree) prop(k int) {
	s.apply(2*k, s.lz[k])
	s.apply(2*k+1, s.lz[k])
	s.lz[k] = s.el
}

func (s *lazySegmentTree) apply(k int, x nodeL) {
	s.d[k] = s.g(s.d[k], x)
	if k < s.sz {
		s.lz[k] = s.h(x, s.lz[k])
	}
}