Editorial for Bubble Cup V8 I Robots protection
Submitting an official solution before solving the problem yourself is a bannable offence.
To solve this problem, we should first take a look at one easier problem. Consider having the same problem statement, but with rectangles instead of triangles. The solution to this problem is pretty straightforward. We will store a matrix representing points in our coordinate system. For each type 1 query, given the rectangle , we would add to the points and , and to the points and . Notice that we expanded the given rectangle when adding s and s, this is because the point is considered in the rectangle even when it is on the border! This way, when a type 2 query is received, to find the answer we simply sum every value in the rectangle to . Since simply summing the points in rectangles is , we should use binary indexed tree for this, hence getting the sufficient time complexity per query.
We will use this approach with some modification to solve the original problem. For handling the triangles we need to introduce new coordinate systems. Depending on the type of triangle (types 1 and 4 are analogous, so are types 2 and 3) we will make two new coordinate systems, where the corresponding hypotenuses are parallel to one axis. For types 2 and 3, point in the original coordinate system would map to , and for types 1 and 4, point would map to , where is the size of the coordinate plane given in the input.
The problem is now somewhat abstracted to the simple rectangle problem. This time we will need three matrices, one representing the original coordinate system and two representing the introduced coordinate systems. For each triangle we need to border it with s and s, similarly as in the rectangle case, only using 2 matrices for each triangle. When adding border for the cathetus we use the original coordinate system and for the hypotenuse we need one of the two introduced coordinate systems, depending on the type of the triangle. Remember, the point belongs to the triangle even when it is on one of catheti or hypotenuse, so be careful.
For calculating the answer for type 2 query, we need to sum the values in the rectangle from to in all three matrices (of course, needs to be mapped accordingly to the coordinate system each matrix represent). Again, to not exceed time limit we need to use binary indexed trees.
Since we are using binary indexed tree, the time complexity of this solution is .
Comments