Solution Find Servers That Handled Most Number of Requests
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public $tree = [];\n\n    public function construct_tree($k) {\n        for ($i = 0; $i < $k; $i++) {\n            $this->tree[$i + $k] = $i;\n        }\n        for ($i = $k - 1; $i > 0; --$i) {\n            $this->tree[$i] = min($this->tree[$i * 2], $this->tree[$i * 2 + 1]);\n        }\n    }\n\n    public function query($k, $l, $r) {\n        $l += $k;\n        $r += $k;\n        $res = PHP_INT_MAX;\n        while ($l < $r) {\n            if ($l & 1) {\n                $res = min($res, $this->tree[$l++]);\n            }\n            if ($r & 1) {\n                $res = min($res, $this->tree[--$r]);\n            }\n            $l >>= 1;\n            $r >>= 1;\n        }\n        return $res;\n    }\n\n    public function update($k, $i, $val) {\n        $this->tree[$i += $k] = $val;\n        for ($i >>= 1; $i > 0; $i >>= 1) {\n            $this->tree[$i] = min($this->tree[$i * 2], $this->tree[$i * 2 + 1]);\n        }\n    }\n\n    public function busiestServers($k, $arrival, $load) {\n        $busy = array_fill(0, $k, false);\n        $n = count($arrival);\n        $this->tree = array_fill(0, $k * 2, PHP_INT_MAX);\n        $count = [];\n        $pq = new SplPriorityQueue();\n        $pq->setExtractFlags(SplPriorityQueue::EXTR_BOTH);\n\n        $this->construct_tree($k);\n        for ($i = 0; $i < $n; ++$i) {\n            while (!$pq->isEmpty() && -$pq->top()[\'priority\'] <= $arrival[$i]) {\n                $server_id = $pq->extract()[\'data\'];\n                $this->update($k, $server_id, $server_id); // mark as available\n                $busy[$server_id] = false;\n            }\n\n            $server_id = $i % $k;\n            if (!$busy[$server_id]) {\n                if (!isset($count[$server_id])) {\n                    $count[$server_id] = 0;\n                }\n                $count[$server_id]++;\n                $busy[$server_id] = true;\n                $pq->insert($server_id, -($arrival[$i] + $load[$i]));\n                $this->update($k, $server_id, PHP_INT_MAX); // mark as unavailable\n                continue;\n            }\n\n            $id = PHP_INT_MAX;\n            // find next available\n            if ($server_id + 1 < $k) {\n                $id = $this->query($k, $server_id + 1, $k);\n                if ($id == PHP_INT_MAX) {\n                    $id = $this->query($k, 0, $server_id);\n                }\n            } else {\n                $id = $this->query($k, 0, $k);\n            }\n        \n            // there is no available server\n            if ($id == PHP_INT_MAX) {\n                continue;\n            }\n\n            if (!isset($count[$id])) {\n                $count[$id] = 0;\n            }\n            $count[$id]++;\n            $busy[$id] = true;\n            $this->update($k, $id, PHP_INT_MAX); \n            $pq->insert($id, -($arrival[$i] + $load[$i]));\n        }\n\n        $max_freq = 0;\n        foreach ($count as $v) {\n            $max_freq = max($max_freq, $v);\n        }\n\n        $res = [];\n        foreach ($count as $server_id => $v) {\n            if ($max_freq == $v) {\n                $res[] = $server_id;\n            }\n        }\n\n        return $res;\n    }\n}\n\n```
['PHP']