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"
#ifdef NYAOWO
#include "grader.cpp"
#endif
#include <bits/stdc++.h>
#define For(i, a, b) for(int i = a; i <= b; i++)
#define Forr(i, a, b) for(int i = a; i >= b; i--)
#define F first
#define S second
#define eb emplace_back
#define sz(x) ((int)x.size())
#define all(x) x.begin(), x.end()
#define int LL
using namespace std;
using LL = long long;
using pii = pair<int, int>;
const int MAXN = 200010;
#define LO(x) (x & (-x))
struct BIT {
int n;
int a[MAXN];
void init(int _n) {
n = _n;
memset(a, 0, sizeof(a));
}
void add(int i, int x) {
while(i <= n) {
a[i] += x;
i += LO(i);
}
}
int ask(int i) {
int res = 0;
while(i) {
res += a[i];
i -= LO(i);
}
return res;
}
} bit;
vector<int> l[MAXN];
vector<int> r[MAXN];
int solve1(vector<int32_t> s) {
int n = sz(s) / 2;
For(i, 1, n) {
l[i].clear();
r[i].clear();
}
For(i, 0, n * 2 - 1) {
if(s[i] < 0) l[-s[i]].eb(i + 1);
else r[s[i]].eb(i + 1);
}
vector<pii> owo;
int add = 0;
For(siz, 1, n) {
For(i, 0, sz(l[siz]) - 1) {
int a = l[siz][i];
int b = r[siz][i];
if(a > b) {
swap(a, b);
add++;
}
owo.eb(a, b);
}
}
sort(all(owo), [&](pii a, pii b) {
return a.F < b.F;
});
int res = 0;
bit.init(n * 2);
For(i, 1, n * 2) bit.add(i, 1);
for(auto &p:owo) {
bit.add(p.F, -1);
res += bit.ask(p.F);
bit.add(p.S, -1);
res += bit.ask(p.S);
}
return res + add;
}
long long count_swaps(std::vector<int32_t> s) {
return solve1(s);
}
# | 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... |