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 = 1e5 + 5;
ll bit[N];
void update(int ind, int val)
{
for(; ind < N; ind = (ind | (ind + 1)))
bit[ind] += val;
}
ll query(int ind)
{
ll res = 0;
for(; ind >= 0; ind = (ind & (ind + 1)) - 1)
res += bit[ind];
return res;
}
long long count_swaps(std::vector<int> s)
{
int n = s.size();
map<int, set<int>> mp;
vector<bool> vec(n);
for(int i = 0; i < n; i++)
{
mp[s[i]].insert(i);
}
for(int i = 0; i < n; i++)
update(i, 1);
ll ans = 0;
for(int i = 0; i < n; i++)
{
if(vec[i])
continue;
ans += (s[i] > 0);
auto it = mp[-s[i]].lower_bound(i);
int j = *it;
mp[-s[i]].erase(it);
vec[i] = vec[j] = true;
update(i, -1);
update(j, -1);
ans += query(j) - query(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... |