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>
#include <iostream>
using namespace std;
#define int int64_t
vector<int>seg;
void update(int k, int v, int pos, int l, int r){
if(l == r){
seg[pos] = v;
return;
}
else if(k <= (l+r)/2) update(k, v, pos*2, l, (r+l)/2);
else update(k, v, pos*2+1, (l+r)/2+1, r);
seg[pos] = seg[pos*2] + seg[pos*2+1];
}
int query(int ql, int qr, int pos, int l, int r){
if(l >= ql && r <= qr) return seg[pos];
else if(l > qr || r < ql) return 0;
else return query(ql, qr, pos*2, l, (l+r)/2) + query(ql, qr, pos*2+1, (l+r)/2+1, r);
}
long long count_swaps(std::vector<int32_t>v){
int n = v.size();
map<int, queue<int>>mp;
vector<int>shoes(n);
if(v[0] < 0){
int cur = 1;
for(int i = 0; i<n; ++i){
if(v[i] < 0){
mp[v[i]].push(cur);
shoes[i] = cur;
cur += 2;
}
}
for(int i = 0; i<n; ++i){
if(v[i] > 0){
shoes[i] = mp[-v[i]].front()+1;
mp[-v[i]].pop();
}
}
}
else{
int cur = 2;
for(int i = 0; i<n; ++i){
if(v[i] > 0){
mp[v[i]].push(cur);
shoes[i] = cur;
cur += 2;
}
}
for(int i = 0; i<n; ++i){
if(v[i] < 0){
shoes[i] = mp[-v[i]].front()-1;
mp[-v[i]].pop();
}
}
}
//for(auto u: shoes) cout<<u<<" ";
//cout<<endl;
vector<vector<int>>fred(n);
seg.assign(n*4, 0);
for(int i = 0; i<n; ++i){
fred[i] = {shoes[i], i};
}
sort(fred.begin(), fred.end());
reverse(fred.begin(), fred.end());
int ans = 0;
for(int i = 0; i<n; ++i){
ans += query(0, fred[i][1]-1, 1, 0, n-1);
update(fred[i][1], 1, 1, 0, n-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... |