# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
331136 | Valera_Grinenko | Arranging Shoes (IOI19_shoes) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <unordered_map>
#include <iomanip>
#include <cmath>
#include <queue>
#include <bitset>
#include <numeric>
#include <array>
#include <cstring>
#include <random>
#include <chrono>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define make_unique(x) sort(all((x))); (x).resize(unique(all((x))) - (x).begin())
typedef long long ll;
typedef long double ld;
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// template<class T>
// using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 2e5 + 42;
int fw[N];
bool u[N];
void add(int k, int x) {
for(; k < N; k += (k & -k)) fw[k] += x;
}
int get(int k) {
int res = 0;
for(; k > 0; k -= (k & -k)) res += fw[k];
return res;
}
int get(int l, int r) {
return get(r) - get(l - 1);
}
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n;
cin >> n;
n *= 2;
vector<int> s(n + 1);
vector<vector<int> > posl(n + 1), posr(n + 1);
for(int i = 1; i <= n; i++) {
cin >> s[i];
if(s[i] > 0) posr[s[i]].pb(i);
else posl[-s[i]].pb(i);
}
ll ans = 0;
for(int i = n; i >= 1; i--) {
if(u[i]) continue;
if(s[i] > 0) {
int pl = posl[s[i]].back();
posl[s[i]].pop_back();
posr[s[i]].pop_back();
u[pl] = true;
add(pl, 1);
ans += i - pl - 1 - get(pl + 1, i - 1);
} else {
int pr = posr[-s[i]].back();
posr[-s[i]].pop_back();
posl[-s[i]].pop_back();
u[pr] = true;
add(pr, 1);
ans += i - pr - get(pr + 1, i - 1);
}
}
cout << ans;
return 0;
}
/*
*/