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;
typedef long long ll;
int v1[200200];
vector<int> pos[100100];
struct Seg{
int tree[200200], sz;
void update(int p, int x){while(p<=sz){tree[p]+=x; p+=p&-p;}}
int query(int r){
int ret = 0;
while(r){
ret += tree[r];
r ^= r&-r;
}
return ret;
}
}tree;
long long count_swaps(std::vector<int> s) {
int n = s.size()/2;
for (int i=n*2-1;i>=0;i--) if (s[i]<0) pos[-s[i]].push_back(i);
ll ret = 0;
for (int i=0;i<n*2;i++) if (s[i]>0){
if (i<pos[s[i]].back()){
ret += pos[s[i]].back()-i+1;
v1[pos[s[i]].back()] = i+1;
}
else{
ret += i-pos[s[i]].back()-1;
v1[i] = pos[s[i]].back()+1;
}
pos[s[i]].pop_back();
}
tree.sz=n*2;
for (int i=0;i<n*2;i++){
if (v1[i]){
tree.update(v1[i], -1);
ret += tree.query(v1[i])*2;
}
else tree.update(i+1, 1);
}
return ret>>1;
}
# | 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... |