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;
typedef long long ll;
ll count_swaps(vector<int>s){
int n = s.size();
map<int, vector<int>>p;
vector<int>a(n);
for(int i = 0, index = 1; i < n; i++){
if(!p[-s[i]].empty()){
a[i] = p[-s[i]].back() + (s[i] < 0 ? -1 : 1);
p[-s[i]].pop_back();
}
else{
p[s[i]].emplace_back(a[i] = index + int(s[i] > 0));
index += 2;
}
}
vector<int>bit(n + 1, 0);
auto update = [&] (int p){
for(; p <= n; p += p & -p){
bit[p]++;
}
};
auto get = [&] (int p){
int ans = 0;
for(; p > 0; p -= p & -p){
ans += bit[p];
}
return ans;
};
ll ans = 0;
for(int i = 0; i < n; i++){
ans += i - get(a[i]);
update(a[i]);
}
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... |