[C++] O(nlogn + qlogn) 200ms - Segment Tree
cpp\nclass Solution {\npublic:\n    vector<int> maximumSumQueries(vector<int>& nums1, vector<int>& nums2, vector<vector<int>>& queries) {\n\n        vector<pair
```cpp\nclass Solution {\npublic:\n    vector<int> maximumSumQueries(vector<int>& nums1, vector<int>& nums2, vector<vector<int>>& queries) {\n\n        vector<pair<int,int>> pairs(nums1.size());\n        transform(nums1.begin(), nums1.end(), nums2.begin(), pairs.begin(),\n                [](int a, int b) { return make_pair(a,b); });\n        sort(pairs.begin(), pairs.end(), greater<>());\n\n        auto dst = pairs.begin();\n        for (auto src = next(pairs.begin()); src != pairs.end(); ++src)\n            if (src->second > dst->second) *(++dst) = *src;\n        pairs.erase(next(dst), pairs.end());\n\n        const unsigned M = pairs.size(), N = bit_ceil(M);\n        vector<int> dp(N*2);\n\n        transform(pairs.begin(), pairs.end(), dp.begin()+N, \n            [](const auto& p) { return p.first + p.second; });\n        for (int i=N-1; i>0; --i)\n            dp[i] = max(dp[2*i], dp[2*i+1]);\n\n        vector<int> ans(queries.size(), -1);\n\n        for (int i=queries.size()-1; i>=0; --i) {\n            auto lb = lower_bound(pairs.begin(), pairs.end(), queries[i][1],\n                [](const pair<int,int>& a, int b) { return a.second < b; });\n            auto rb = lower_bound(pairs.rbegin(), pairs.rend(), queries[i][0],\n                [](const pair<int,int>& a, int b) { return a.first < b; });\n \n            if (distance(lb, rb.base()) <= 0)\n                continue;\n\n            unsigned l = N + (lb - pairs.begin()),\n                     r = N + (pairs.rend() - rb - 1),\n                     depth = max(bit_width(l ^ r) - 1, 0);\n\n            ans[i] = max(dp[l], dp[r]);\n\n            while (depth--) {\n                if (!(l & 1))  ans[i] = max(ans[i], dp[l+1]);\n                if (  r & 1 )  ans[i] = max(ans[i], dp[r-1]);\n                l >>= 1, r >>= 1;\n            }\n        }\n\n        return ans;\n    }\n};\n```
['C++']