Kingdom
Time limit1sMemory limit128 MB
Roads merge cities into connected states over time, and each query asks how many states a horizontal line meets and how many cities those states contain.
- Level
Hard8 of 10
- Topics
- Union-find, Segment tree, Geometry, Intervals
- Solved
- No attempts yet
Problem
In an ancient kingdom there were cities. At first every city was isolated. As time went on the kings ordered roads to be built between cities, and each road is a straight line segment between the two cities it connects.
The cities are partitioned into disjoint groups by road connectivity. A connected group of cities is called a state: a state is made up of some cities together with the roads that link them.
A historical record lists the road constructions in chronological order. A road between two cities and never crosses another road except possibly at a shared endpoint city. Before a road is built, and may already belong to the same state or to two different states; after it is built they belong to the same state, so two states merge into one whenever necessary.
Professor Kim, a historian, wants to answer this question about a past moment: how many states does a horizontal line (the latitude of a particular place) pass through? The figure below shows one possible layout of roads. A circle is a city and a segment is a road. Here there are states: the line passes through two states containing cities in total, and the line passes through one state containing cities.

Write a program that processes the following two kinds of commands:
road A B: a road is built between cities and . It does not cross any other road except at shared endpoint cities. This command only updates the state of the kingdom; your program prints nothing for it.line C: a query. Print how many states the line passes through and the total number of cities in those states.
Input
The input is read from standard input. The first line contains the number of test cases . Each test case has the following form.
The first line contains an integer , the number of cities, with . Each of the next lines contains two integers and with , the coordinates of a city, separated by a single space. The cities are numbered from to in the order they are given.
The next line contains an integer , the number of commands, with . Each of the next lines is either road A B or line C, where and (with ) is a real number whose fractional part is always . At most one road is ever built between a given pair of cities, and every test case contains at least one query.
Output
Write to standard output. For every query, across all test cases, print exactly one line containing two integers: the number of states the line passes through and the total number of cities in those states.