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;
//#define int long long
#define ll long long
#define fi first
#define se second
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define sz(a) (long long) a.size()
#define endl '\n'
#define dbg(a) cerr << #a << " = " << a << endl;
#define print(a) for (auto x : a) cerr << x << " "; cerr << endl;
const long long INF = 1e18, MOD = 1e9+7;
struct segTree {
int size = 1;
vector<int> t;
void init(int n) {
while (size < n) size *= 2;
t.assign(2 * size, 0LL);
}
int merge(int a, int b) {
return a + b;
}
void set(int i, int x, int lx, int rx, int k) {
if (rx - lx == 1) {
t[x] += k;
return;
}
int m = (lx + rx) / 2;
if (i < m) {
set(i, 2 * x + 1, lx, m, k);
}
else {
set(i, 2 * x + 2, m, rx, k);
}
t[x] = merge(t[2 * x + 1], t[2 * x + 2]);
}
void set(int i, int k) {
set(i, 0, 0, size, k);
}
int calc(int l, int r, int x, int lx, int rx) {
if (lx >= l && rx <= r) {
return t[x];
}
if (lx >= r || rx <= l) {
return 0;
}
int m = (lx + rx) / 2;
auto m0 = calc(l, r, 2 * x + 1, lx, m);
auto m1 = calc(l, r, 2 * x + 2, m, rx);
return merge(m0, m1);
}
int calc(int l, int r) {
return calc(l, r, 0, 0, size);
}
};
ll count_swaps(vector<int> tmp) {
int n = sz(tmp);
vector pos(n + 1, vector<int>()), neg(n + 1, vector<int>());
for (int i = 0; i < n; i++) {
if (tmp[i] > 0) {
pos[tmp[i]].pb(i);
}
else {
neg[abs(tmp[i])].pb(i);
}
}
// ara formo parelles
vector<int> other;
vector<int> a(n), add;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < sz(pos[i]); j++) {
a[pos[i][j]] = a[neg[i][j]] = sz(other);
other.emplace_back(max(pos[i][j], neg[i][j]));
add.emplace_back(pos[i][j] < neg[i][j]);
}
}
segTree st;
st.init(n);
ll ans = 0;
for (int i = 0; i < n; i++) {
if (other[a[i]] == i) {
continue;
}
ans += other[a[i]] - i - 1 + add[a[i]];
ans -= st.calc(i, other[a[i]]);
st.set(other[a[i]], 1);
}
return ans;
}
//signed main() {
//cout << count_swaps({2, 1, -1, -2}) << endl;
//}
# | 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... |