← Home
```go
package main

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

type DSU struct {
	parent map[int]int
}

func (d *DSU) find(x int) int {
	if x == 0 {
		return 0
	}
	if d.parent[x] != x {
		d.parent[x] = d.find(d.parent[x])
	}
	return d.parent[x]
}

func (d *DSU) union(x, y int) {
	px, py := d.find(x), d.find(y)
	if px != py {
		d.parent[px] = py
	}
}

func main() {
	in := bufio.NewReader(os.Stdin)
	out := bufio.NewWriter(os.Stdout)
	defer out.Flush()

	var n, q int
	fmt.Fscan(in, &n, &q)

	rowDSU := make(map[int]*DSU)
	colDSU := make(map[int]*DSU)

	for i := 0; i < q; i++ {
		var x, y int
		var dir byte
		fmt.Fscan(in, &x, &y, &dir)

		if dir == 'L' {
			x0 := n + 1 - y
			if _, ok := rowDSU[y]; !ok {
				rowDSU[y] = &DSU{parent: make(map[int]int)}
			}
			if _, ok := rowDSU[y].parent[x0]; !ok {
				rowDSU[y].parent[x0] = x0
			}

			curX := rowDSU[y].find(x0)
			count := 0

			for curX > 0 {
				if d, ok := colDSU[curX]; ok {
					if d.find(y) != y {
						break
					}
				}

				count++

				if _, ok := rowDSU[y].parent[curX-1]; !ok {
					rowDSU[y].parent[curX-1] = curX - 1
				}
				rowDSU[y].union(curX, curX-1)

				if _, ok := colDSU[curX]; !ok {
					colDSU[curX] = &DSU{parent: make(map[int]int)}
				}
				if _, ok := colDSU[curX].parent[y]; !ok {
					colDSU[curX].parent[y] = y
				}
				if _, ok := colDSU[curX].parent[y-1]; !ok {
					colDSU[curX].parent[y-1] = y - 1
				}
				colDSU[curX].union(y, y-1)

				curX = rowDSU[y].find(curX)
			}
			fmt.Fprintln(out, count)
		} else {
			y0 := n + 1 - x
			if _, ok := colDSU[x]; !ok {
				colDSU[x] = &DSU{parent: make(map[int]int)}
			}
			if _, ok := colDSU[x].parent[y0]; !ok {
				colDSU[x].parent[y0] = y0
			}

			curY := colDSU[x].find(y0)
			count := 0

			for curY > 0 {
				if d, ok := rowDSU[curY]; ok {
					if d.find(x) != x {
						break
					}
				}

				count++

				if _, ok := colDSU[x].parent[curY-1]; !ok {
					colDSU[x].parent[curY-1] = curY - 1
				}
				colDSU[x].union(curY, curY-1)

				if _, ok := rowDSU[curY]; !ok {
					rowDSU[curY] = &DSU{parent: make(map[int]int)}
				}
				if _, ok := rowDSU[curY].parent[x]; !ok {
					rowDSU[curY].parent[x] = x
				}
				if _, ok := rowDSU[curY].parent[x-1]; !ok {
					rowDSU[curY].parent[x-1] = x - 1
				}
				rowDSU[curY].union(x, x-1)

				curY = colDSU[x].find(curY)
			}
			fmt.Fprintln(out, count)
		}
	}
}
```