# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1257481 | Anphat | Arranging Shoes (IOI19_shoes) | C++20 | 0 ms | 0 KiB |
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f first
#define s second
#define sz(a) (int)((a).size())
#define all(a) (a).begin(), (a).end()
#define uni(a) sort(all(a)), (a).resize(unique(all(a)) - (a).begin());
typedef pair <int, int> ii;
const int N = 2e5 + 10;
int n, a[N], f[N];
multiset <int> st[N];
ll ans;
void upd(int x, int y) {
for (; x < N; x += x & -x)
f[x] += y;
}
int get(int x) {
int s = 0;
for (; x; x -= x & -x)
s += f[x];
return s;
}
int get(int l, int r) {
return get(r) - get(l - 1);
}
void solve() {
cin >> n;
for (int i = 1; i <= 2 * n; i++) {
cin >> a[i];
st[a[i] + n].insert(i);
upd(i, 1);
}
for (int i = 1; i <= 2 * n; i++) {
if (!sz(st[n - a[i]]))
continue;
if (!st[a[i] + n].count(i))
continue;
auto it = st[n - a[i]].lower_bound(i);
if (it != st[n - a[i]].end()) {
if (i + 1 <= *it - 1) ans += get(i + 1, *it - 1);
if (get(i, 2 * n) % 2 == 0 && a[i] > 0) ans++;
st[n - a[i]].erase(it);
upd(*it, -1);
}
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// freopen("a.inp", "r", stdin);
// freopen("a.out", "w", stdout);
int t = 1;
// cin >> t;
while (t--) {
solve();
}
return 0;
}