#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include "shoes.h"
using namespace std;
#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()
#define ll long long
#define ld long double
#define ui uint64_t
#define cont(set, element) ((set).find(element) != (set).end())
/********* DEBUG *********/
template <typename T>
void outvec(const vector<T>& Z){
for (const T& x : Z)
cout << x << ' ';
cout << "\n";
}
void printVariable(const any& var) {
if (!var.has_value()) {
cout << "null";
return;
}
if (var.type() == typeid(int)) {
cout << any_cast<int>(var);
} else if (var.type() == typeid(double)) {
cout << any_cast<double>(var);
} else if (var.type() == typeid(float)) {
cout << any_cast<float>(var);
} else if (var.type() == typeid(char)) {
cout << any_cast<char>(var);
} else if (var.type() == typeid(bool)) {
cout << (any_cast<bool>(var) ? "true" : "false");
} else if (var.type() == typeid(string)) {
cout << any_cast<string>(var);
} else if (var.type() == typeid(const char*)) {
cout << any_cast<const char*>(var);
} else if (var.type() == typeid(long long)) {
cout << any_cast<long long>(var);
} else {
cout << "[unknown type]";
}
}
template<typename... Args>
void outval(Args... args) {
vector<any> variables = {args...};
for (size_t i = 0; i < variables.size(); ++i) {
printVariable(variables[i]);
if (i != variables.size() - 1) {
cout << " ";
}
}
cout << "\n";
}
#define nl "\n"
#define sp << " " <<
/********* DEBUG *********/
const ll MOD = 1000000007;
const ll inf = 1e18;
const ll mxN = 100005;
vector<int> tree, lazy;
void push(int nd, int start, int end){
if (lazy[nd] != 0){
tree[nd] += lazy[nd];
if (start != end){
lazy[2 * nd + 1] += lazy[nd];
lazy[2 * nd + 2] += lazy[nd];
}
}
lazy[nd] = 0;
}
void upd(int nd, int l, int r, int val, int start, int end){
if (end < l || start > r)
return;
if (l <= start && end <= r){
lazy[nd] += val;
push(nd, start, end);
return;
}
int m = (start+end)/2;
upd(2 * nd + 1, l, r, val, start, m);
upd(2 * nd + 2, l, r, val, m+1, end);
}
ll query(ll idx, ll nd, ll start, ll end){
push(nd, start, end);
if (start == end)
return tree[nd];
ll m = (start + end) / 2;
if (idx <= m)
return query(idx, 2 * nd + 1, start, m);
else
return query(idx, 2 * nd + 2, m + 1, end);
}
long long count_swaps(vector<int> s) {
ll ans = 0, need = 0, n = s.size();
tree.resize(n*4);
lazy.resize(n*4);
vector<vector<ll>> l(n/2+1), r(n/2+1);
vector<ll> ptrs(n/2+1, 0);
vector<bool> dn(n);
for (int i = 0; i < n; i++){
if (s[i] < 0){
l[abs(s[i])].push_back(i);
}
else{
r[s[i]].push_back(i);
}
}
for (int i = 0; i < n; i++){
if (dn[i])
continue;
ll ptr = ptrs[abs(s[i])];
ptrs[abs(s[i])]++;
if (s[i] < 0){
s[i] *= -1;
ll idl = l[s[i]][ptr], idr = r[s[i]][ptr];
dn[idr] = true;
ll xl = query(idl, 0, 0, n-1);
ll xr = query(idr, 0, 0, n-1);
upd(0, idl, idr, 1, 0, n-1);
idl += xl;
idl += xr;
ans += idr-idl-1;
}
else{
ll idl = l[s[i]][ptr], idr = r[s[i]][ptr];
dn[idl] = true;
ll xl = query(idl, 0, 0, n-1);
ll xr = query(idr, 0, 0, n-1);
upd(0, idr, idl, 1, 0, n-1);
idl += xl;
idr += xr;
ans += idl-idr;
}
}
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... |