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;
vector<int> pos[100100];
pair<int, int> a[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;
inline int myabs(int x){
if (x<0) return -x;
return x;
}
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;
vector<int> v1(s.size());
int cnt = 1;
for (int i=0;i<n*2;i++) if (s[i]>0){
if (i<pos[s[i]].back()) ret++;
else ret--;
ret += myabs(i-pos[s[i]].back());
a[cnt] = {i, pos[s[i]].back()};
if (a[cnt].first>a[cnt].second) swap(a[cnt].first, a[cnt].second);
v1[a[cnt].second] = a[cnt].first+1;
pos[s[i]].pop_back();
cnt++;
}
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/2;
}
# | 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... |