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;
queue<int> q[100001], q2[100001];
bool mark[200001];
int fwt[200001];
void ins(int v, int a){
for(; v <= 200000; v += (v & -v)) fwt[v] += a;
}
int query(int v){
int ret = 0;
for(; v > 0; v -= (v & -v)) ret += fwt[v];
return ret;
}
long long count_swaps(vector<int> S){
int len = S.size();
S.insert(S.begin(), 0);
for(int i = 1; i <= len; i++){
ins(i, 1); ins(i + len, 1);
if(S[i] > 0) q[S[i]].push(i);
else q2[-S[i]].push(i);
}
long long ans = 0;
for(int i = 1; i <= len; i++){
if(mark[i]) continue;
mark[i] = true;
if(S[i] < 0){
q2[-S[i]].pop();
ans += query(q[-S[i]].front() - 1) - query(i);
ins(q[-S[i]].front(), -1);
mark[q[-S[i]].front()] = true;
q[-S[i]].pop();
}
else{
q[S[i]].pop();
ans += query(q2[S[i]].front() - 1) - query(i) + 1;
ins(q2[S[i]].front(), -1);
mark[q2[S[i]].front()] = true;
q2[S[i]].pop();
}
}
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... |