#P9885. Diagonal Fancy
Diagonal Fancy
Diagonal Fancy
Problem Description
Given a matrix with rows and columns, your objective is to compute the total number of continuous sub-square matrices that are diagonal fancy . A square matrix is designated as diagonal fancy if it satisfies the subsequent criteria: For any indices , , , and , if , then . For any indices , , , and , if , then . Here, signifies the element located at the -th row and -th column of matrix . A continuous sub-square matrix from matrix with rows and columns is defined as the matrix derived from by selecting continuous rows and continuous columns.
Input
Ensure to use cin/cout and disable synchronization with stdio to avoid unexpected TLE verdict. The input consists of multiple test cases. The first line of the input contains an integer (), which represents the number of test cases. The first line of each test case contains two integers and (), representing the number of rows and columns in the matrix . Each of the next lines contains space-separated integers (), representing the elements of the matrix for that particular test case. It is guaranteed that over all test cases.
Output
For each test case, output a single integer in a single line, which represents the count of continuous sub-square matrices in matrix that are diagonal fancy.
Sample Input
3
2 2
1 2
3 1
2 2
1 2
2 1
3 3
1 2 3
4 1 2
5 4 1
Sample Output
5
4
14
Hint
In the first test case, there are diagonal fancy subsquares in total. They are listed in bold below.
$$\begin{align} &\textbf{1}\text{ }2&\ \ \ \ &1\text{ }\textbf{2}&\ \ \ \ &1\text{ }2&\ \ \ \ &1\text{ }2&\ \ \ \ &\textbf{1}\text{ }\textbf{2}&\notag\newline &3\text{ }1&\ \ \ \ &3\text{ }1&\ \ \ \ &\textbf{3}\text{ }1&\ \ \ \ &3\text{ }\textbf{1}&\ \ \ \ &\textbf{3}\text{ }\textbf{1}&\notag \end{align} $$Source
2023“钉耙编程”中国大学生算法设计超级联赛(8)