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;
long long tree[800005];
void update(int k, int left, int right, int poz) {
if (left==right) {
if (left==poz)
tree[k]++;
return;
}
if (left>right||left>poz||right<poz)
return;
int mid=(left+right)/2;
update(2*k, left, mid, poz);
update(2*k+1, mid+1, right, poz);
tree[k]=tree[2*k]+tree[2*k+1];
}
long long query(int k, int left, int right, int l, int r) {
if (left>right||left>r||right<l)
return 0;
if (l<=left&&right<=r)
return tree[k];
int mid=(left+right)/2;
long long r1=query(2*k, left, mid, l, r);
long long r2=query(2*k+1, mid+1, right, l, r);
return r1+r2;
}
long long count_swaps(vector<int> a) {
int n=a.size()/2;
queue<int> q[2*n+5];
long long rez=0;
for (int i=0;i<2*n;i++) {
int golemina=a[i], obr;
if (golemina<0) golemina=n-a[i], obr=-a[i];
else obr=n+a[i];
if (q[obr].empty()) {
q[golemina].push(i);
update(1, 0, 2*n-1, i);
}
else {
rez+=query(1, 0, 2*n-1, q[obr].front()+1, i)+(a[i]<0);
update(1, 0, 2*n-1, q[obr].front());
q[obr].pop();
}
}
return rez;
}
# | 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... |