405333: GYM101908 C Pizza Cutter

Memory Limit:1024 MB Time Limit:0 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

C. Pizza Cuttertime limit per test0.5 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard output

Grandpa Giuseppe won a professional pizza cutter, the kind of type reel and, to celebrate, baked a rectangle pizza to his grandchildren! He always sliced his pizzas into pieces by making cuts over continuous lines, not necessarily rectilinear, of two types: some begin at the left edge of the pizza, follow continuously to the right and end up in the right edge; other start on lower edge, follow continuously up and end up on the top edge. But Grandpa Giuseppe always followed a property: two cuts of the same type would never intersect. Here is an example with 4 cuts, two of each type, in the left part of the figure, which divide the pizza in 9 pieces.

It turns out that Grandpa Giuseppe simply loves geometry, topology, combinatorics and stuff; so, he decided to show to his grandchildren who could get more pieces with the same number of cuts if cross cuts of the same type were allowed. The right part of the figure shows, for example, that if the two cuts of the type that go from left to right could intercept, the pizza would be divided into 10 pieces.

Grandpa Giuseppe ruled out the property, but will not make random cuts. In addition to being one of the two types, they will comply with the following restrictions:

  • Two cuts have at most one intersection point and, if they have, it is because the cuts cross each other at that point;
  • Three cuts do not intersect in a single point;
  • Two cuts do not intersect at the border of the pizza;
  • A cut does not intercept a pizza corner.

Given the start and end points of each cut, your program should compute the number of resulting pieces from the cuts of Grandfather Giuseppe.

Input

The first line of the input contains two integers $$$X$$$ and $$$Y$$$, ($$$1 \leq X, Y \leq 10^9$$$), representing the coordinates $$$(X, Y)$$$ of the upper-right corner of the pizza. The lower left corner has always coordinates $$$(0, 0)$$$. The second line contains two integers $$$H$$$ and $$$V$$$, ($$$1 \leq H, V \leq 10^5$$$), indicating, respectively, the number of cuts ranging from left to right and the number of cuts ranging from bottom to top. Each of the following lines $$$H$$$ contains two integers $$$Y_1$$$ and $$$Y_2$$$, a cut that intercepts the left side with y-coordinate $$$Y_1$$$ and the right side at y-coordinate $$$Y_2$$$. Each of the following $$$V$$$ lines contains two integers $$$X_1$$$ and $$$X_2$$$, a cut that intercept the bottom side at x-coordinate $$$X_1$$$ and the upper side at x-coordinate $$$X_2$$$.

ExamplesInput
3 4
3 2
1 2
2 1
3 3
1 1
2 2
Output
13
Input
5 5
3 3
2 1
3 2
1 3
3 4
4 3
2 2
Output
19
Input
10000 10000
1 2
321 3455
10 2347
543 8765
Output
6

加入题单

算法标签: