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;
using ll = long long;
struct ft {
vector<int> v; int n;
ft(int _n): n(_n) {v.resize(n+1,0);}
inline int lsb(int x) {return x&(-x);}
void up(int x, int u) {
for(;x<=n;x+=lsb(x)) v[x]+=u;
}
int qry(int x) {
int ans = 0;
for(;x;x-=lsb(x)) ans += v[x];
return ans;
}
};
long long count_swaps(std::vector<int> s) {
int n = s.size() / 2;
vector<int> v, pos[n+1];
for(int i=0;i<2*n;i++) {
if(s[i]<0) {
v.push_back(i);
v.push_back(0);
} else pos[s[i]].push_back(i);
}
for(int i=1;i<=n;i++) reverse(pos[i].begin(),pos[i].end());
for(int i=0;i<2*n;i+=2) {
v[i+1]=end(pos[-s[v[i]]])[-1];
pos[-s[v[i]]].pop_back();
}
int ans = 0; ft fenwick(2*n);
for(int i=2*n;i--;) {
ans += fenwick.qry(v[i]);
fenwick.up(v[i]+1,1);
}
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... |