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 <bits/stdc++.h>
using namespace std;
const int MN=1e5+2;
typedef long long ll;
typedef pair<int,int> pii;
struct node{int l,r,sum;} seg[4*MN];
void bld(int l,int r,int i) {
seg[i].l=l;
seg[i].r=r;
if(l==r) return;
int mid=(l+r)/2;
bld(l,mid,i*2);
bld(mid+1,r,i*2+1);
seg[i].sum=0;
}
int qry(int l,int r,int i) {
if(l==seg[i].l&&r==seg[i].r) return seg[i].sum;
int mid=(seg[i].l+seg[i].r)/2;
if(r<=mid) return qry(l,r,i*2);
else if(l>mid) return qry(l,r,i*2+1);
else return qry(l,mid,i*2)+qry(mid+1,r,i*2+1);
}
void upd(int p,int v,int i) {
if(seg[i].l==seg[i].r) {
seg[i].sum=v;
return;
}
int mid=(seg[i].l+seg[i].r)/2;
if(p<=mid) upd(p,v,i*2);
else upd(p,v,i*2+1);
seg[i].sum=seg[i*2].sum+seg[i*2+1].sum;
}
ll count_swaps(vector<int> S) {
ll N=S.size()/2,ans=0,sub=0;
bld(0,N*2-1,1);
vector<vector<int>> pos(2*N+1);
for(int i=0;i<2*N;++i) {
pos[S[i]+N].push_back(i);
if(S[i]<0) upd(i,-1,1);
else upd(i,1,1);
}
vector<pii> in;
for(int i=0;i<N;++i)
for(int j=0;j<pos[i].size();++j) {
in.push_back(pii(min(pos[i][j],pos[N+N-i][j]),max(pos[i][j],pos[N+N-i][j])));
ans+=(pos[N+N-i][j]<pos[i][j]);
}
for(pii p:in) {
if(p.second==p.first+1) continue;
ans+=p.second-p.first-1;
sub+=abs(qry(p.first+1,p.second-1,1));
}
return ans-sub/2;
}
Compilation message (stderr)
shoes.cpp: In function 'll count_swaps(std::vector<int>)':
shoes.cpp:49:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | for(int j=0;j<pos[i].size();++j) {
| ~^~~~~~~~~~~~~~
# | 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... |