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 <bits/stdc++.h>
#include "shoes.h"
using namespace std;
#define pi pair<int, int>
#define pii pair<int, pi>
#define fi first
#define se second
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
set<int> L[200005], R[200005];
long long vis[200005], ft[200005], n;
void upd(int l, int r, int v){
r++;r++;
l++;
for(;l<=n;l+=(l & -l))ft[l] += v;
for(;r<=n;r+=(r & -r))ft[r] -= v;
}
long long query(int p){
p++;
long long sum = 0;
for(;p;p-=(p & -p))sum += ft[p];
return sum;
}
long long count_swaps(std::vector<int> s) {
n = (int)s.size();
for(int i=0;i<n;i++){
if(s[i] < 0)L[-s[i]].insert(i);
else R[s[i]].insert(i);
}
for(int i=0;i<n;i++)upd(i, i, i);
long long ans = 0;
int cnt = 0;
for(int i=0;i<n;i++){
if(vis[i])continue;
if(s[i] < 0){
L[-s[i]].erase(L[-s[i]].begin());
ans += max(0ll, query(*R[-s[i]].begin()) - query(i) - 1);
vis[*R[-s[i]].begin()] = 1;
upd(i+1, *R[-s[i]].begin(), 1);
R[-s[i]].erase(R[-s[i]].begin());
}
else{
ans++;
R[s[i]].erase(R[s[i]].begin());
ans += max(0ll, query(*L[s[i]].begin()) - query(i) - 1);
vis[*L[s[i]].begin()] = 1;
upd(i+1, *L[s[i]].begin(), 1);
L[s[i]].erase(L[s[i]].begin());
}
}
return ans;
}
Compilation message (stderr)
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:38:6: warning: unused variable 'cnt' [-Wunused-variable]
38 | int cnt = 0;
| ^~~
# | 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... |