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;
struct bit {
vector<int> arr;
bit(int sz) {
arr=vector<int>(sz,0);
}
void upd(int i,int v) {
for(;i<arr.size();i+=-i&i) arr[i]+=v;
}
int qry(int i) {
int ret=0;
for(;i>0;i-=-i&i) ret+=arr[i];
return ret;
}
};
ll count_swaps(vector<int> S) {
ll N=S.size()/2,ans=0,sub=0;
bit bit(N*2+2);
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]) {
bit.upd(sti[i],-2);
if(sti[i]==i-1) {
bit.upd(sti[i],1);
continue;
}
ans+=i-sti[i]-1;
sub+=bit.qry(i)-bit.qry(sti[i]);
bit.upd(i,1);
} else bit.upd(i,1);
}
return ans-sub/2;
}
Compilation message (stderr)
shoes.cpp: In member function 'void bit::upd(int, int)':
shoes.cpp:12:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
12 | for(;i<arr.size();i+=-i&i) arr[i]+=v;
| ~^~~~~~~~~~~
shoes.cpp: In function 'll count_swaps(std::vector<int>)':
shoes.cpp:29:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | 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... |