Buggy python submission (verdict: Time Limit Exceeded):
import sys


N, Q = map(int, input().split())
query = [tuple(map(int, sys.stdin.readline().split())) for _ in range(Q)]

# H = [N - 2] * N
# W = [N - 2] * N
# H[0] = H[-1] = W[0] = W[-1] = 0

flagH = [N - 1] * N
flagW = [N - 1] * N

cnt = 0
for t, x in query:
    if t == 1:
        # for i in range(1, flagW[x - 1]):
        #     H[i] -= 1
        cnt += max(0, flagW[x - 1] - 1)
        # #不要 W[x - 1] -= flagW[x - 1] - 1
        
        for i in range(1, flagW[x - 1]):
            flagH[i] = min(flagH[i], x - 1)
        flagW[x - 1] = 0
    else:
        # for i in range(1, flagH[x - 1]):
        #     W[i] -= 1
        cnt += max(0, flagH[x - 1] - 1)
        # #不要　H[x - 1] -= flagH[x - 1] - 1
        
        for i in range(1, flagH[x - 1]):
            flagW[i] = min(flagW[i], x - 1)
        flagH[x - 1] = 0

print(pow(N - 2, 2) - cnt)


Fixed submission:
import sys


class SegmentTree():
    """
    update, get を提供するSegmentTree

    Attributes
    ----------
    __n : int
        葉の数。2 ^ i - 1
    __dot :
        Segment function
    __e: int
        単位元
    __node: list
        Segment Tree
    """
    def __init__(self, A, dot, e):
        """
        Parameters
        ----------
        A : list
            対象の配列
        dot :
            Segment function
        e : int
            単位元
        """
        n = 2 ** (len(A) - 1).bit_length()
        self.__n = n
        self.__dot = dot
        self.__e = e
        self.__node = [e] * (2 * n)
        for i in range(len(A)):
            self.__node[i + n] = A[i]
        for i in range(n - 1, 0, -1):
            self.__node[i] = self.__dot(self.__node[2 * i], self.__node[2 * i + 1])
    
    def update(self, i, c):
        i += self.__n
        node = self.__node
        node[i] = self.__dot(node[i], c)
        while i > 1:
            i //= 2
            node[i] = self.__dot(node[2 * i], node[2 * i + 1])

    def get(self, l, r):
        vl, vr = self.__e, self.__e
        l += self.__n
        r += self.__n
        while (l < r):
            if l & 1:
                vl = self.__dot(vl, self.__node[l])
                l += 1
            l //= 2
            if r & 1:
                r -= 1
                vr = self.__dot(vr, self.__node[r])
            r //= 2
        return self.__dot(vl, vr)


N, Q = map(int, input().split())
query = [tuple(map(int, sys.stdin.readline().split())) for _ in range(Q)]

# H = [N - 2] * N
# W = [N - 2] * N
# H[0] = H[-1] = W[0] = W[-1] = 0

flagH = [N - 1] * N
flagW = [N - 1] * N

stH = SegmentTree([N - 1] * N, min, N - 1)
stW = SegmentTree([N - 1] * N, min, N - 1)

cnt = 0
for t, x in query:
    if t == 1:
        # for i in range(1, flagW[x - 1]):
        #     H[i] -= 1
        cnt += max(0, min(stW.get(x - 1, N), flagW[x - 1]) - 1)
        # #不要 W[x - 1] -= flagW[x - 1] - 1
        
        # for i in range(1, flagW[x - 1]):
        #     flagH[i] = min(flagH[i], x - 1)
        flagW[x - 1] = 0
        stH.update(stW.get(x - 1, N), x - 1)

    else:
        # for i in range(1, flagH[x - 1]):
        #     W[i] -= 1
        cnt += max(0, min(stH.get(x - 1, N), flagH[x - 1]) - 1)
        # #不要　H[x - 1] -= flagH[x - 1] - 1
        
        # for i in range(1, flagH[x - 1]):
        #     flagW[i] = min(flagW[i], x - 1)
        flagH[x - 1] = 0
        stW.update(stH.get(x - 1, N), x - 1)

print(pow(N - 2, 2) - cnt)

Buggy python submission (verdict: Time Limit Exceeded):
import sys


N, Q = map(int, input().split())
query = [tuple(map(int, sys.stdin.readline().split())) for _ in range(Q)]

H = [N - 2] * N
W = [N - 2] * N
H[0] = H[-1] = W[0] = W[-1] = 0

flagH = [N - 1] * N
flagW = [N - 1] * N

for t, x in query:
    if t == 1:
        for i in range(1, flagW[x - 1]):
            H[i] -= 1
        W[x - 1] -= flagW[x - 1] - 1
        
        for i in range(1, flagW[x - 1]):
            flagH[i] = min(flagH[i], x - 1)
        flagW[x - 1] = 0
    else:
        for i in range(1, flagH[x - 1]):
            W[i] -= 1
        H[x - 1] -= flagH[x - 1] - 1
        
        for i in range(1, flagH[x - 1]):
            flagW[i] = min(flagW[i], x - 1)
        flagH[x - 1] = 0

print(sum(H))


Fixed submission:
import sys


class SegmentTree():
    """
    update, get を提供するSegmentTree

    Attributes
    ----------
    __n : int
        葉の数。2 ^ i - 1
    __dot :
        Segment function
    __e: int
        単位元
    __node: list
        Segment Tree
    """
    def __init__(self, A, dot, e):
        """
        Parameters
        ----------
        A : list
            対象の配列
        dot :
            Segment function
        e : int
            単位元
        """
        n = 2 ** (len(A) - 1).bit_length()
        self.__n = n
        self.__dot = dot
        self.__e = e
        self.__node = [e] * (2 * n)
        for i in range(len(A)):
            self.__node[i + n] = A[i]
        for i in range(n - 1, 0, -1):
            self.__node[i] = self.__dot(self.__node[2 * i], self.__node[2 * i + 1])
    
    def update(self, i, c):
        i += self.__n
        node = self.__node
        node[i] = self.__dot(node[i], c)
        while i > 1:
            i //= 2
            node[i] = self.__dot(node[2 * i], node[2 * i + 1])

    def get(self, l, r):
        vl, vr = self.__e, self.__e
        l += self.__n
        r += self.__n
        while (l < r):
            if l & 1:
                vl = self.__dot(vl, self.__node[l])
                l += 1
            l //= 2
            if r & 1:
                r -= 1
                vr = self.__dot(vr, self.__node[r])
            r //= 2
        return self.__dot(vl, vr)


N, Q = map(int, input().split())
query = [tuple(map(int, sys.stdin.readline().split())) for _ in range(Q)]

# H = [N - 2] * N
# W = [N - 2] * N
# H[0] = H[-1] = W[0] = W[-1] = 0

flagH = [N - 1] * N
flagW = [N - 1] * N

stH = SegmentTree([N - 1] * N, min, N - 1)
stW = SegmentTree([N - 1] * N, min, N - 1)

cnt = 0
for t, x in query:
    if t == 1:
        # for i in range(1, flagW[x - 1]):
        #     H[i] -= 1
        cnt += max(0, min(stW.get(x - 1, N), flagW[x - 1]) - 1)
        # #不要 W[x - 1] -= flagW[x - 1] - 1
        
        # for i in range(1, flagW[x - 1]):
        #     flagH[i] = min(flagH[i], x - 1)
        flagW[x - 1] = 0
        stH.update(stW.get(x - 1, N), x - 1)

    else:
        # for i in range(1, flagH[x - 1]):
        #     W[i] -= 1
        cnt += max(0, min(stH.get(x - 1, N), flagH[x - 1]) - 1)
        # #不要　H[x - 1] -= flagH[x - 1] - 1
        
        # for i in range(1, flagH[x - 1]):
        #     flagW[i] = min(flagW[i], x - 1)
        flagH[x - 1] = 0
        stW.update(stH.get(x - 1, N), x - 1)

print(pow(N - 2, 2) - cnt)