1508.cpp (564B)
1 class Solution { 2 static const int MOD = 1E9 + 7; 3 4 public: 5 int rangeSum(const vector<int> &nums, int n, int left, int right) const { 6 vector<int> vec; 7 for (int i = 0; i < n; i++) { 8 for (int j = i, acc = 0; j < n; j++) { 9 vec.push_back(acc += nums[j]); 10 } 11 } 12 13 nth_element(vec.begin(), vec.begin() + left - 1, vec.end()); 14 nth_element(vec.begin() + left, vec.begin() + right - 1, vec.end()); 15 return accumulate(begin(vec) + left - 1, begin(vec) + right, 0ll) % MOD; 16 } 17 };