|
| 1 | +/* |
| 2 | +Problem: Salary Queries |
| 3 | +
|
| 4 | +Approach: |
| 5 | +- Use coordinate compression because salaries can be up to 1e9. |
| 6 | +- Maintain frequencies of salaries using a Fenwick Tree (Binary Indexed Tree). |
| 7 | +- For update queries, decrease the old salary count and increase the new one. |
| 8 | +- For range queries, use prefix sums from the Fenwick Tree. |
| 9 | +
|
| 10 | +Time Complexity: |
| 11 | +O((n + q) log (n + q)) |
| 12 | +Space Complexity: |
| 13 | +O(n + q) |
| 14 | +Example: |
| 15 | +Input: |
| 16 | +5 3 |
| 17 | +3 7 2 2 5 |
| 18 | +? 2 3 |
| 19 | +! 3 6 |
| 20 | +? 2 3 |
| 21 | +Output: |
| 22 | +3 |
| 23 | +2 |
| 24 | +Submission Link: |
| 25 | +https://cses.fi/paste/129b78ff774776c0f188fe/ |
| 26 | +*/ |
| 27 | + |
| 28 | +#include <bits/stdc++.h> |
| 29 | +using namespace std; |
| 30 | + |
| 31 | +struct Fenwick { |
| 32 | + int n; |
| 33 | + vector<int> bit; |
| 34 | + |
| 35 | + Fenwick(int n) : n(n), bit(n + 1, 0) {} |
| 36 | + |
| 37 | + void update(int i, int v) { |
| 38 | + for (; i <= n; i += i & -i) |
| 39 | + bit[i] += v; |
| 40 | + } |
| 41 | + |
| 42 | + int query(int i) { |
| 43 | + int s = 0; |
| 44 | + for (; i > 0; i -= i & -i) |
| 45 | + s += bit[i]; |
| 46 | + return s; |
| 47 | + } |
| 48 | +}; |
| 49 | + |
| 50 | +int main() { |
| 51 | + int n, q; |
| 52 | + cin >> n >> q; |
| 53 | + |
| 54 | + vector<int> salary(n); |
| 55 | + for (int i = 0; i < n; i++) |
| 56 | + cin >> salary[i]; |
| 57 | + |
| 58 | + vector<int> all = salary; |
| 59 | + vector<pair<char, pair<int, int>>> queries; |
| 60 | + |
| 61 | + for (int i = 0; i < q; i++) { |
| 62 | + char type; |
| 63 | + cin >> type; |
| 64 | + if (type == '!') { |
| 65 | + int k, x; |
| 66 | + cin >> k >> x; |
| 67 | + queries.push_back({type, {k - 1, x}}); |
| 68 | + all.push_back(x); |
| 69 | + } else { |
| 70 | + int a, b; |
| 71 | + cin >> a >> b; |
| 72 | + queries.push_back({type, {a, b}}); |
| 73 | + all.push_back(a); |
| 74 | + all.push_back(b); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + // Coordinate compression |
| 79 | + sort(all.begin(), all.end()); |
| 80 | + all.erase(unique(all.begin(), all.end()), all.end()); |
| 81 | + |
| 82 | + auto get_id = [&](int x) { |
| 83 | + return int(lower_bound(all.begin(), all.end(), x) - all.begin()) + 1; |
| 84 | + }; |
| 85 | + |
| 86 | + Fenwick fw(all.size()); |
| 87 | + |
| 88 | + for (int x : salary) |
| 89 | + fw.update(get_id(x), 1); |
| 90 | + |
| 91 | + for (auto &qr : queries) { |
| 92 | + if (qr.first == '!') { |
| 93 | + int idx = qr.second.first; |
| 94 | + int x = qr.second.second; |
| 95 | + |
| 96 | + fw.update(get_id(salary[idx]), -1); |
| 97 | + fw.update(get_id(x), 1); |
| 98 | + salary[idx] = x; |
| 99 | + } else { |
| 100 | + int a = qr.second.first; |
| 101 | + int b = qr.second.second; |
| 102 | + cout << fw.query(get_id(b)) - fw.query(get_id(a) - 1) << "\n"; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return 0; |
| 107 | +} |
| 108 | +Footer |
0 commit comments