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 "shoes.h"
#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
typedef long long ll;
const int N = 200005;
int n;
int a[N];
int t[N * 4];
void ubd(int tl, int tr, int x, int y, int pos)
{
if (tl == tr)
{
t[pos] = y;
return;
}
int m = (tl + tr) / 2;
if (x <= m)
ubd(tl, m, x, y, pos * 2);
else
ubd(m + 1, tr, x, y, pos * 2 + 1);
t[pos] = t[pos * 2] + t[pos * 2 + 1];
}
int qry(int tl, int tr, int l, int r, int pos)
{
if (l > r)
return 0;
if (tl == l && tr == r)
{
return t[pos];
}
int m = (tl + tr) / 2;
return (qry(tl, m, l, min(m, r), pos * 2) +
qry(m + 1, tr, max(m + 1, l), r, pos * 2 + 1));
}
deque<int> l[N], r[N];
long long count_swaps(std::vector<int> s)
{
n = sz(s) / 2;
for (int i = 1; i <= n * 2; ++i)
a[i] = s[i - 1];
for (int i = 1; i <= n * 2; ++i)
{
ubd(1, n * 2, i, 1, 1);
if (a[i] < 0)
l[-a[i]].push_back(i);
else
r[a[i]].push_back(i);
}
ll ans = 0;
for (int i = 1; i <= n * 2; ++i)
{
if (qry(1, n * 2, i, i, 1) == 0)
continue;
ans += qry(1, n * 2, min(l[abs(a[i])].front(), r[abs(a[i])].front()),
max(l[abs(a[i])].front(), r[abs(a[i])].front()), 1) - 2;
if (a[i] > 0)
++ans;
ubd(1, n * 2, l[abs(a[i])].front(), 0, 1);
ubd(1, n * 2, r[abs(a[i])].front(), 0, 1);
l[abs(a[i])].pop_front();
r[abs(a[i])].pop_front();
}
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... |