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;
using i32 = int;
#define int long long
#define len(x) (int)(x.size())
#define inf 1000'000'000'000'000'000LL
#define all(x) x.begin(), x.end()
#define low_bit(x) (x & (-x))
template<typename T>
using vec = vector<T>;
template<typename T>
struct fenvik {
private:
vec<T> fen;
T _get(int x) {
T res = 0;
for (int i = x; i > 0; i -= low_bit(i)) {
res += fen[i];
}
return res;
}
void _add(int x, T val) {
for (int i = x; i < len(fen); i += low_bit(i)) {
fen[i] += val;
}
}
public:
void init(int x) {
fen.clear();
fen.resize(x + 1);
}
void inc(int i, T x) {
i++;
_add(i, x);
}
T get(int l, int r) {
if (l == 0) return _get(r + 1);
return _get(r + 1) - _get(l);
}
T get(int i) {
return get(i, i);
}
void set(int i, T x) {
inc(i, x - get(i));
}
};
int count_swaps(vec<i32> s) {
int n = len(s);
deque<int> p1, p0;
for (int i = 0; i < n; i++) {
if (s[i] > 0) p1.push_back(i);
else p0.push_back(i);
}
fenvik<int> exist;
exist.init(n);
for (int i = 0; i < n; i++) {
exist.set(i, 1);
}
int last = 1;
int ans = 0;
for (int i = 0; i < n; i++) {
if (exist.get(i, i) == 0) continue;
if (s[i] == -last) {
last *= -1;
continue;
}
if (last == -1) {
int p = p0.front();
p0.pop_front();
exist.set(p, 0);
ans += exist.get(i, p);
} else {
int p = p1.front();
p1.pop_front();
exist.set(p, 0);
ans += exist.get(i, p);
}
last *= -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... |