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;
int segment[262144];
void update(int a, int b) {
a += 131072; b += 131072;
while (a <= b) {
if (a % 2 == 1) {segment[a++]++;}
if (b % 2 == 0) {segment[b--]++;}
a /= 2; b /= 2;
}
}
int give(int a) {
int total = a;
a += 131072;
while (a > 0) {
total += segment[a];
a /= 2;
}
return total;
}
long long count_swaps(vector<int> s) {
for (int i=0;i<262144;i++) {
segment[i] = 0;
}
int n = s.size(), shift = *max_element(s.begin(), s.end()), ans = 0;
vector<deque<int>> positions(2 * shift + 1);
for (int i=0;i<n;i++) {
if (positions[shift - s[i]].empty()) {
positions[shift + s[i]].push_back(i);
} else {
ans += i - give(positions[shift - s[i]][0]);
update(positions[shift - s[i]][0], i);
if (s[i] > 0) {
ans--;
}
positions[shift - s[i]].pop_front();
}
}
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... |