## Royal Guard

View as PDF

Points: 10 (partial)
Time limit: 0.6s
Memory limit: 64M

Author:
Problem type
##### 2015 Mock CCC by Alex and Timothy

Royal Guards protect the cities we live in. However, their number are limited and they have many zones in the city to protect, so they must constantly travel.

We will focus our attention on the patrol route of one Royal Guard throughout the course of a single day. If we take an aerial view of the city as a 2-dimensional coordinate plane, our particular Royal Guard always travels in straight lines parallel to the x- or y-axes. It is a well-known fact that Royal Guards travel by teleporting, but it's lesser known that they cannot teleport through massive buildings. Massive buildings are big, but compared to the city as a whole, they become mere points on the plane. If a massive building is on one of our Royal Guard's straight paths (even at the beginning or end of the path), he is forced to stop in front of it and walk around (or through) it while contemplating his job security and future career paths. Since these are unpleasant things to think of for a Royal Guard, he will have to mentally prepare himself at the beginning of the day as he learns of his patrol route of that day.

Out of pity for the Royal Guard and his monotonous daily life, you decide to help him write a program to compute the number of times he will encounter a building and ponder about his existence so he won't have to do it himself every day.

#### Input Format

The first line of input will have an integer , the number of massive buildings.
The next lines of input will each have the x- and y-coordinates of a single massive building. You can assume that the coordinates are pairwise distinct.
The th line will have an integer , the number of turning points on the Royal Guard's route. The th path the Royal Guard walks is from the th turning point to the th turning point .
The next lines will each contain the x- and y-coordinates of a turning point. It is guaranteed for the two turning points and , exactly one of or will hold .
All numbers in the input will be between and , inclusive.

#### Output Format

The first and only line of output should be the total number of times that the Royal Guard encounters a building and falls deep into thought.
Note that the answer may be very large. In particular, it is not guaranteed to fit in a 32-bit integer.

#### Sample Input

4
2 2
2 3
3 2
3 3
5
1 2
4 2
3 2
3 5
3 1

#### Sample Output

7

#### Explanation

The massive buildings are arranged in a square shape. Note that paths can start or end on massive buildings. The following diagram depicts the Royal Guard's path, where each star represents a moment that he spends pondering his existence:

## Comments

• commented on Feb. 6, 2020, 6:02 p.m.

Anyone know why my code doesn't work?

• commented on July 27, 2017, 11:02 a.m.

Problem with my method or with my optimization?

I keep TLE'ing test cases after 9. Is there a faster method to complete this problem or is it just because of how I wrote my code?

• commented on July 27, 2017, 2:51 p.m.

Your code's time complexity is , which is too slow.