problems / 122

IP Geolocation Table

easy binary-search parsing arrays 1000 ms · 256 MB

IP Geolocation Table

You are given a geo-IP table: a list of IPv4 address blocks, each owned by one country, followed by a batch of queries asking which block (if any) owns a given address.

An IPv4 address is written as four decimal octets separated by dots, a.b.c.d, where each of a, b, c, d is an integer from 0 to 255 with no leading zeros (so 7 is allowed, 07 is not; 0 is allowed). Internally, convert it to the 32-bit integer a·2^24 + b·2^16 + c·2^8 + d.

Each block is written as exactly one line in one of two forms:

  • Range form: IP1 - IP2 CODE, meaning every address from IP1 to IP2 inclusive (as 32-bit integers, so IP1 ≤ IP2). There may be any number of space characters (zero or more, at most 20) on each side of the -, independently — so 10.0.0.0-10.0.0.9 US, 10.0.0.0 - 10.0.0.9 US and 10.0.0.0 -10.0.0.9 US are all valid and equivalent. Exactly one space separates IP2 from CODE.
  • CIDR form: IP/k CODE, meaning the block of 2^(32-k) addresses obtained by masking IP's 32-bit integer with the top k bits (0 ≤ k ≤ 32, no leading zeros, so k = 0 is the entire address space and k = 32 is a single address). Exactly one space separates IP from CODE and no space surrounds the /.

CODE is exactly two uppercase English letters (A–Z). It is never the string ??, which is reserved below to mean "no owner".

The n blocks are given in increasing order of their starting address and are pairwise non-overlapping: if block i covers integers [s_i, e_i], then s_1 ≤ e_1 < s_2 ≤ e_2 < ... < s_n ≤ e_n. There may be gaps between consecutive blocks (addresses owned by nobody).

Input

The first line contains n (0 ≤ n ≤ 10^5), the number of blocks.

Each of the next n lines describes one block, in the range form or the CIDR form above.

The next line contains q (0 ≤ q ≤ 2·10^5), the number of queries.

Each of the next q lines contains one IPv4 address to look up.

Output

For each query, in order, print one line: the CODE of the block that contains it, or ?? if no block contains it.

Notes

In the first example, 1.0.1.0/24 covers 1.0.1.0 through 1.0.1.255. The address 1.0.2.0 falls in the gap between that block and 2.0.0.0 - 2.0.0.9, so it prints ??. The address 2.0.0.9 is the last address of the third block, so it still counts as owned.

In the second example, 10.0.0.6 -10.0.0.10 DE has a space before the dash but none after it — both are allowed. 10.0.0.5/32 owns only the single address 10.0.0.5.

Example 1

Input

3
1.0.0.0 - 1.0.0.255 AU
1.0.1.0/24 JP
2.0.0.0-2.0.0.9 US
5
1.0.0.5
1.0.1.200
1.0.2.0
2.0.0.9
3.0.0.0

Output

AU
JP
??
US
??

Example 2

Input

2
10.0.0.5/32 FR
10.0.0.6 -10.0.0.10 DE
4
10.0.0.4
10.0.0.5
10.0.0.6
10.0.0.11

Output

??
FR
DE
??

Hints · reveal one at a time if you're stuck

Hint 1

Turn every address into its 32-bit integer and every block into an integer interval. Since the blocks are sorted and disjoint, how could you find the one block that might contain a query without scanning all of them?

Hint 2

Binary search the block starts for the last start not exceeding the query, then check that the query is within that block's end. Be careful when parsing: the spaces around the dash are optional, and for a CIDR prefix of length 0 the mask shift must not be undefined.

report an issue Prefer your own editor? sooks prep 122 (CLI)
Sign in to run code

Run your code on the examples, or submit it against every test.