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 ll long long
#define MAXN 200005
int ft[MAXN];
int sum(int r){
int an=0;
for(int i=r; i>=0; i=(i&(i+1))-1){
an += ft[i];
}
return an;
}
int sum(int l, int r){
if(l!=0) return sum(r)-sum(l-1);
else return sum(r);
}
void add(int r, int x){
for(int i=r; i<MAXN; i|=(i+1)){
ft[i] += x;
}
}
long long count_swaps(std::vector<int> s) {
int n=s.size()/2;
int clo[2*n];
int have[3][n+5];
memset(have,-1,sizeof(have));
memset(clo,-1,sizeof(clo));
for(int i=0; i<2*n; i++){
int x = s[i], y=abs(s[i]);
if(x<0){
if(have[2][y]!=-1){
clo[have[2][y]] = i;
have[2][y]=-1;
}
else have[1][y]=i;
}
if(x>0){
if(have[1][y]!=-1){
clo[have[1][y]] = i;
have[1][y]=-1;
}
else have[2][y]=i;
}
}
memset(ft,0,sizeof(ft));
bool taken[2*n];
memset(taken,0,sizeof(taken));
ll an=0;
for(int i=0; i<2*n; i++){
if(taken[i]) continue;
int b = sum(i,clo[i]);
b += (clo[i]-i-1);
if(s[i]>0) b++;
an+=b;
add(clo[i],-1);
taken[clo[i]]=1;
}
return an;
}
# | 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... |