This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Bismillahir-Rahmanir-Rahim
# include <bits/stdc++.h>
# define pb push_back
# define ff first
# define ss second
# define nl "\n"
# define sz(x) ((int)(x).size())
# define all(x) (x).begin(), (x).end()
# define deb(x) cerr << #x << " = " << x << endl;
# define pll pair <ll, ll>
# define pii pair <int, int>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const ll maxn = 1e5 + 7;
const ll inf = 2e18 + 0;
const ll mod = 1e9 + 7;
const ll dx[] = {-1, 1, 0, 0};
const ll dy[] = {0, 0, -1, 1};
using namespace std;
pll p[maxn];
ll ans, N;
vector <ll> qwe[2][maxn];
struct segtree {
ll t[maxn * 8];
void update (ll pos, ll x, ll v = 1, ll tl = 0, ll tr = N + N - 1) {
if (tl == tr) {
t[v] += x;
return;
}
ll tm = (tl + tr) >> 1;
if (pos <= tm) {
update(pos, x, v * 2, tl, tm);
}
else {
update(pos, x, v * 2 + 1, tm + 1, tr);
}
t[v] = t[v * 2] + t[v * 2 + 1];
}
ll get (ll l, ll r, ll v = 1, ll tl = 0, ll tr = N + N - 1) {
if (tr < l || r < tl) return 0;
if (l <= tl && tr <= r) return t[v];
ll tm = (tl + tr) >> 1;
return get(l, r, v * 2, tl, tm) + get(l, r, v * 2 + 1, tm + 1, tr);
}
} st;
void upd (ll x, ll id) {
if (x < 0) {
p[-x].ff = id;
}
else {
p[x].ss = id;
}
}
long long count_swaps(vector<int> S){
ll n = sz(S) / 2; N = n;
set <ll> zxc;
for (ll i = 0; i < n + n; ++i) {
zxc.insert(abs(S[i]));
if (S[i] < 0) {
qwe[0][-S[i]].pb(i);
}
else {
qwe[1][S[i]].pb(i);
}
}
ll sz = 0;
for (ll x : zxc) {
for (ll i = 0; i < sz(qwe[0][x]); ++i) {
++sz;
ll pos1 = qwe[0][x][i], pos2 = qwe[1][x][i];
S[pos1] = -sz, S[pos2] = sz;
p[sz] = {-1, -1};
upd(S[pos1], pos1);
upd(S[pos2], pos2);
}
}
for (ll i = 0; i < n + n; ++i) {
if (st.get(i, i)) continue;
ll x = abs(S[i]);
if (S[i] > 0) ans++;
if (p[x].ff > p[x].ss) swap(p[x].ff, p[x].ss);
ans += p[x].ss - p[x].ff - 1 - st.get(p[x].ff + 1, p[x].ss - 1);
st.update(p[x].ff, 1); st.update(p[x].ss, 1);
}
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... |