forked from himanshu010/dsa-programming-codes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathINVCNT.cpp
More file actions
89 lines (74 loc) · 1.78 KB
/
Copy pathINVCNT.cpp
File metadata and controls
89 lines (74 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define ld long double
#define F first
#define S second
#define P pair<int, int>
#define pb push_back
// a INDEXING : 1 BASED
// tree INDEXING : 1 BASED
int a[100000];
int tree[100000] = {0};
void update(int index, int value, int n)
{
while (index <= n)
{
tree[index] += value;
index += (index & (-index));
}
}
int query(int index)
{
int ans = 0;
while (index > 0)
{
ans += tree[index];
index -= (index & (-index));
}
return ans;
}
int32_t main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
while (t--)
{
int i, j, k, n, no_of_query, left, right, m, ans = 0, cnt = 0, sum = 0;
// BUILD AND UPDATE
{
cin >> n;
for (int i = 1; i <= n; ++i)
{
cin >> a[i];
}
int ans = 0;
for (int i = n; i >= 1; i--)
{
// cout << ans << "------>";
ans += query(a[i] - 1);
// cout << ans << endl;
// cout << endl;
update(a[i], 1, n);
}
cout << ans << "---" << endl;
}
int tree[100000] = {0};
// //QUERY
// {
// cin >> no_of_query;
// while (no_of_query--) {
// cin >> left >> right;
// //QUERY(index-to-query)
// cout << query(right) - query(left - 1) << endl;
// }
// }
}
}