Skip to content

Commit 16b39df

Browse files
authored
Merge pull request #516 from ayush2005k/day-09-ayush2005k
Add Day-09 Salary Queries solution (C++)
2 parents b516c3b + 569dbd2 commit 16b39df

1 file changed

Lines changed: 122 additions & 0 deletions

File tree

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
Problem: Salary Queries
3+
Source: CSES
4+
Problem Link: https://cses.fi/problemset/task/1144/
5+
6+
Short Problem Statement:
7+
A company has n employees with salaries.
8+
You need to process two types of queries:
9+
1) Update an employee’s salary.
10+
2) Count how many employees have salaries in a given range [a, b].
11+
12+
Approach:
13+
- Use coordinate compression because salaries can be up to 1e9.
14+
- Maintain frequencies of salaries using a Fenwick Tree (Binary Indexed Tree).
15+
- For update queries, decrease the old salary count and increase the new one.
16+
- For range queries, use prefix sums from the Fenwick Tree.
17+
18+
Time Complexity:
19+
O((n + q) log (n + q))
20+
21+
Space Complexity:
22+
O(n + q)
23+
24+
Example:
25+
Input:
26+
5 3
27+
3 7 2 2 5
28+
? 2 3
29+
! 3 6
30+
? 2 3
31+
32+
Output:
33+
3
34+
2
35+
36+
Submission Link:
37+
https://cses.fi/paste/129b78ff774776c0f188fe/
38+
*/
39+
40+
#include <bits/stdc++.h>
41+
using namespace std;
42+
43+
struct Fenwick {
44+
int n;
45+
vector<int> bit;
46+
47+
Fenwick(int n) : n(n), bit(n + 1, 0) {}
48+
49+
void update(int i, int v) {
50+
for (; i <= n; i += i & -i)
51+
bit[i] += v;
52+
}
53+
54+
int query(int i) {
55+
int s = 0;
56+
for (; i > 0; i -= i & -i)
57+
s += bit[i];
58+
return s;
59+
}
60+
};
61+
62+
int main() {
63+
ios::sync_with_stdio(false);
64+
cin.tie(nullptr);
65+
66+
int n, q;
67+
cin >> n >> q;
68+
69+
vector<int> salary(n);
70+
for (int i = 0; i < n; i++)
71+
cin >> salary[i];
72+
73+
vector<int> all = salary;
74+
vector<pair<char, pair<int, int>>> queries;
75+
76+
for (int i = 0; i < q; i++) {
77+
char type;
78+
cin >> type;
79+
if (type == '!') {
80+
int k, x;
81+
cin >> k >> x;
82+
queries.push_back({type, {k - 1, x}});
83+
all.push_back(x);
84+
} else {
85+
int a, b;
86+
cin >> a >> b;
87+
queries.push_back({type, {a, b}});
88+
all.push_back(a);
89+
all.push_back(b);
90+
}
91+
}
92+
93+
// Coordinate compression
94+
sort(all.begin(), all.end());
95+
all.erase(unique(all.begin(), all.end()), all.end());
96+
97+
auto get_id = [&](int x) {
98+
return int(lower_bound(all.begin(), all.end(), x) - all.begin()) + 1;
99+
};
100+
101+
Fenwick fw(all.size());
102+
103+
for (int x : salary)
104+
fw.update(get_id(x), 1);
105+
106+
for (auto &qr : queries) {
107+
if (qr.first == '!') {
108+
int idx = qr.second.first;
109+
int x = qr.second.second;
110+
111+
fw.update(get_id(salary[idx]), -1);
112+
fw.update(get_id(x), 1);
113+
salary[idx] = x;
114+
} else {
115+
int a = qr.second.first;
116+
int b = qr.second.second;
117+
cout << fw.query(get_id(b)) - fw.query(get_id(a) - 1) << "\n";
118+
}
119+
}
120+
121+
return 0;
122+
}

0 commit comments

Comments
 (0)