Problem solution · C++

Count Subarrays with Even Odd Ratio II

Count Subarrays with Even Odd Ratio II: a C++ solution using segment tree or range structure. Learn the idea, check the complexity, and read the full code, with credit to Kamyu LeetCode Solutions.

Technique
Segment tree or range structure
Source
Kamyu LeetCode Solutions
Length
58 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Segment tree or range structure

For Count Subarrays with Even Odd Ratio II, the implementation stores interval information in a range-query data structure so updates and queries avoid rescanning the full input.

  1. Choose the aggregate stored for each interval or prefix.
  2. Build or initialize the structure from the input.
  3. Apply updates and combine the affected nodes to answer each query.

Code notes

  • 58 lines of C++ from the credited upstream file count-subarrays-with-even-odd-ratio-ii.cpp.
  • The implementation visibly relies on sequence storage, hash lookup.
  • 5 loop blocks detected.

Complexity

Count the build once, then multiply the logarithmic update or query path by the number of operations.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from Kamyu LeetCode Solutions by kamyu104 and is used under the MIT licence.

Full codeCount Subarrays with Even Odd Ratio II · C++C++
Use this to learn the idea, then write your own version.
// Time:  O(nlogn)// Space: O(n) // prefix sum, sort, coordinate compression, fenwick treeclass BIT {public:    BIT(int n) : bit_(n + 1) {  // 0-indexed    }        void add(int i, int val) {        ++i;        for (; i < size(bit_); i += lower_bit(i)) {            bit_[i] += val;        }    }     int query(int i) const {        ++i;        int total = 0;        for (; i > 0; i -= lower_bit(i)) {            total += bit_[i];        }        return total;    } private:    inline int lower_bit(int i) const {        return i & -i;    }        vector<int> bit_;}; class Solution {public:    long long countRatioSubarrays(vector<int>& nums, int a, int b) {        vector<int64_t> prefix(size(nums) + 1);        for (int i = 0; i < size(nums); ++i) {            prefix[i + 1] = prefix[i] + (nums[i] % 2 == 0 ? b : -a);        }        vector<int64_t> sorted_nums(prefix);        ranges::sort(sorted_nums);        sorted_nums.erase(unique(begin(sorted_nums), end(sorted_nums)), end(sorted_nums));        unordered_map<int64_t, int> val_to_idx;        for (int i = 0; i < size(sorted_nums); ++i) {            val_to_idx[sorted_nums[i]] = i;        }        BIT bit(size(val_to_idx));        int64_t result = 0;        for (int i = 0; i < size(prefix); ++i) {            const auto& idx = val_to_idx[prefix[i]];            result += i - bit.query(idx - 1);            bit.add(idx, 1);        }        return result;    }}; 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗