Graph on a Table
All submissions for this problem are available.
Read problems statements in Mandarin chinese, Russian and Vietnamese as well.You are given a table with $N$ rows and $M$ columns. Let's denote the cell in row $r$ and column $c$ by $(r, c)$. You are also given $Q$ rectangles in this table. Consider a directed acyclic graph such that each vertex corresponds to a cell of the table (so there are $N \cdot M$ vertices) and there is an edge from cell $(r_1, c_1)$ to cell $(r_2, c_2)$ if and only if: - $r_1 < r_2, c_1 < c_2$ - there is a rectangle containing cells $(r_1, c_1)$ and $(r_2, c_2)$ Let's define the length of a path as the number of vertices it contains. Find the length of the longest path and the number of different paths with maximum length in this graph. Two paths are different if their lengths are different or there is a cell which is visited in one path, but not in the other. Since the number of paths can be large, compute it modulo $10^9 + 7$. ### Input - The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. - The first line of each test case contains three space-separated integers $N$, $M$ and $Q$. - Each of the following $Q$ lines contains four space-separated integers $r_1$, $c_1$, $r_2$ and $c_2$ describing a rectangle which contains all cells $(r, c)$ such that $r_1 \le r \le r_2$ and $c_1 \le c \le c_2$. ### Output For each test case, print a single line containing two space-separated integers — the length of the longest path in the graph and the number of paths with this length modulo $10^9 + 7$. ### Constraints - $1 \le T \le 10^5$ - $1 \le N, M \le 2,000$ - $1 \le Q \le 5 \cdot 10^5$ - $1 \le r_1 \le r_2 \le N$ - $1 \le c_1 \le c_2 \le M$ - the sum of $N \cdot M$ for all test cases does not exceed $10^7$ - the sum of $Q$ for all test cases does not exceed $5 \cdot 10^5$ ### Subtasks **Subtask #1 (5 points):** - $1 \le T \le 100$ - $1 \le N, M, Q \le 10$ **Subtask #2 (10 points):** - $1 \le T \le 100$ - $1 \le N, M, Q \le 100$ **Subtask #3 (20 points):** - the sum of $Q$ does not exceed $2 \cdot 10^5$ - the sum of $N \cdot M$ does not exceed $4 \cdot 10^5$ **Subtask #4 (65 points):** original constraints ### Example Input ``` 2 3 4 2 1 1 2 2 2 2 3 4 3 3 2 1 1 3 3 1 1 3 3 ``` ### Example Output ``` 3 2 3 1 ``` ### Explanation **Example case 1:** There are four edges in this graph: $(1, 1) \rightarrow (2, 2)$ due to the first rectangle and $(2, 2) \rightarrow (3, 3)$, $(2, 2) \rightarrow (3, 4)$, $(2, 3) \rightarrow (3, 4)$ due to the second rectangle. There are no paths with lengths greater than $3$ and two paths with length $3$: $(1, 1) \rightarrow (2, 2) \rightarrow (3, 3)$ and $(1, 1) \rightarrow (2, 2) \rightarrow (3, 4)$.
|Tags||dsu, dynamic-programming, hard, isaf27, isaf27, likecs, ltime61|
|Time Limit:||4 sec|
|Source Limit:||50000 Bytes|
|Languages:||C, CPP14, JAVA, PYTH, PYTH 3.6, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, rust, SCALA, swift, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, kotlin, PERL6, TEXT, SCM chicken, CLOJ, COB, FS|
Fetching successful submissions
If you are still having problems, see a sample solution here.