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 pb push_back
typedef long long ll;
const int len = 2e5+5, mx = 2e5;
int vis[len], tree[len], shoe[len];
vector<int> lef[len], rig[len];
void upd(int i, int v){
while (i <= mx)
tree[i] += v, i += i&(-i);
}
int ask(int i){
int ans = 0;
while (i > 0)
ans += tree[i], i -= i&(-i);
return ans;
}
ll count_swaps(vector<int> S) {
ll ans = 0;
int n = S.size();
for (int i = 1; i <= n; i++)
shoe[i] = S[i-1];
for (int i = n; i >= 1; i--){
if (shoe[i] > 0) rig[shoe[i]].pb(i);
else lef[-shoe[i]].pb(i);
upd(i, +1);
}
for (int i = 1; i <= n; i++){
if (vis[i]) continue;
int j = lef[abs(shoe[i])].back();
if (shoe[i] < 0)
j = rig[abs(shoe[i])].back();
ans += ask(j)-2 + (shoe[i] > 0);
vis[i] = vis[j] = 1;
upd(i, -1), upd(j, -1);
lef[abs(shoe[i])].pop_back();
rig[abs(shoe[i])].pop_back();
}
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... |