#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);
}
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][f].empty()){
//cerr << sum(i) << " sum(i) " << endl << sum(q[x][d].front() - 1) << "sum(qFRONT())" << endl;
ans += (sum(i) - sum(q[x][f].front() - 1) - 1);
//cerr << "ans: " << ans << "tavdapirvelad" << endl;
if(k > 0){
--ans;
//cerr << "ans: " << ans << " sabolood" << endl;
update(q[x][f].front() , -1);
update(i - 1, 1);
q[x][f].pop();
} else {
update(i , -1);
update(q[x][f].front() , 1);
q[x][f].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:42:17: warning: variable 'd' set but not used [-Wunused-but-set-variable]
int f , d;
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
152 ms |
134952 KB |
Output is correct |
2 |
Incorrect |
136 ms |
134880 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
152 ms |
134952 KB |
Output is correct |
2 |
Incorrect |
136 ms |
134880 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
152 ms |
134952 KB |
Output is correct |
2 |
Incorrect |
136 ms |
134880 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
163 ms |
134984 KB |
Output is correct |
2 |
Incorrect |
146 ms |
134968 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
152 ms |
134952 KB |
Output is correct |
2 |
Incorrect |
136 ms |
134880 KB |
Output isn't correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
152 ms |
134952 KB |
Output is correct |
2 |
Incorrect |
136 ms |
134880 KB |
Output isn't correct |