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 <iostream>
#include "shoes.h"
#include <map>
using namespace std;
int const NMAX = 2e5;
int freq[1 + NMAX];
map <pair <int, int>, int> pos;
pair <int, int> arr[1 + NMAX];
bool vis[1 + NMAX];
int aib[1 + NMAX];
void update(int pos, int add, int n) {
for(int i = pos;i <= n;i = 2 * i - (i&(i-1))) {
aib[i] += add;
}
}
int query(int pos) {
int ans = 0;
for(int i = pos;i > 0;i = (i&(i-1))) {
ans += aib[i];
}
return ans;
}
long long count_swaps(vector <int> s) {
int n = s.size();
for(int i = 0;i < n;i++) {
freq[s[i]+n]++;
arr[i] = {s[i], freq[s[i]+n]};
pos[arr[i]] = i;
update(i+1, 1, n);
}
long long ans = 0;
for(int i = 0;i < n;i++) {
if(!vis[i]) {
pair <int, int> partner = {-arr[i].first, arr[i].second};
int to = max(pos[partner], i), from = min(pos[partner], i);
int dist = query(to+1) - query(from+1);
if(s[i] < 0) {
dist--;
}
ans += dist;
vis[i] = vis[pos[partner]] = true;
update(i+1, -1, n);
update(pos[partner]+1, -1, n);
}
}
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... |