- Choose the aggregate stored for each interval or prefix.
- Build or initialize the structure from the input.
- Apply updates and combine the affected nodes to answer each query.
Code notes
- 118 lines of C++ from the credited upstream file minimum-operations-to-equalize-subarrays.cpp.
- The implementation visibly relies on sequence storage, hash lookup.
- 7 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.
Use this to learn the idea, then write your own version.
123 45class PersistentSegmentTree {6public:7 PersistentSegmentTree(const auto& vals) 8 : sorted_unique_vals_(vals) {9 10 sort(begin(sorted_unique_vals_), end(sorted_unique_vals_));11 sorted_unique_vals_.erase(unique(begin(sorted_unique_vals_), end(sorted_unique_vals_)), end(sorted_unique_vals_));12 n_ = size(sorted_unique_vals_);13 val_to_idx_.reserve(n_);14 for (int i = 0; i < n_; ++i) {15 val_to_idx_[sorted_unique_vals_[i]] = i;16 }17 build(vals);18 }19 20 int64_t query(int l, int r) {21 int a = roots_[l];22 int b = roots_[r + 1];23 int64_t left_cnt = 0, left_total = 0;24 auto med_cnt = ((r - l + 1) / 2) + 1;25 int left = 0, right = n_ - 1;26 while (left < right) {27 const auto& mid = left + (right - left) / 2;28 const auto& cnt = nodes_[nodes_[b].left].cnt - nodes_[nodes_[a].left].cnt;29 if (med_cnt <= cnt) {30 a = nodes_[a].left;31 b = nodes_[b].left;32 right = mid;33 } else {34 left_cnt += cnt;35 left_total += nodes_[nodes_[b].left].total - nodes_[nodes_[a].left].total;36 med_cnt -= cnt;37 a = nodes_[a].right;38 b = nodes_[b].right;39 left = mid + 1;40 }41 }42 return (sorted_unique_vals_[left] * left_cnt - left_total) +43 (((nodes_[roots_[r + 1]].total - nodes_[roots_[l]].total - left_total)) - sorted_unique_vals_[left] * ((r - l + 1) - left_cnt));44 }45 46private:47 inline int new_node() {48 nodes_.emplace_back();49 return size(nodes_) - 1;50 }51 52 inline int copy_node(int idx) {53 nodes_.emplace_back(nodes_[idx]);54 return size(nodes_) - 1;55 }56 57 void build(const auto& vals) {58 int root = new_node();59 roots_.emplace_back(root);60 for (const auto& x : vals) {61 root = copy_node(root);62 roots_.emplace_back(root);63 int curr = root;64 int left = 0, right = n_ - 1;65 const auto& i = val_to_idx_[x];66 while (left < right) {67 ++nodes_[curr].cnt;68 nodes_[curr].total += x;69 const auto& mid = left + (right - left) / 2;70 if (i <= mid) {71 curr = nodes_[curr].left = copy_node(nodes_[curr].left);72 right = mid;73 } else {74 curr = nodes_[curr].right = copy_node(nodes_[curr].right);75 left = mid + 1;76 }77 }78 ++nodes_[curr].cnt;79 nodes_[curr].total += x;80 }81 }82 83 struct Node {84 int left = 0;85 int right = 0;86 int cnt = 0;87 int64_t total = 0;88 };89 90 int n_;91 vector<int64_t> sorted_unique_vals_;92 unordered_map<int64_t, int> val_to_idx_;93 vector<Node> nodes_;94 vector<int> roots_;95};96 97class Solution {98public:99 vector<long long> minOperations(vector<int>& nums, int k, vector<vector<int>>& queries) {100 vector<int> prefix(size(nums) + 1);101 for (int i = 0; i < size(nums); ++i) {102 prefix[i + 1] = prefix[i] + (i - 1 >= 0 && nums[i] % k != nums[i - 1] % k ? 1 : 0);103 }104 vector<int64_t> vals(size(nums));105 for (int i = 0; i < size(vals); ++i) {106 vals[i] = nums[i] / k;107 }108 PersistentSegmentTree pst(vals);109 vector<long long> result;110 result.reserve(size(queries));111 for (const auto& q : queries) {112 const auto& s = q[0], &t = q[1];113 result.emplace_back(prefix[t + 1] - prefix[s + 1] == 0 ? pst.query(s, t) : -1);114 }115 return result;116 }117};118