Memento Mori
Time limit2.5sMemory limit64 MB
Count submatrices with exactly four marked cells whose rows increase and whose columns follow the order given by permutation p, with no smaller qualifying submatrix inside.
- Level
Hard9 of 10
- Topics
- Matrix, Dynamic programming, Divide and conquer
- Solved
- No attempts yet
Problem
Professor Zhang has an matrix where every element is 0. Professor Zhang changes elements of the matrix into 1s.
Given a permutation of , Professor Zhang wants to count the submatrices that satisfy all of the following:
- The submatrix contains exactly 4 ones.
- Let the positions of the ones in the submatrix be . Then , and for all .
- The chosen submatrix contains no other submatrix that satisfies the two conditions above.
Input
The first line contains an integer , the number of test cases. Each test case starts with a line containing three integers , , and (), the size of the matrix and the number of ones. The next line contains four integers , the permutation. Each of the next lines contains two integers and (, ), the position of the -th one. No two ones share the same position.
There are at most 250 test cases, and the total input size is at most 250 KiB.
Output
For each test case, output a single integer: the number of submatrices that satisfy all the requirements.