This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "shoes.h"
#include<bits/stdc++.h>
using namespace std ;
#define ll long long
const int N = 2e5 + 2 ;
deque<int>pos[N];
struct BIT {
vector<ll> v;
int sz;
explicit BIT(int n) {
v.resize(n);
sz = n;
}
//gets sum of range [0, i]
ll get(int i) {
ll ret = 0;
for (++i; i; i -= i & -i)
ret += v[i - 1];
return ret;
}
void update(int i, ll x) {
for (++i; i <= sz; i += i & -i)
v[i - 1] += x;
}
ll query(int st, int en) {
return get(en) - get(st - 1);
}
};
int finish[N * 4];
long long count_swaps(vector<int> v) {
int n = v.size() / 2;
for(int i = 0 ; i < n * 2; i ++){
pos[n + v[i]].push_back(i);
}
long long ans = 0 ;
BIT bit (n * 2);
for(int i = 0 ;i < n * 2 ; i ++ ){
if(finish[i])continue ;
int ab = -v[i] , p = pos[ab + n].front();
finish[p]++;
pos[ab + n].pop_front();
pos[v[i] + n].pop_front();
int betwen = bit.query(i , p );
ans += p - i - 1 - betwen + (v[i] > 0 ? 1 : 0);
bit.update(p , 1 );
}
return ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |