Code with Luv logoNavigate back to the homepage

Inversion Count and Range Sum Querries using BIT

Luv
October 19th, 2020 · 1 min read

Introduction

In the last article, we studied how Binary Indexed Trees work and how they can be implemented. If you haven’t read that article yet, I would highly suggest to first go and take a look at that article first here

Questions

BIT can be used in many questions and there are various techniques to use BIT as well. For example, it can be directly used to perform Range sum queries or it can be used as a hash array to solve questions like Inversion Count. I have discussed both these approaches in the video below in detail.

BIT Questions and Code

The C++ code of both the questions discussed in the video above is as follows.

Range Sum Query

Given an array of size N and Q queries Solve two types of quereies
Type 1 : i x : replace ith index by value x
Type 2 : l r : find sum from l to r

Input
15 4
23 4 5 6 3
32 2 4
41 2 6
52 2 4
62 1 5
Output
115
217
323
Code
1#include<bits/stdc++.h>
2using namespace std;
3
4const int N = 1e5+10;
5int bit[N];
6
7void update(int i, int x){
8 for(; i < N; i += (i&-i))
9 bit[i] += x;
10}
11
12int sum(int i){
13 int ans = 0;
14 for(; i > 0; i -= (i&-i))
15 ans += bit[i];
16 return ans;
17}
18
19int main(){
20 int n, q;
21 cin >> n >> q;
22 int a[n+10];
23 for(int i = 1;i <= n; ++i){
24 cin >> a[i];
25 update(i, a[i]);
26 }
27
28 while(q--){
29 int type;
30 cin >> type;
31 if(type == 1){
32 int i, x;
33 cin >> i >> x;
34 update(i, x - a[i]);
35 a[i] = x;
36 }
37 else{
38 int l, r;
39 cin >> l >> r;
40 cout << sum(r) - sum(l-1) << endl;
41 }
42 }
43}

Inversion Count

Two elements a[i] and a[j] form an inversion if a[i] > a[j] and i < j. We need to count all such pairs.

Input
15
28 4 9 2 8
Output
15
Input
15
2100000000 10000 10000000000 10 100000000
Output
15
Code
1#include<bits/stdc++.h>
2using namespace std;
3
4const int N = 1e5+10;
5int bit[N];
6
7void update(int i, int x){
8 for(; i < N; i += (i&-i))
9 bit[i] += x;
10}
11
12int sum(int i){
13 int ans = 0;
14 for(; i > 0; i -= (i&-i))
15 ans += bit[i];
16 return ans;
17}
18
19int main(){
20 int n;
21 cin >> n;
22 long long a[n+10];
23 map<long long,int> mp;
24 for(int i = 1;i <= n; ++i){
25 cin >> a[i];
26 mp[a[i]];
27 }
28
29
30 // compression of numbers for the case where a[i] > 10 ^ 6
31 int ptr = 1;
32 for(auto &pr : mp){
33 pr.second = ptr++;
34 }
35
36 for(int i = 1; i <= n; ++i){
37 a[i] = mp[a[i]];
38 }
39
40 // Finding Inversion count
41
42
43 int inversion_ct = 0;
44 for(int i = 1; i<= n; ++i){
45 inversion_ct += (sum(N-5) - sum(a[i]));
46 update(a[i], 1);
47 }
48
49 cout << inversion_ct << endl;
50
51
52}

BIT Questions

Range Sumer Queries| Leetcode
Inversion Count| Spoj
Almost Difference | Codeforces

I hope you all found the article as well as the video helpful. If you have any questions or doubt you can comment on the youtube video itself. I try to reply to most of the doubt questions. Feel free to ping me on any social media. Links can be found at bottom of the page.

More articles from Luv

Binary Indexed Trees / Fenwick Trees Tutorial

Binary Indexed Trees(BIT) is considered as an advanced data structure but it's quite easy to implement and use and has a lot of applications in CP as well

October 18th, 2020 · 2 min read

Job in Cisco, India | Interview Questions | CTC | Work Culture | Off-Campus

Every year Cisco hires a lot of software engineers but a lot of students have a doubt about applying for Cisco due to it being a networking company.

October 25th, 2020 · 3 min read
© 2020 Luv
Link to $https://www.youtube.com/c/LuvIsMe?sub_confirmation=1Link to $https://www.instagram.com/i._m_.luv/Link to $https://twitter.com/luvk1412Link to $https://www.linkedin.com/in/luvk1412/Link to $https://github.com/luvk1412