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 mxN = 2e5;
long long T[mxN+5];
int n;
void add(int i, int x){
i++;
while(i <= mxN){
T[i] += x;
i += i & -i;
}
}
long long query(int i){
i++;
long long sum = 0;
while(i > 0){
sum += T[i];
i -= i & -i;
}
return sum;
}
long long count_swaps(vector<int> s) {
n = int(s.size());
vector<queue<int>> q(mxN+5);
long long ans = 0;
for(int i=0; i<n; ++i){
int aux = 1e5-s[i] ;
if(!q[aux].empty()){
int l = q[aux].front();
q[aux].pop();
ans = ans + (i-l) - (s[i] > 0) - query(i) + query(l);
add(l, -1);
} else {
q[s[i]+1e5].push(i);
add(i, 1);
}
}
return ans;
}
# | 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... |