Skip to content

Commit 79282ba

Browse files
authored
Merge pull request #512 from Krishna200608/feature/issue-507-salary-queries
Solved Issue #507: Salary Queries (CSES 1144)
2 parents db473f0 + a9b2c82 commit 79282ba

1 file changed

Lines changed: 140 additions & 0 deletions

File tree

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
Submission Link:
3+
https://cses.fi/paste/df31068806cfc4bef186a9/
4+
*/
5+
6+
/*
7+
Problem: Salary Queries
8+
Link: https://cses.fi/problemset/task/1144/
9+
Author: Krishna200608
10+
11+
Short Problem Statement:
12+
Maintain an array of salaries. Process queries to update a salary or count employees
13+
with salaries in a range [a, b]. Values up to 10^9 require coordinate compression.
14+
15+
Approach:
16+
Coordinate Compression: Collect all initial salaries and update values.
17+
Sort and remove duplicates to map large values to indices.
18+
Use a Fenwick Tree (BIT) over compressed indices.
19+
Point updates for salary changes.
20+
Range queries using prefix sums on BIT.
21+
22+
Time : O(Q log(N+Q))
23+
Space Complexity: O(N+Q)
24+
*/
25+
26+
#include <bits/stdc++.h>
27+
using namespace std;
28+
29+
#define MOD 1000000007
30+
const int M = 1e9 + 7;
31+
const double PI = acos(-1.0);
32+
#define INF 1e18
33+
#define pb push_back
34+
#define ppb pop_back
35+
#define ll long long
36+
#define no cout << "NO" << endl
37+
#define yes cout << "YES" << endl
38+
#define ff first
39+
#define ss second
40+
#define inn(x) int x; cin >> x
41+
#define ill(x) ll x; cin >> x
42+
#define all(x) x.begin(), x.end()
43+
#define in(a) for (int i = 0; i < (int)a.size(); i++) cin >> a[i]
44+
#define out(a) for (int i = 0; i < (int)a.size(); i++) cout << a[i] << " "
45+
typedef vector<int> vi;
46+
typedef vector<ll> vll;
47+
#define ceil_div(n, x) (((n) % (x) == 0) ? ((n) / (x)) : ((n) / (x) + 1))
48+
#define debug(x) cout << "x -> " << x << endl
49+
#define outt(x) cout << x << endl
50+
#define endl "\n"
51+
52+
const int MAX_M = 600005;
53+
int bit[MAX_M];
54+
int max_val;
55+
56+
void update(int idx, int val) {
57+
for (; idx <= max_val; idx += idx & -idx)
58+
bit[idx] += val;
59+
}
60+
61+
int query(int idx) {
62+
int sum = 0;
63+
for (; idx > 0; idx -= idx & -idx)
64+
sum += bit[idx];
65+
return sum;
66+
}
67+
68+
void solve() {
69+
int n, q;
70+
if (!(cin >> n >> q)) return;
71+
72+
vi p(n);
73+
vi vals;
74+
75+
for (int i = 0; i < n; i++) {
76+
cin >> p[i];
77+
vals.pb(p[i]);
78+
}
79+
80+
struct Qry {
81+
char type;
82+
int k, x;
83+
};
84+
85+
vector<Qry> qs(q);
86+
87+
for (int i = 0; i < q; i++) {
88+
cin >> qs[i].type >> qs[i].k >> qs[i].x;
89+
if (qs[i].type == '!') vals.pb(qs[i].x);
90+
}
91+
92+
sort(all(vals));
93+
vals.erase(unique(all(vals)), vals.end());
94+
max_val = vals.size();
95+
96+
auto get_idx = [&](int v) {
97+
return lower_bound(all(vals), v) - vals.begin() + 1;
98+
};
99+
100+
for (int x : p) {
101+
update(get_idx(x), 1);
102+
}
103+
104+
for (int i = 0; i < q; i++) {
105+
if (qs[i].type == '!') {
106+
int k = qs[i].k - 1;
107+
int x = qs[i].x;
108+
update(get_idx(p[k]), -1);
109+
p[k] = x;
110+
update(get_idx(x), 1);
111+
} else {
112+
int a = qs[i].k;
113+
int b = qs[i].x;
114+
115+
int r = upper_bound(all(vals), b) - vals.begin();
116+
int l = lower_bound(all(vals), a) - vals.begin();
117+
118+
outt(query(r) - query(l));
119+
}
120+
}
121+
}
122+
123+
signed main() {
124+
ios_base::sync_with_stdio(false);
125+
cin.tie(nullptr);
126+
cout.tie(nullptr);
127+
128+
auto begin = chrono::high_resolution_clock::now();
129+
130+
int t = 1;
131+
while (t--) {
132+
solve();
133+
}
134+
135+
auto end = chrono::high_resolution_clock::now();
136+
auto elapsed = chrono::duration_cast<chrono::nanoseconds>(end - begin);
137+
cerr << "Time measured: " << elapsed.count() * 1e-6 << "ms";
138+
139+
return 0;
140+
}

0 commit comments

Comments
 (0)