Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int N =1e5 + 5;
- int inv(int pos, int BIT[]){
- int count = 0;
- while(pos > 0){
- count += BIT[pos];
- pos -= (pos & (-pos));
- }
- return count;
- }
- void upd(int pos, int BIT[], int n){
- while(pos <= n ){
- BIT[pos]++;
- pos += (pos & (-pos));
- }
- }
- int main(){
- int t;
- scanf("%d", &t);
- while(t--){
- int n, i;long long int ans = 0;
- scanf("%d", &n);
- int BIT[N] = {0};
- vector < int > arr(n), v(n);
- for(i = 0;i < n; i++)
- scanf("%d", &arr[i]), v[i] = arr[i];
- sort(v.begin(), v.end());
- for(i = 0; i < n; i++)
- arr[i] = lower_bound(v.begin(), v.end(), arr[i]) - v.begin() + 1;
- for(i = n-1; i >= 0; i--)
- ans += inv(arr[i]-1, BIT), upd(arr[i], BIT, n);
- cout << ans << '\n';
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment