#P9279. IP Blacklist and Whitelist Management
IP Blacklist and Whitelist Management
IP Blacklist and Whitelist Management
You are given a range of IP addresses from \(0\) to \(10^9\). Initially, all clients can access all IPs.
There are \(N\) countries (numbered from \(0\) to \(N-1\)). Each country has its own set of IP intervals (formally, a union of several intervals). In addition, there are \(M\) clients (numbered from \(0\) to \(M-1\)).
You need to process \(Q\) operations. The operations are of the following types:
- 1 X: Add country \(X\) (and all countries merged with \(X\)) to the global blacklist (i.e. for all clients, the IP intervals corresponding to that country’s group become banned).
- 2 X Y: Add the IP interval \([X, Y]\) to the global blacklist.
- 3 C X: For client \(C\), add country \(X\) (and its merged group) to its personal blacklist.
- 4 C X Y: For client \(C\), add the IP interval \([X, Y]\) to its personal blacklist.
- 5 C X: For client \(C\), add country \(X\) (and its merged group) to its whitelist. An IP in the whitelist is always accessible regardless of any blacklist operations.
- 6 C X Y: For client \(C\), add the IP interval \([X, Y]\) to its whitelist.
- 7 X Y: Merge country \(X\) and country \(Y\) into a single group. Their IP intervals become the union of their individual intervals.
- 8 C X Y: Query: For client \(C\), within the IP interval \([X, Y]\), determine how many IP addresses are accessible. An IP is accessible if it is not in an effective blacklist. Note that whitelist operations override any blacklist operations (both prior and future).
The global blacklist applies to all clients, while each client may have its own additional blacklist and whitelist. Initially, each client has access to all IPs in \([0,10^9]\).
Input constraints: The values in operations and the number of operations are such that a correct simulation is possible using advanced interval manipulation. All interval bounds and country indices are within their valid ranges.
inputFormat
The first line contains three integers \(N\), \(M\), and \(Q\): the number of countries, the number of clients, and the number of operations respectively.
Then, \(N\) blocks follow. For each country (from \(0\) to \(N-1\)): the first number is an integer \(T\) representing the number of intervals for that country, followed by \(T\) pairs of integers \(L\) and \(R\) (with \(0 \le L \le R \le 10^9\)) describing the IP intervals for that country.
Then \(Q\) lines follow, each representing an operation. The operations are formatted as described:
- Type 1:
1 X
- Type 2:
2 X Y
- Type 3:
3 C X
- Type 4:
4 C X Y
- Type 5:
5 C X
- Type 6:
6 C X Y
- Type 7:
7 X Y
- Type 8:
8 C X Y
(a query operation)
It is guaranteed that all values provided in the operations are within the applicable ranges.
Sample Input:
2 2 7 1 100 200 1 300 400 1 0 3 1 1 5 1 0 2 500 600 6 1 550 570 8 0 0 1000 8 1 0 1000
outputFormat
For each query operation (type 8), output one line containing a single integer representing the number of accessible IP addresses in the specified interval for that client.
Sample Output:
799 820
sample
2 2 7
1 100 200
1 300 400
1 0
3 1 1
5 1 0
2 500 600
6 1 550 570
8 0 0 1000
8 1 0 1000
799
820
</p>