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;
template<class T, T m_(T, T)> struct IterativeSegmentTree{
int n; vector<T> ST;
IterativeSegmentTree(){}
IterativeSegmentTree(vector<T> &a){
n = a.size(); ST.resize(n << 1);
for (int i=n;i<(n<<1);i++)ST[i]=a[i-n];
for (int i=n-1;i>0;i--)ST[i]=m_(ST[i<<1],ST[i<<1|1]);
}
void update(int pos, T val){ // replace with val
ST[pos += n] = val;
for (pos >>= 1; pos > 0; pos >>= 1)
ST[pos] = m_(ST[pos<<1], ST[pos<<1|1]);
}
T query(int l, int r){ // [l, r]
T ansL, ansR; bool hasL = 0, hasR = 0;
for (l += n, r += n + 1; l < r; l >>= 1, r >>= 1) {
if (l & 1)
ansL=(hasL?m_(ansL,ST[l++]):ST[l++]),hasL=1;
if (r & 1)
ansR=(hasR?m_(ST[--r],ansR):ST[--r]),hasR=1;
}
if (!hasL) return ansR; if (!hasR) return ansL;
return m_(ansL, ansR);
}
};
int merge(int a, int b){
return a+b;
}
long long count_swaps(vector<int> v) {
int n=v.size();
vector<int>vacio(n,0);
IterativeSegmentTree<int,merge>st(vacio);
long long cnt=0;
map<int, pair<int,int>>pos;
int position=0;
for(int zapato:v){
if(zapato<0){
pos[zapato].first=position;
}
else{
pos[zapato].second=position;
}
position++;
}
for(auto &nose:pos){
int der=st.query(nose.second.second,n-1);
int izq=st.query(0,nose.second.first);
bool menor=(nose.second.first<nose.second.second);
cnt+=(der-izq-menor);
st.update(der,1);
st.update(izq,1);
}
return cnt;
}
Compilation message (stderr)
shoes.cpp: In member function 'T IterativeSegmentTree<T, m_>::query(int, int)':
shoes.cpp:24:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
24 | if (!hasL) return ansR; if (!hasR) return ansL;
| ^~
shoes.cpp:24:29: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
24 | if (!hasL) return ansR; if (!hasR) return ansL;
| ^~
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:12:12: warning: 'ansR' may be used uninitialized in this function [-Wmaybe-uninitialized]
12 | ST[pos += n] = val;
| ~~~~^~~~
shoes.cpp:17:13: note: 'ansR' was declared here
17 | T ansL, ansR; bool hasL = 0, hasR = 0;
| ^~~~
shoes.cpp:12:12: warning: 'ansR' may be used uninitialized in this function [-Wmaybe-uninitialized]
12 | ST[pos += n] = val;
| ~~~~^~~~
shoes.cpp:17:13: note: 'ansR' was declared here
17 | T ansL, ansR; bool hasL = 0, hasR = 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... |