Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define fast ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
- #define ll long long
- #define ld long double
- #define pb push_back
- #define fe first
- #define se second
- #define st(x) sort(x.begin(),x.end())
- #define rst(x) sort(x.rbegin(), x.rend())
- #define all(x) x.begin(),x.end()
- long double pi = 3.14159265358979323;
- const double EPS = 1e-12;
- const int N = 2*1e5 + 5;
- const int mod = 1e9 + 7;
- //range update using BIT
- ll bit[N],n,a[N];
- void update(ll idx,ll val)
- {
- while(idx <= n)
- {
- bit[idx] += val;
- idx += idx & (-idx);
- }
- }
- ll query(ll idx)
- {
- ll sum = 0 ;
- while(idx > 0)
- {
- sum += bit[idx];
- idx -= idx & (-idx);
- }
- return sum;
- }
- ll convert()
- {
- ll temp[n];
- for(int i = 0 ; i < n ;i++ ){
- temp[i] = a[i];
- }
- sort(temp , temp + n);
- for(int i = 0 ; i < n ; i++ )
- {
- a[i] = lower_bound(temp , temp + n ,a[i]) - temp + 1;
- }
- for(int i = 0 ; i <= n ; i++ )
- bit[i] = 0 ;
- ll ans = 0 ;
- for(int i = n - 1 ; i >= 0; i-- )
- {
- ans += query(a[i] - 1);
- update(a[i],1);
- }
- return ans;
- }
- int main()
- {
- fast;
- ll t;
- cin >> t ;
- while(t--)
- {
- cin >> n ;
- for(int i = 0 ; i < n ;i++ ){
- cin >> a[i];
- }
- cout << convert() << "\n";
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment