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>
#define MAXN 400020
#define DELTA 100001
#define pb push_back
#define ll long long
#define fi first
#define se second
#define mp make_pair
using namespace std;
int n, t[4*MAXN], par[MAXN], tleaf[MAXN];
queue<int> posOf[MAXN];
void build(int v, int tl, int tr)
{
if (tl == tr)
t[v] = 1;
else
{
int tm = (tl + tr) / 2;
build(v*2, tl, tm);
build(v*2+1, tm+1, tr);
t[v] = t[v*2] + t[v*2+1];
}
}
int sum(int v, int tl, int tr, int l, int r)
{
if (l > r)
return 0;
if (l == tl && r == tr)
return t[v];
int tm = (tl + tr) / 2;
return sum(v*2, tl, tm, l, min(r, tm)) + sum(v*2+1, tm+1, tr, max(l, tm+1), r);
}
void update(int v, int tl, int tr, int pos)
{
if (tl == tr)
t[v] = 0;
else
{
int tm = (tl + tr) / 2;
if (pos <= tm)
update(v*2, tl, tm, pos);
else
update(v*2+1, tm+1, tr, pos);
t[v] = t[v*2] + t[v*2+1];
}
}
long long count_swaps(std::vector<int> s)
{
n = s.size();
for (int i = n-1; i >= 0; i--)
{
tleaf[i] = 1;
int other = DELTA - s[i];
if (!posOf[other].empty())
{
par[i] = posOf[other].front();
par[par[i]] = i;
tleaf[par[i]] = 0;
posOf[other].pop();
}
else
{
par[i] = -1;
posOf[s[i]+DELTA].push(i);
}
}
build(1, 0, n-1);
long long res = 0;
for (int i = 0; i < n; i++)
{
if (tleaf[i] == 1)
{
res += sum(1, 0, n-1, i+1, par[i]-1);
//tleaf[par[i]] = 0;
update(1, 0, n-1, par[i]);
if (s[i] > 0) res++;
}
}
return res;
}
# | 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... |