#include<bits/stdc++.h>
#include<shoes.h>
using namespace std;
const int NAX = 1e5 + 5;
int N;
// fenwick tree to count range sums (+0 , +1 , +2)
int f[2 * NAX + 1];
void update(int idx , int u){
++idx;
while(idx <= N){
f[idx] += u;
idx += idx & (-idx);
}
}
long long sum(int idx){
long long sum = 0;
++idx;
while(idx){
sum += f[idx];
idx -= idx & (-idx);
}
return sum;
}
// 0 - negative x's , 1 - positive x's
queue<int> q[NAX][2];
long long count_swaps(vector<int> S){
N = S.size();
long long ans = 0;
// update BIT
for(int i = 0; i < N; ++i){
update(i , 1);
}
long long ans = 0;
for(int i = 0; i < N; ++i){
int x = abs(S[i]);
int k = S[i];
int f , d;
if(S[i] > 0){
// opposite
d = 0;
// its
f = 1;
} else {
d = 1;
f = 0;
}
// when queue is empty
if(!q[x][d].empty()){
//cerr << sum(i) << " sum(i) " << endl << sum(q[x][d].front() - 1) << "sum(qFRONT())" << endl;
ans += (sum(i) - sum(q[x][d].front() - 1) - 1);
//cerr << "ans: " << ans << "tavdapirvelad" << endl;
if(k > 0){
--ans;
//cerr << "ans: " << ans << " sabolood" << endl;
update(q[x][d].front() , -1);
update(i - 1, 1);
q[x][d].pop();
} else {
update(i , -1);
update(q[x][d].front() , 1);
q[x][d].pop();
}
} else {
q[x][f].push(i);
}
}
return ans;
}
Compilation message
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:39:15: error: redeclaration of 'long long int ans'
long long ans = 0;
^~~
shoes.cpp:34:15: note: 'long long int ans' previously declared here
long long ans = 0;
^~~