Benewski's Patterns

Time limit: 0.6s Memory limit: 256MB Input: Output:

After playing petanque with his friends and beating them all the time, Benewski has decided to meditate for a little and quickly became interested in architecture and, most importantly, in shapes.

More exactly, he started drawing shapes made of straight line segments and decided to use them for his drawings.

Over time, his drawings became more and more complex, and he is now wondering, given one of his drawings, how many times a specific pattern appears in it.

A pattern is given by the coordinates of its segments that represent it. For a pattern to appear in a drawing, one should find the exact segments of the pattern up to translation in it. More exactly, a pattern occurs in the drawing if there exist numbers (dx,dydx, dy) such that, when shifting the pattern up by dxdx units on the XX axis and up by dydy units on the YY axis, one finds a subset of the segments of the drawing.

Two occurences of a pattern are considered different if the sets of the segments that replicate the pattern are different.

Example of a pattern (left side) and a drawing (right side)..\text{Example of a pattern (left side) and a drawing (right side).}.

One should notice that the pattern is replicated by segments {AB,DC}\{AB, DC\}, even though there are other segments crossing it.

Note also that the subset {IJ,FK}\{IJ, FK\} does not form the pattern, even though the shape appears in its structure.

Task

As Benewski's now busy with his new hobby (fencing), he counts on you to help him with this task.

You are given two integers aa and bb. Compute the sum of those two integers.

Input data

The first line contains SS, the number of segments that describe the patttern.

The next SS lines will each contain integers X1,Y1,X2,Y2X'_1, Y'_1, X'_2, Y'_2, representing the coordinates of the end points of each segment (i.e. the segment has endpoints (X1,Y1)(X'_1, Y'_1) and (X2,Y2)(X'_2, Y'_2)).

The next line contains NN, the number of segments in the drawing.

The next NN lines will each contain integers X1,Y1,X2,Y2X_1, Y_1, X_2, Y_2, representing the coordinates of the end points of each segment (endpoints (X1,Y1)(X_1, Y_1) and (X2,Y2)(X_2, Y_2)).

Output data

You need to write a single integer, representing the number of occurences of the pattern.

Constraints and clarifications

  • All segments given in the drawing are distinct (there are no duplicates).
  • All segments given in the pattern are distinct.
  • 1S501 \leq S \leq 50.
  • 0X1,Y1,X2,Y21000 \leq X'_1, Y'_1, X'_2, Y'_2 \leq 100.
  • 1N1051 \leq N \leq 10^5.
  • 109X1,Y1,X2,Y2109-10^9 \leq X_1, Y_1, X_2, Y_2 \leq 10^9.
# Score Constraints
1 0 Examples
2 20 S=1.S = 1.
3 10 N20.N \leq 20.
4 10 N5000.N \leq 5000.
5 20 0X1,Y1,X2,Y25000.0 \leq X_1, Y_1, X_2, Y_2 \leq 5000.
6 40 No additional restrictions

Example 1

stdin

2
2 0 0 2
1 0 2 2
6
4 5 7 2
5 0 7 4
5 0 3 3
4 1 2 3
2 1 3 4
3 1 4 3

stdout

1

Explanation

The first sample case is explained in figure 11.

Example 2

stdin

3
0 0 0 1
1 0 1 1
0 1 1 0
15
-1 2 -1 3
-1 3 0 2
0 2 0 3
0 3 1 2
1 2 1 3
1 3 2 2
2 2 2 3
2 3 -1 1
-1 1 -1 2
-1 2 0 1
0 1 0 2
0 2 1 1
1 1 1 2
1 1 2 2
2 1 2 2

stdout

5

Explanation

The second sample case: pattern (left side) and drawing (right side)..\text{The \textbf{second sample case}: pattern (left side) and drawing (right side).}.

For the second sample case, notice how segments can be part of multiple patterns.

The 5 occurences of the pattern are

  • {AB,BC,CD}\{\textcolor{red}{AB}, \textcolor{red}{BC}, \textcolor{red}{CD}\},
  • {CD,DE,EF}\{\textcolor{blue}{CD}, \textcolor{blue}{DE}, \textcolor{blue}{EF}\},
  • {EF,FG,GH}\{\textcolor{orange}{EF}, \textcolor{orange}{FG}, \textcolor{orange}{GH}\},
  • {KA,AJ,JC}\{\textcolor{green}{KA}, \textcolor{green}{AJ}, \textcolor{green}{JC}\},
  • {JC,CI,IE}\{\textcolor{violet}{JC}, \textcolor{violet}{CI}, \textcolor{violet}{IE}\}.

Log in or sign up to be able to send submissions!