Skip to content

Commit 3ea705b

Browse files
authored
Merge pull request #533 from Abh-igyan/patch-5
Add data structure solution q1
2 parents 8834de5 + 5556d1b commit 3ea705b

1 file changed

Lines changed: 54 additions & 0 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//submission: https://cses.fi/paste/dcc99ae97fc9eb1ef1b84d/
2+
3+
// P.S.: Given salaries of n persons (from 1 to n) and queries in form of !,a,b or ?,a,b. If ! is there, then update salary of a person to a as b, if ? is there then find no.
4+
// of people having salary in range [a,b] and print it
5+
#include <bits/stdc++.h>
6+
#include <ext/pb_ds/assoc_container.hpp> // Core PBDS header
7+
//We use a Policy based Data Structure, specifically: Tree (Red Black) with entries in ascending order so that all operations take logk time
8+
using namespace std;
9+
using namespace __gnu_pbds; // PBDS lives in this namespace
10+
11+
using ll = long long;
12+
#define all(x) (x).begin(), (x).end()
13+
14+
template<class T>
15+
using Tree=tree<T,null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
16+
const int inf=1e9+7;
17+
int sal[200005];
18+
19+
20+
void solve() {
21+
int n,q;
22+
cin>>n>>q;
23+
Tree<pair<int,int>> ost;
24+
for(int i=1;i<=n;i++){
25+
cin>>sal[i];
26+
ost.insert({sal[i],i});
27+
}
28+
29+
while(q--){
30+
char c; ll p,r;
31+
cin>>c>>p>>r;
32+
if(c=='!'){
33+
ost.erase({sal[p],p});
34+
ost.insert({r,p});
35+
sal[p]=r;
36+
}
37+
//order_of_key funtion gives elements count less than the element
38+
else{
39+
int count_upto_r=ost.order_of_key({r,inf});
40+
int count_lessthan_p=ost.order_of_key({p,-1});
41+
cout<<count_upto_r-count_lessthan_p<<endl;
42+
}
43+
}
44+
}
45+
// T.C.: O(Q*logn)
46+
//S.C: O(n)
47+
int main() {
48+
// Fast I/O
49+
ios::sync_with_stdio(false);
50+
cin.tie(nullptr);
51+
52+
solve();
53+
return 0;
54+
}

0 commit comments

Comments
 (0)