PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SRD0000081. Câu lạc bộ hình học ẩn danh (Geometers Anonymous Club)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Câu lạc bộ hình học ẩn danh (Geometers Anonymous Club)

Geometers Anonymous Club

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Given nn convex polygons with vertices listed counterclockwise, output the number of vertices of their Minkowski sum for each interval [l,r][l,r]. The Minkowski sum A+BA+B is the set {a+b∣a∈A,b∈B}\{a+b\mid a\in A,b\in B\}. Each polygon has at least three vertices and no three consecutive vertices are collinear.

Input

Read nn polygons; for polygon ii, read kik_i and then its kik_i CCW vertex coordinates. Then read qq and its qq intervals.

Output

Print the vertex count of the Minkowski sum for each query.

Subtasks

  • Subtask 1 (10 points): 1≤n≤201\le n\le 20, 1≤q≤301\le q\le 30. 3≤ki3\le k_i; ∑ki≤300000\sum k_i\le300000; ∣xij∣,∣yij∣≤109|x_{ij}|,|y_{ij}|\le10^9; đa giác lồi nghiêm ngặt, CCW; 1≤l≤r≤n1\le l\le r\le n.
  • Subtask 2 (20 points): 1≤n≤10001\le n\le 1000, 1≤q≤10001\le q\le 1000. 3≤ki3\le k_i; ∑ki≤300000\sum k_i\le300000; ∣xij∣,∣yij∣≤109|x_{ij}|,|y_{ij}|\le10^9; đa giác lồi nghiêm ngặt, CCW; 1≤l≤r≤n1\le l\le r\le n.
  • Subtask 3 (25 points): 1≤n≤100001\le n\le 10000, 1≤q≤100001\le q\le 10000. 3≤ki3\le k_i; ∑ki≤300000\sum k_i\le300000; ∣xij∣,∣yij∣≤109|x_{ij}|,|y_{ij}|\le10^9; đa giác lồi nghiêm ngặt, CCW; 1≤l≤r≤n1\le l\le r\le n.
  • Subtask 4 (45 points): 1≤n≤1000001\le n\le 100000, 1≤q≤1000001\le q\le 100000. 3≤ki3\le k_i; ∑ki≤300000\sum k_i\le300000; ∣xij∣,∣yij∣≤109|x_{ij}|,|y_{ij}|\le10^9; đa giác lồi nghiêm ngặt, CCW; 1≤l≤r≤n1\le l\le r\le n.

Examples

Input

3
3
0 0
1 0
0 1
4
1 1
1 2
0 2
0 1
3
2 2
1 2
2 1
3
1 2
2 3
1 3

Output

5
5
6

Explanation

The first two polygons have five distinct edge directions altogether, so their Minkowski sum has five vertices. The last two also yield five, while all three together yield six.