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;
const int maxx = 200001;
int fen[maxx];
void update(int idx) {
idx++;
while (idx < maxx)fen[idx]++ , idx += (idx & -idx);
}
int get(int idx) {
int ret = 0;
while (idx > 0) {
ret += fen[idx];
idx -= (idx & -idx);
}
return ret;
}
int query(int l , int r) {
l++; r++;
if (l > r)return 0;
return get(r) - get(l - 1);
}
map< int , queue<int> >mp;
long long count_swaps(vector<int> s) {
long long ret = 0;
int n = s.size();
for (int i = 0;i<n;i++)mp[s[i]].push(i);
for (int i = 0;i<n;i++) {
if (mp[s[i]].front() == i) {
int nxt = mp[-s[i]].front();
mp[s[i]].pop();
mp[-s[i]].pop();
ret += (nxt - i - 1) - query(i + 1 , nxt - 1);
update(nxt);
update(i);
if (s[i] >= 0)ret++;
}
}
return ret;
}
# | 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... |