This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("inline,Ofast,no-stack-protector,unroll-loops,fast-math,O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,popcnt,avx,abm")
#include <bits/stdc++.h>
#include "shoes.h"
using namespace std;
int t[801010];
int tAdd[801010];
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 srt[401010];
bool used[401010];
vector<int> have[401010];
int p[401010];
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] + 200000].push_back(i);
long long ans = 0;
for (int i = 0; i < n; i++) {
if (used[i])continue;
long long val = -s[i] + 200000;
if (!srt[val]) {
srt[val] = true;
sort(have[val].begin(), have[val].end());
}
long long sc = have[val][p[val]++];
p[s[i] + 200000]++;
long long cur = gt(1, 0, n - 1, i);
long long 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... |