Untitled
raw download clone
CPP
views 19
,
size 1874 b
#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define mkp make_pair
#define ms(a, b) memset(a, b, sizeof(a))
#define ALL(x) x.begin(), x.end()
#define FOR(i, a, b) for(int i = a; i <= b; ++i)
#define REP(i, a, b) for(int i = a; i >= b; --i);
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
constexpr ll MOD = 1e9 + 7;

ll cnt = 0, n;
vector<int> vec;
void cal(int l, int r) {
	if(l == r) return;
	int mid = (l + r) >> 1;
	cal(l, mid), cal(mid + 1, r);
	vector<int> ret(r - l + 1);
	int idx = 0, lidx = l, ridx = mid + 1, tcnt = 0;
	while(lidx <= mid && ridx <= r) {
		if(vec[lidx] < vec[ridx])
			ret[idx++] = vec[lidx++], tcnt++;
		else
			ret[idx++] = vec[ridx++], (ret[idx - 1] % 2 ? cnt -= tcnt : cnt += tcnt);
	}
	while(lidx <= mid)
		ret[idx++] = vec[lidx++], tcnt++;
	while(ridx <= r)
		ret[idx++] = vec[ridx++], (ret[idx - 1] % 2 ? cnt -= tcnt : cnt += tcnt);
	for(int i = l, j = 0; i <= r; ++i, ++j)
		vec[i] = ret[j];
}

inline void solve() {
	cin >> n;
	vector<bool> used(n + 1, 0);
	vec = vector<int>(2 * n + 1);
	for(int i = 1; i <= 2 * n; ++i) {
		int tmp; cin >> tmp;
		vec[i] = tmp * 2 - !used[tmp];
		used[tmp] = 1;
	}
	cal(1, 2 * n);
	cout << cnt - n << '\n';
}

//#define IOFILE
int main() {
#ifndef IOFILE
	//#pragma GCC optimize("O2")
	cin.tie(nullptr), cout.tie(nullptr), ios::sync_with_stdio(false);
#endif
#ifdef IOFILE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	freopen("err.txt", "w", stderr);
	clock_t time = clock();
#endif
	int t = 1;
	// cin >> t;
	for(int i = 1; i <= t; ++i) {
		// cout << "Case " << i << ":\n";
		solve();
	}

#ifdef IOFILE
	cout << "\nUse " << static_cast<double>(clock() - time) / CLOCKS_PER_SEC << " seconds";
#endif
}
close fullscreen
Login or Register to edit or fork this paste. It's free.