Processing math: 100%
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 xi and yj. A fertilizer sprinkler at position (z,w) will supply fertilizer to cells (i,j) satisfying iz and jw. 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(1Q,K3×105;Q,KN×M;1N,M109). - 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 109+7. ### Constraints - 20% of the tests have N,M100. - 20% of the tests have N,M4000. - 20% of the tests have N4000. - 20% of the tests have N3×105. - The remaining 20% of the tests have no additional constraints. ### Example Input: 3321122132 Output: 12
Topic
Prefix sum
Data structure
Rating 2100
Solution (0) Solution