Colorful Square
Time limit5sMemory limit512 MB
Given colored points on a plane, find the smallest axis-aligned square that contains at least one point of every color.
- Level
Hard8 of 10
- Topics
- Binary search, Sliding window, Segment tree
- Solved
- No attempts yet
Problem
Given points on a plane, each point has one of colors. The color of each point is an integer in .
A square is called a colorful square if, for each of the colors, it contains at least one point of that color. Find the colorful square with the smallest side length and print that side length.
Here, squares must have all four sides horizontal or vertical. A point on the boundary of a square counts as inside it. A square with side length 0, which is a single point, also counts as a valid square.
Input
The first line contains two integers and , separated by a single space.
The next lines follow. The -th line contains three integers , , and , separated by single spaces. They describe the point and its color .
Output
Print the answer to the problem on the first line.
Constraints
- For every (), and are integers from 1 to 250 000.
- For every color (), at least one of the points has color .