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 "shoes.h"
using namespace std;
int t[401010];
int tAdd[401010];
void build(int v, int vl, int vr) {
if (vl == vr) {
t[v] = vl;
return;
}
int vm = (vl + vr) / 2;
build(2 * v, vl, vm);
build(2 * v + 1, vm + 1, vr);
}
void push(int v, int vl, int vr) {
if (tAdd[v] == 0)return;
t[v] += tAdd[v];
if (vl != vr) {
tAdd[2 * v] += tAdd[v];
tAdd[2 * v + 1] += tAdd[v];
}
tAdd[v] = 0;
return;
}
void upd(int v, int vl, int vr, int l, int r, int val) {
push(v, vl, vr);
if (l <= vl && vr <= r) {
tAdd[v] += val;
push(v, vl, vr);
return;
}
if (r < vl || vr < l)return;
int vm = (vl + vr) / 2;
upd(2 * v, vl, vm, l, r, val);
upd(2 * v + 1, vm + 1, vr, l, r, val);
}
int gt(int v, int vl, int vr, int pos) {
push(v, vl, vr);
if (vl == vr)
return t[v];
int vm = (vl + vr) / 2;
if (pos <= vm)
return gt(2 * v, vl, vm, pos);
return gt(2 * v + 1, vm + 1, vr, pos);
}
bool used[201010];
vector<int> have[201010];
int p[201010];
long long count_swaps(vector<int> s) {
int n = s.size();
build(1, 0, n - 1);
for (int i = 0; i < n; i++)
have[s[i] + 100000].push_back(i);
for (int i = 0; i < n; i++)
sort(have[s[i] + 100000].begin(), have[s[i] + 100000].end());
int ans = 0;
for (int i = 0; i < n; i++) {
if (used[i])continue;
int val = -s[i] + 100000;
int sc = have[val][p[val]++];
p[s[i] + 100000]++;
int cur = gt(1, 0, n - 1, i);
int id = gt(1, 0, n - 1, sc);
// cout << i << ", " << cur << " << " << sc << ", " << id << endl;
used[sc] = true;
ans += (id - cur);
if (s[i] < 0)
ans--;
upd(1, 0, n - 1, i, sc, 1);
}
return ans;
}
/**
2
2 1 -1 -2
3
-2 2 2 -2 -2 2
*/
# | 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... |