Planting flower - MarisaOJ: Marisa Online Judge

Planting flower

Time limit: 1000 ms
Memory limit: 256 MB
A rectangular garden is represented by a grid of size $N$ rows and $M$ columns. The rows are numbered from $1$ to $N$, and the columns are numbered from $1$ to $M$. The intersection of row $i$ and column $j$ is denoted as cell $(i,j)$. In the garden, there are $Q$ water sprinklers and $K$ fertilizer sprinklers (a cell can have both types of sprinklers). A water sprinkler placed at position $(x,y)$ will water cells $(i,j)$ satisfying $x \leq i$ and $y \leq j$. A fertilizer sprinkler at position $(z,w)$ will supply fertilizer to cells $(i,j)$ satisfying $i \leq z$ and $j \leq w$. A cell can have flowers if it has both fertilizer and water. The goal is to select a rectangular piece of land with sides parallel to the boundaries of the garden that satisfies the conditions for growing flowers. Count the number of ways to choose such a rectangular piece of land. ### Input - The first line contains four positive integers $N, M, Q, K (1 \leq Q, K \leq 3 \times 10^5; Q, K \leq N \times M; 1 \leq N, M \leq 10^9)$. - The next $Q$ lines each contain two integers $x, y$ representing the position of a water sprinkler. - The next $K$ lines each contain two integers $z, w$ representing the position of a fertilizer sprinkler. ### Output - Output a single integer, the number of rectangular pieces of land that satisfy the conditions, modulo $10^9+7$. ### Constraints - 20% of the tests have $N, M \leq 100$. - 20% of the tests have $N, M \leq 4000$. - 20% of the tests have $N \leq 4000$. - 20% of the tests have $N \leq 3 \times 10^5$. - The remaining 20% of the tests have no additional constraints. ### Example Input: ``` 3 3 2 1 1 2 2 1 3 2 ``` Output: ``` 12 ```
Topic
Prefix sum
Data structure
Rating 2100
Solution (0) Solution