Maximum Sum Queries
import math\nfrom typing import List\nimport bisect\n\n\nclass Solution:\n    def maximumSumQueries(self, nums1: List[int], nums2: List[int], queries: List[List
import math\nfrom typing import List\nimport bisect\n\n\nclass Solution:\n    def maximumSumQueries(self, nums1: List[int], nums2: List[int], queries: List[List[int]]) -> List[int]:\n        def buildSparseTable(arr, n):\n            k = int(math.log2(n))\n            st = [[None for j in range(n)] for i in range(k + 1)]\n            for i in range(k + 1):\n                for j in range(n - (1 << i) + 1):\n                    if i == 0:\n                        st[i][j] = arr[j]\n                    else:\n                        st[i][j] = max(st[i - 1][j], st[i - 1][j + (1 << (i - 1))])\n            return st\n\n        def getMax(st, l, r):\n            i = int(math.log2(r - l + 1))\n            return max(st[i][l], st[i][r - (1 << i) + 1])\n\n        n = len(nums1)\n        a = sorted([[nums1[i], nums2[i]] for i in range(n)])\n        max_y = [v[1] for v in a]\n        for i in range(n - 2, -1, -1):\n            max_y[i] = max(max_y[i], max_y[i + 1])\n        _sum = [v[0] + v[1] for v in a]\n        st_max_sum = buildSparseTable(_sum, n)\n        res = []\n        for x, y in queries:\n            i = bisect.bisect_left(a, [x, y])\n            if i == n or max_y[i] < y:\n                res.append(-1)\n                continue\n            left, right = i, n - 1\n            while left < right:\n                m = (left + right + 1) // 2\n                if max_y[m] >= y:\n                    left = m\n                else:\n                    right = m - 1\n            j = right\n            res.append(getMax(st_max_sum, i, j))\n        return res
[]