Submission #1134919

#TimeUsernameProblemLanguageResultExecution timeMemory
1134919trvhungArranging Shoes (IOI19_shoes)C++20
100 / 100
81 ms17992 KiB
#ifndef TPV
#include "shoes.h"
#endif

#include <bits/stdc++.h>
// #include <ext/rope>
// #include <ext/pb_ds/assoc_container.hpp>

// using namespace __gnu_pbds;
// using namespace __gnu_cxx;
using namespace std;

// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")

// #define   ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define            ll long long
#define           ull unsigned long long
#define            ld long double
#define            pb push_back
#define  bit(mask, i) ((mask >> i) & 1)
#define            el '\n'
#define             F first
#define             S second

template <class X, class Y> bool maximize(X &x, const Y &y) { return (x < y ? x = y, 1 : 0); }
template <class X, class Y> bool minimize(X &x, const Y &y) { return (x > y ? x = y, 1 : 0); }

const int INF = 1e9;
const ll LINF = 1e18;
const int MOD = 1e9 + 7;
const int MULTI = 0;
const ld eps = 1e-9;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; // R D L U
const int ddx[4] = {-1, 1, 1, -1}, ddy[4] = {1, 1, -1, -1}; // UR DR DL UL
const char cx[4] = {'R', 'D', 'L', 'U'};
const ll base = 31;
const int nMOD = 2;
const ll mods[] = {(ll)1e9 + 10777, (ll)1e9 + 19777, (ll)1e9 + 3, (ll)1e9 + 3777};

const int maxn = 2e5 + 5;
int n, a[maxn];
ll res;
vector<int> vec[maxn];
bool mark[maxn];

#define vec(i) vec[i + 100000]

struct seg {
	int st[4 * maxn], lz[4 * maxn];

	void push(int id, int l, int r) {
		int t = lz[id];

		st[id << 1] += t;
		lz[id << 1] += t;

		st[id << 1 | 1] += t;
		lz[id << 1 | 1] += t;

		lz[id] = 0;
	}

	void update(int id, int l, int r, int u, int v, int val) {
		if (l == u && r == v) {
			st[id] += val;
			lz[id] += val;
			return;
		}

		int mid = l + r >> 1; push(id, l, r);
		if (v <= mid) update(id << 1, l, mid, u, v, val);
		else if (u > mid) update(id << 1 | 1, mid + 1, r, u, v, val);
		else {
			update(id << 1, l, mid, u, mid, val);
			update(id << 1 | 1, mid + 1, r, mid + 1, v, val);
		}

		st[id] = max(st[id << 1], st[id << 1 | 1]);
	}

	int get(int id, int l, int r, int u) {
		if (l == r) return st[id];

		push(id, l, r); int mid = l + r >> 1;

		if (u <= mid) return get(id << 1, l, mid, u);
		else return get(id << 1 | 1, mid + 1, r, u);
	}
} IT;

void solve() {
	n >>= 1;

	for (int i = 2 * n; i >= 1; --i)
		vec(a[i]).pb(i);

	int i = 1;
	while (i <= 2 * n) {
		if (mark[i]) {
			i++;
			continue;
		}

		int oth = a[i] * -1;
		int posOth = vec(oth).back() + IT.get(1, 1, 2 * n, vec(oth).back());

		mark[vec(oth).back()] = true;
		res += posOth - (a[i] < 0 ? i + 1 : i) - (a[i] < 0 ? IT.get(1, 1, 2 * n, i + 1) : IT.get(1, 1, 2 * n, i));

		IT.update(1, 1, 2 * n, i + 1, vec(oth).back(), 1);
		vec(a[i]).pop_back();
		vec(oth).pop_back();

		i++;
	}
}

ll count_swaps(vector<int> s) {
	n = (int) s.size();
	for (int i = 1; i <= n; ++i)
		a[i] = s[i - 1];

	solve();

	return res;
}

#ifdef TPV

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

	freopen("input.inp", "r", stdin);
	freopen("output.out", "w", stdout);

	vector<int> I;
	int x;

	while (cin >> x) I.push_back(x);

	cout << count_swaps(I);

	return 0;
}

#endif
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...