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 <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define sort undefined_function // To use stable_sort instead sort 
#define bpc __builtin_popcount
#define ull unsigned long long
#define ld double
#define ll long long
#define mp make_pair
#define F first
#define S second
#pragma GCC optimize("O3")
#ifdef LOCAL 
        #include "debug.h"
#else 
        #define dbg(...) 0
#endif
using namespace __gnu_pbds;
using namespace std;
typedef tree<long long, null_type, less_equal<long long>,
    rb_tree_tag, tree_order_statistics_node_update> Tree;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll INF = 9223372036854775807LL;
const ll inf = 2147483647;
const ll MOD = 998244353; //[998244353, 1e9 + 7, 1e9 + 13]
const ld PI = acos(-1);
//const ll NROOT = 300;
ll binpow(ll a, ll b, ll _MOD = -1) {
        if (_MOD == -1)
                _MOD = MOD;
        ll res = 1;
        for (; b; b /= 2, a *= a, a %= _MOD)
                if (b & 1) res *= a, res %= _MOD;
        return res;
}
void set_IO(string s) {
#ifndef LOCAL
        string in  = s +  ".in";
        string out = s + ".out";
        freopen(in.c_str(),  "r",  stdin);
        freopen(out.c_str(), "w", stdout);
#endif
}
bool dataOverflow(ll a, ll b) {return (log10(a) + log10(b) >= 18);}
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a * b / gcd(a, b);}
ll ceil(ll a, ll b) {return (a + b - 1) / b;}
class FT{
private:
        int n;
        vector<ll> v;
public:
        FT(int _n) {
                n = _n;
                v.resize(n + 10);
        }
        void update(int p, ll x) {
                while (p <= n) {
                        v[p] += x;
                        p += p & -p;
                }
        }
        void update(int l, int r, ll x) {
                update(l, x);
                update(r + 1, -x);
        }
        ll query(int p) {
                ll ans = 0;
                while (0 < p) {
                        ans += v[p];
                        p -= p & -p; 
                }
                return ans;
        }
        ll query(int l, int r) {
                return query(r) - query(l - 1);
        }
};
ll count_swaps(vector<int> S) {
        int n = S.size();
        FT aux(n + 10);
        map<int, stack<int>> p;
        ll res = 0;
        for (int i = n - 1; i >= 0; i --) {
                int x = S[i];
                if (x < 0) {
                        if (p[-x].empty()) {
                                p[x].push(i);
                        } else {
                                int j = p[-x].top(); p[-x].pop();
                                int pi = i + aux.query(i);
                                int pj = j + aux.query(j);
                                res += abs(pj - pi) - 1;
                                aux.update(i + 1, j, -1);
                        }
                } else {
                        if (p[-x].empty()) {
                                p[x].push(i);
                        } else {
                                int j = p[-x].top(); p[-x].pop();
                                int pi = i + aux.query(i);
                                int pj = j + aux.query(j);
                                res += abs(pj - pi);
                                aux.update(i + 1, j, -1);
                        }
                }
        }
        return res;
}
#ifdef LOCAL
int32_t main() {
        ios_base::sync_with_stdio(0);
        cin.tie(0);
        cout << count_swaps({2, 1, -1, -2}) << "\n";
        cout << count_swaps({-2, 2, 2, -2, -2, 2}) << "\n";
        return 0;
}
#endif
// \   ^__^
//  \  (oo)\_______
//     (__)\  -_-  )\/\
//         ||----w |
//         ||     ||
Compilation message (stderr)
shoes.cpp:146:1: warning: multi-line comment [-Wcomment]
  146 | //     (__)\  -_-  )\/\
      | ^
shoes.cpp: In function 'void set_IO(std::string)':
shoes.cpp:47:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   47 |         freopen(in.c_str(),  "r",  stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
shoes.cpp:48:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   48 |         freopen(out.c_str(), "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~| # | 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... |