Pastries

Time limit1sMemory limit128 MB

Summary
Given up to 100,000 triangles with integer vertices and 100,000 axis-aligned lines, count for each line how many triangles it cuts into two positive-area pieces.
Level

Medium7 of 10

Topics
Geometry, Binary search, Sorting, Implementation
Solved
No attempts yet

Problem

A bakery has baked NN triangular pastries. Every pastry can be described as a triangle whose three vertices have integer coordinates on the 2D plane.

A child wants to cut the pastries with a large knife. Each cut is made along a vertical line x=cx = c or a horizontal line y=cy = c. For a single cut, we want to know how many pastries in total get cut. A pastry is considered cut if the cut divides it into two parts and both parts have area greater than 00.

Given the positions of the pastries and the list of cuts, write a program that determines how many pastries each cut slices through.

Input

The first line contains the number of pastries NN. (2≤N≤100,0002 \le N \le 100{,}000)

Each of the next NN lines contains six non-negative integers, each smaller than 10610^6. In order they are (x1,y1)(x_1, y_1), (x2,y2)(x_2, y_2), (x3,y3)(x_3, y_3), the three vertices of a triangular pastry. No three of these points are collinear. Different pastries may overlap or touch each other.

The next line contains the number of cuts MM. (2≤M≤100,0002 \le M \le 100{,}000)

Each of the next MM lines describes one cut in the form x = c or y = c, where cc is a non-negative integer smaller than 10610^6.

Output

Print, one per line and in the given order, how many pastries each cut slices through. Treat every cut independently; that is, assume the pastry magically becomes whole again after each cut.

Examples2

  1. Example 1

    Input
    3
    1 0 0 2 2 2
    1 3 3 5 4 0
    5 4 4 5 4 4
    4
    x = 4
    x = 1
    y = 3
    y = 1
    
    Expected output
    0
    1
    1
    2
    
  2. Example 2

    Input
    4
    2 7 6 0 0 5
    7 1 7 10 11 11
    5 10 2 9 6 8
    1 9 10 10 4 1
    4
    y = 6
    x = 2
    x = 4
    x = 9
    
    Expected output
    3
    2
    3
    2