Problem solution · C++

Sum of Beautiful Subsequences

Sum of Beautiful Subsequences: 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
138 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 Sum of Beautiful Subsequences, 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

  • 138 lines of C++ from the credited upstream file sum-of-beautiful-subsequences.cpp.
  • The implementation visibly relies on sequence storage, hash lookup.
  • 17 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 codeSum of Beautiful Subsequences · C++C++
Use this to learn the idea, then write your own version.
// Time:  precompute: O(rlogr), r = max_nums//        runtime:    O(mx + nlogr * (log(nlogr) + logn)), mx = max(nums)// Space: O(rlogr) // number theory, bit, fenwick treeconst int MOD = 1e9 + 7; class 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] = (bit_[i] + val) % MOD;        }    }     int query(int i) const {        ++i;        int total = 0;        for (; i > 0; i -= lower_bit(i)) {            total = (total + bit_[i]) % MOD;        }        return total;    } private:    inline int lower_bit(int i) const {        return i & -i;    }        vector<int> bit_;}; const auto& factors = [](int n) {  // Time: O(nlogn)    vector<vector<int>> result(n + 1);    for (int i = 1; i <= n; ++i) {        for (int j = i; j <= n; j += i) {            result[j].emplace_back(i);        }    }    return result;}; const auto& phi_sieve = [](int n) {  // Time: O(nlog(logn))    vector<int> phi(n + 1);    iota(begin(phi), end(phi), 0);    for (int i = 2; i <= n; ++i) {        if (phi[i] != i) {            continue;        }        for (int j = i; j <= n; j += i) {            phi[j] -= phi[j] / i;        }    }    return phi;}; const int MAX_NUM = 7 * 1e4;const auto& FACTORS = factors(MAX_NUM);const auto& PHI = phi_sieve(MAX_NUM);class Solution {public:    int totalBeauty(vector<int>& nums) {        const auto& mx = ranges::max(nums);        vector<int> val_to_idx(mx + 1);        const auto& count = [&](const auto& arr){            vector<int> sorted_arr(arr);            sort(begin(sorted_arr), end(sorted_arr));            for (int i = 0; i < size(sorted_arr); ++i) {  // coordinate compression                val_to_idx[sorted_arr[i]] = i;            }            BIT bit(size(arr));            for (const auto& x : arr) {                bit.add(val_to_idx[x], bit.query(val_to_idx[x] - 1) + 1);            }            return bit.query(size(arr) - 1);        };         vector<vector<int>> lookup(ranges::max(nums) + 1);        for (const auto& x : nums) {            for (const auto& d : FACTORS[x]) {                lookup[d].emplace_back(x);            }        }        int result = 0;        vector<int> cnt(mx + 1);        for (int64_t g = size(cnt) - 1; g >= 1; --g) {            result = (result + (static_cast<int64_t>(PHI[g]) * count(lookup[g])) % MOD) % MOD;        }        return result;    }}; // Time:  precompute: O(rlogr), r = max_nums//        runtime:    O(mx * log(mx) + nlogr * (log(nlogr) + logn)), mx = max(nums)// Space: O(rlogr)// number theory, bit, fenwick treeclass Solution2 {public:    int totalBeauty(vector<int>& nums) {        const auto& count = [&](const auto& arr){            unordered_set<int> arr_set(cbegin(arr), cend(arr));            vector<int> sorted_arr(cbegin(arr_set), cend(arr_set));            sort(begin(sorted_arr), end(sorted_arr));            unordered_map<int, int> val_to_idx;            for (int i = 0; i < size(sorted_arr); ++i) {  // coordinate compression                val_to_idx[sorted_arr[i]] = i;            }            BIT bit(size(val_to_idx));            for (const auto& x : arr) {                bit.add(val_to_idx[x], bit.query(val_to_idx[x] - 1) + 1);            }            return bit.query(size(val_to_idx) - 1);        };         const auto& mx = ranges::max(nums);        vector<vector<int>> lookup(ranges::max(nums) + 1);        for (const auto& x : nums) {            for (const auto& d : FACTORS[x]) {                lookup[d].emplace_back(x);            }        }        int result = 0;        vector<int> cnt(mx + 1);        for (int64_t g = size(cnt) - 1; g >= 1; --g) {            cnt[g] = count(lookup[g]);            for(int ng = g + g; ng <= mx; ng += g){                cnt[g] = ((cnt[g] - cnt[ng]) % MOD + MOD) % MOD;            }            result = (result + (g * cnt[g]) % MOD) % MOD;        }        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 ↗