Python | Union-Find
Code\n\nclass LUPrefix:\n\n    def __init__(self, n: int):\n        self.prnt = [-1] * n\n        self.size = [0] * n\n\n    def _find(self, i):\n        while 
# Code\n```\nclass LUPrefix:\n\n    def __init__(self, n: int):\n        self.prnt = [-1] * n\n        self.size = [0] * n\n\n    def _find(self, i):\n        while i != self.prnt[i]:\n            i = self._find(self.prnt[i])\n        return self.prnt[i]\n    \n    def _union(self, x, y):\n        if x < 0 or y >= len(self.prnt):\n            return\n        if self.prnt[x] < 0 or self.prnt[y] < 0:\n            return\n        px, py = self._find(x), self._find(y)\n        if px != py:\n            self.prnt[py] = px\n            self.size[px] += self.size[py]\n\n    def upload(self, v: int) -> None:\n        v -= 1\n        self.prnt[v] = v\n        self.size[v] = 1\n        self._union(v - 1, v)\n        self._union(v, v + 1)\n\n    def longest(self) -> int:\n        return self.size[0]\n```
['Python3']