This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// I can do all things through Christ who strengthens me
// Philippians 4:13
#include "shoes.h"
#include "bits/stdc++.h"
using namespace std;
#define REP(i, j, k) for (int i = j; i < (k); i++)
#define RREP(i, j, k) for (int i = j; i >= (k); i--)
template <class T>
inline bool mnto(T &a, const T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T &a, const T b) {return a < b ? a = b, 1 : 0;}
typedef long long ll;
typedef long double ld;
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
#define SZ(x) (int) x.size()
#define ALL(x) x.begin(), x.end()
#define pb push_back
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<ll> vll;
typedef tuple<int, int, int> iii;
typedef vector<iii> viii;
#ifndef DEBUG
#define cerr if (0) cerr
#endif
const int INF = 1000000005;
const ll LINF = 1000000000000000005;
const int MAXN = 200005;
int n;
vi vp[2][MAXN];
namespace st2 {
ll count_swaps(vi s) {
vi lp, rp;
REP (i, 0, 2 * n) {
if (s[i] < 0) {
lp.pb(i);
} else {
rp.pb(i);
}
}
ll ans = 0;
while (!s.empty()) {
int id = -1;
RREP (i, SZ(s) - 2, 0) {
if (abs(s[i]) == abs(s.back()) && s[i] != s.back()) {
id = i;
break;
}
}
assert(id != -1);
ans += SZ(s) - id - (s.back() < 0 ? 1 : 2);
s.pop_back();
s.erase(s.begin() + id);
}
return ans;
/*
vi id(n);
iota(ALL(id), 0);
do {
ll cur = 0;
REP (i, 0, n) {
REP (j, i + 1, n) {
if (id[i] > id[j]) {
cur++;
}
}
}
vector<bool> vis(n, 0);
REP (i, 0, n) {
REP (j, 0, n) {
if (vis[j] || abs(s[lp[i]]) != abs(s[rp[j]])) {
continue;
}
vis[j] = 1;
}
}
} while (next_permutation(ALL(id)));
*/
}
}
namespace st3 {
ll count_swaps(vi s) {
int ptr = 0;
ll ans = 0;
REP (i, 0, 2 * n) {
if (s[i] < 0) {
ans += abs(i - ptr);
ptr += 2;
}
}
return ans;
}
}
int fw[MAXN];
void incre(int i, int x) {
i++;
for (; i <= 2 * n; i += i & -i) {
fw[i] += x;
}
}
int qsm(int i) {
i++;
int res = 0;
for (; i > 0; i -= i & -i) {
res += fw[i];
}
return res;
}
int qsm(int s, int e) {
return qsm(e) - qsm(s - 1);
}
ll count_swaps(vi s) {
n = SZ(s) / 2;
/*
bool st3 = 1;
REP (i, 1, 2 * n) {
if (abs(s[i]) != abs(s[i - 1])) {
st3 = 0;
}
}
bool st2 = n <= 1000;
if (st2) {
return st2::count_swaps(s);
} else if (st3) {
return st3::count_swaps(s);
}
ll ans = 0;
REP (i, 0, n) {
ans += i;
}
*/
REP (i, 0, 2 * n) {
if (s[i] < 0) {
vp[0][-s[i]].pb(i);
} else {
vp[1][s[i]].pb(i);
}
}
vector<bool> vis(2 * n, 0);
ll ans = 0;
RREP (i, 2 * n - 1, 0) {
if (vis[i]) {
continue;
}
bool b = s[i] > 0;
int j = vp[b ^ 1][abs(s[i])].back();
vp[b][abs(s[i])].pop_back(); vp[b ^ 1][abs(s[i])].pop_back();
vis[i] = vis[j] = 1;
int cur = i - j - b;
cur -= qsm(j, i);
incre(j, -1); incre(i, -1);
ans += cur;
}
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... |