Rectangular Pizza
InterviewTime limit1sMemory limit1024 MB
Given the cut positions on a W by H pizza, count the slices whose area is at most K.
- Level
Medium5 of 10
- Topics
- Sorting, Two pointers, Binary search
- Solved
- No attempts yet
Problem
Yongmo is in charge of preparing pizza for club members to share at the CTP event.
The pizza Yongmo ordered is a rectangle with width and height . In both the horizontal and vertical directions, there is at least one straight cut across the pizza. Each cut is at a natural number distance of 1 or more from the bottom edge or the left edge.

When the pizza has = 7 and = 5, with a horizontal cut at = 2 and vertical cuts at = 1 and = 5, the slices have the sizes shown in the figure above.
The CTP staff cannot give a slice to a member if its size is greater than , because one person would find it hard to eat. The staff counts the slices that can be given to members. Given the pizza information, find how many slices the CTP staff can give to members.
Input
The first line contains , , and separated by spaces. is the maximum size of a slice that a member can eat.
The second line contains , the number of horizontal cuts.
The third line contains the vertical positions of the horizontal cuts separated by spaces.
The fourth line contains , the number of vertical cuts.
The fifth line contains the horizontal positions of the vertical cuts separated by spaces.
Output
Print the number of pizza slices that the CTP staff can give to members.
Constraints
- ,
- , , and and are natural numbers.
- The sequences and are in ascending order.