- 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
- 137 lines of C++ from the credited upstream file power-update-after-k-th-largest-insertion-i.cpp.
- The implementation visibly relies on sequence storage, hash lookup.
- 11 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 4#include <ext/pb_ds/assoc_container.hpp>5#include <ext/pb_ds/tree_policy.hpp>6using namespace __gnu_pbds;7 89class Solution {10public:11 vector<int> powerUpdate(vector<int>& nums, int p, vector<vector<int>>& queries) {12 static const uint32_t MOD = 1e9 + 7;13 14 const auto& powmod = [&](uint32_t a, uint32_t b, uint32_t mod) {15 a %= mod;16 uint32_t result = 1;17 while (b) {18 if (b & 1) {19 result = (static_cast<uint64_t>(result) * a) % mod;20 }21 a = (static_cast<uint64_t>(a) * a) % mod;22 b >>= 1;23 }24 return result;25 };26 27 using ordered_set = tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update>;28 ordered_set os;29 int i = 0;30 for (; i < size(nums); ++i) {31 os.insert({nums[i], i});32 }33 vector<int> result;34 result.reserve(size(queries));35 for (const auto& q : queries) {36 os.insert({q[0], i++});37 p = powmod(p, os.find_by_order(size(os) - q[1])->first, MOD);38 result.emplace_back(p);39 }40 return result;41 }42};43 44454647class Solution2 {48public:49 vector<int> powerUpdate(vector<int>& nums, int p, vector<vector<int>>& queries) {50 static const uint32_t MOD = 1e9 + 7;51 52 const auto& powmod = [&](uint32_t a, uint32_t b, uint32_t mod) {53 a %= mod;54 uint32_t result = 1;55 while (b) {56 if (b & 1) {57 result = (static_cast<uint64_t>(result) * a) % mod;58 }59 a = (static_cast<uint64_t>(a) * a) % mod;60 b >>= 1;61 }62 return result;63 };64 65 vector<int> sorted_vals(nums);66 for (const auto& q : queries) {67 sorted_vals.emplace_back(q[0]);68 }69 ranges::sort(sorted_vals);70 sorted_vals.erase(unique(begin(sorted_vals), end(sorted_vals)), end(sorted_vals));71 unordered_map<int, int> val_to_idx;72 for (int i = 0; i < size(sorted_vals); ++i) {73 val_to_idx[sorted_vals[i]] = i;74 }75 BIT bit(size(val_to_idx));76 for (const auto& x : nums) {77 bit.add(val_to_idx[x], +1);78 }79 vector<int> result;80 result.reserve(size(queries));81 int total = size(nums);82 for (const auto& q : queries) {83 bit.add(val_to_idx[q[0]], +1);84 const auto& i = bit.kth_element(++total - q[1] + 1);85 p = powmod(p, sorted_vals[i], MOD);86 result.emplace_back(p);87 }88 return result;89 }90 91private:92 class BIT {93 public:94 BIT(int n) : bit_(n + 1) { 95 }96 97 void add(int i, int val) {98 ++i;99 for (; i < size(bit_); i += lower_bit(i)) {100 bit_[i] += val;101 }102 }103 104 int query(int i) const {105 ++i;106 int total = 0;107 for (; i > 0; i -= lower_bit(i)) {108 total += bit_[i];109 }110 return total;111 }112 113 int kth_element(int k) const {114 int total = 0;115 int pos = 0;116 for (int i = floor_log2_x(size(bit_) - 1); i >= 0; --i) {117 if (pos + (1 << i) < size(bit_) && !(total + bit_[pos + (1 << i)] >= k)) {118 total += bit_[pos + (1 << i)];119 pos += (1 << i);120 }121 }122 return (pos + 1) - 1;123 }124 125 private:126 int lower_bit(int i) const {127 return i & -i;128 }129 130 int floor_log2_x(int x) const {131 return bit_width(static_cast<uint32_t>(x)) - 1;132 };133 134 vector<int> bit_;135 };136};137