The maximum distance in a cardinal plane

3.6

13 votes
Segment tree, 3-D, Algorithms, Convex Hull, Math
Problem

You are given an infinite 2D cardinal plane but this plane is not normal. And, you have many balls but the balls are not also normal. Upon each ball a coordinate value (x,y) is written which means that you can place this ball on this point on the plane. For a given value of k, the power of a ball is defined as f(k)=(|x+y|k)2. Initially, you have N containers and each container has a ball. Now, you will perform two types of queries that are defined as:

  • + i x y : Put a ball in the ith container that has (x,y) written upon it.
  • ? l r k : Now choose 2 balls from among all balls contained in the containers from l to r such that |fi(k)fj(k)| is maximum possible and print this value. Here fi(k) denotes the power of a ball chosen from the ith container for a particular value of k.

For each query of type ?, find the maximum possible value of |fi(k)fj(k)| for the given k.

Note: It is not necessary to select two balls from different containers.

Input format

  • The first line contains the number N denotes the number of containers.
  • Next N lines contain two space-separated integers x and y denotes the coordinate written upon the ith ball.
  • The next line contains Q integers indicating the number of queries to be performed. 
  • Next Q lines contain the details of that query with the format as described in the problem statement.

Output format

For each query of type ?, print the maximum possible value of |fi(k)fj(k)| for the given k in the new line.

Constraints

2N105

1Q2105

1iN

1l<rN

108x,y,k108

Time Limit: 8
Memory Limit: 512
Source Limit:
Explanation

-

Editor Image

?