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 "shoes.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define sz(x) ((int)(x).size())
vector<int> fen;
void add(int i, int x){
for(i++; i < sz(fen); i+=i&-i) fen[i] += x;
}
int qry(int i){
int ans = 0;
for(i++; i > 0; i-=i&-i) ans += fen[i];
return ans;
}
ll count_swaps(vector<int> s) {
ll ans = 0;
fen.resize(sz(s)+1);
vector<queue<int>> vl(sz(s)/2+1), vr(sz(s)/2+1);
vector<pair<int, int>> v;
for(int i = 0; i < sz(s); i++){
if(s[i] > 0){
if(vl[s[i]].empty()) vr[s[i]].push(i);
else{
v.emplace_back(vl[s[i]].front(), i);
vl[s[i]].pop();
}
}
else{
if(vr[-s[i]].empty()) vl[-s[i]].push(i);
else{
ans++;
v.emplace_back(vr[-s[i]].front(), i);
vr[-s[i]].pop();
}
}
}
sort(v.begin(), v.end());
for(auto [i, j] : v){
int L = i+qry(i);
int R = j+qry(j);
ans += R-L-1;
add(i, 1);
add(j, -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... |