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>
#define LL long long
#define F first
#define S second
using namespace std;
const int N = 1e6+10;
vector<pair<LL,LL>>vec;
bool matched[N];
LL tree[N][2];
void update(int v,int tl,int tr,int pos ,int val,int t){
if(tl==tr){
tree[v][t] += val;
return ;
}
int mid = (tl+tr)/2;
if(pos<=mid)
update(v*2,tl,mid,pos,val,t);
else
update(v*2+1,mid+1,tr,pos,val,t);
tree[v][t] = tree[v*2][t]+tree[v*2+1][t];
}
LL get(int v,int tl,int tr,int l,int r,int t){
if(tl>r||tr<l)
return 0;
if(tl>=l&&tr<=r)
return tree[v][t];
int mid = (tl+tr)/2;
return get(v*2,tl,mid,l,r,t)+get(v*2+1,mid+1,tr,l,r,t);
}
long long count_swaps(std::vector<int> s) {
LL n = s.size()/2,ans = 0;
int cnt = 0;
for(int i=0;i<n*2;i++){
if(s[i]<0){
bool q = 0;
for(int j=0;j<i;j++)
if(s[j]==abs(s[i])&&!matched[j]){
vec.push_back({j,i});ans += abs(i-j);
q = 1;
matched[j] = 1;
break;
}
if(q)continue;
for(int j=i+1;j<n*2;j++)
if(s[j]==abs(s[i])&&!matched[j]){
vec.push_back({i,j});ans += abs(i-j)-1;
matched[j] = 1;
break;
}
}
}
for(auto x:vec){
update(1,1,n*2,x.F,1,0);
update(1,1,n*2,x.S,1,1);
}
sort(vec.begin(),vec.end());
for(int i=0;i<vec.size();i++){
update(1,1,n*2,vec[i].F,-1,0);
update(1,1,n*2,vec[i].S,-1,1);
ans -= get(1,1,n*2,vec[i].S,n*2,1);
ans += get(1,1,n*2,vec[i].S,n*2,0);
}
return ans;
}
Compilation message (stderr)
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:76:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
76 | for(int i=0;i<vec.size();i++){
| ~^~~~~~~~~~~
shoes.cpp:47:6: warning: unused variable 'cnt' [-Wunused-variable]
47 | int cnt = 0;
| ^~~
# | 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... |