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;
typedef long long ll;
const int MN=2e5+2;
ll bit[MN];
void upd(int i,ll v) {
for(;i<MN;i+=-i&i) bit[i]+=v;
}
ll qry(int i) {
ll ret=0;
for(;i>0;i-=-i&i) ret+=bit[i];
return ret;
}
ll count_swaps(vector<int> S) {
int N=S.size()/2;
ll ans=0,sub=0;
vector<int> sti(N*2+2,0),edi(N*2+2,0);
S.insert(S.begin(),0);
vector<vector<int>> pos(2*MN);
for(int i=1;i<=2*N;++i) pos[S[i]+N].push_back(i);
for(int i=0;i<N;++i)
for(int j=0;j<pos[i].size();++j) {
ans+=(pos[N+N-i][j]<pos[i][j]);
int f=min(pos[N+N-i][j],pos[i][j]),s=max(pos[N+N-i][j],pos[i][j]);
sti[s]=f;
edi[f]=s;
}
for(int i=1;i<=2*N;++i) {
if(sti[i]) {
upd(sti[i],-2);
if(sti[i]==i-1) {
upd(sti[i],1);
continue;
}
ans+=i-sti[i]-1;
sub+=qry(i)-qry(sti[i]);
upd(i,1);
} else upd(i,1);
}
return ans-sub/2;
}
Compilation message (stderr)
shoes.cpp: In function 'll count_swaps(std::vector<int>)':
shoes.cpp:25:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | 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... |