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];
queue<int>que[N];
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) que[s[i]].push(i);
for(int i=0;i<n*2;i++){
if(s[i]<0){
int j = que[-s[i]].front();
vec.push_back(make_pair(min(i,j),max(i,j)));
ans += abs(i-j)-1+(i>j);
que[-s[i]].pop();
}
}
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:68: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]
68 | for(int i=0;i<vec.size();i++){
| ~^~~~~~~~~~~
shoes.cpp:48:6: warning: unused variable 'cnt' [-Wunused-variable]
48 | 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... |