#include "shoes.h"
#include <bits//stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<long long, null_type, less<long long>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
typedef tree<long long, null_type, less_equal<long long>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_multiset;
#define ll long long
#define ld long double
#define iloop(m, h) for (ll i = m; i != h; i += (m < h ? 1 : -1))
#define jloop(m, h) for (auto j = m; j != h; j += (m < h ? 1 : -1))
#define kloop(m, h) for (auto k = m; k != h; k += (m < h ? 1 : -1))
#define lloop(m, h) for (auto l = m; l != h; l += (m < h ? 1 : -1))
#define iloop_(m, h, g) for (auto i = m; i < h; i += g)
#define jloop_(m, h, g) for (auto j = m; j < h; j += g)
#define kloop_(m, h, g) for (auto k = m; k < h; k += g)
#define lloop_(m, h, g) for (auto l = m; l < h; l += g)
#define getchar_unlocked _getchar_nolock // comment before submission
#define pll pair<ll, ll>
#define plll pair<ll, pll>
#define pllll pair<pll, pll>
#define vll vector<ll>
#define qll queue<ll>
#define dll deque<ll>
#define pqll priority_queue<ll>
#define gll greater<ll>
#define INF 1000000000000000
#define MOD1 1000000007
#define MOD2 998244353
#define MOD3 1000000009
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
ll n, a[200005];
struct node {
ll s, e, m, v = 0;
node *l, *r;
node (ll S, ll E) {
s = S, e = E, m = (S+E)>>1;
if (s == e) return;
l = new node(s, m);
r = new node(m+1, e);
}
void upd(ll X, ll V) {
if (s == e) {v = V; return;}
if (X <= m) l->upd(X, V);
else r->upd(X, V);
v = l->v + r->v;
}
ll query(ll S, ll E) {
if (s == S && e == E) return v;
if (E <= m) return l->query(S, E);
if (S > m) return r->query(S, E);
return l->query(S, m) + r->query(m+1, E);
}
};
ll count_swaps(vector<int> s) {
n = s.size() / 2;
set<pll> st;
iloop(0, 2*n) {a[i] = s[i]; st.insert({a[i], i});}
bool act[2*n];
memset(act, 0, sizeof(act));
node root = node(0, 2*n-1);
ll cans = 0;
iloop(0, 2*n) {
if (act[i]) continue;
act[i] = 1;
st.erase({a[i], i});
auto it = st.lower_bound({-a[i], -INF});
ll tm = (*it).second;
st.erase(it);
cans += tm - i - root.query(i, tm) - 1;
if (a[i] > 0) cans++;
act[tm] = 1;
root.upd(i, 1);
root.upd(tm, 1);
}
return cans;
}
# | 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... |