This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define MAX 1e9+7
#define MIN -1
#define all(a) a.begin (),a.end ()
#define pb push_back
#define mp make_pair
#define mem(a, c) memset(a, c, sizeof(a))
#define F first
#define S second
#define PI 3.141592653589
#define SMAX 1e18+7
using namespace std;
typedef vector <int> vi;
typedef pair<int, int> ii;
typedef vector <ii> vii;
typedef long long ll;
typedef double db;
int tree[800200];
void update(int node, int in, int fin, int pos) {
if (pos < in || pos >= fin) {
return;
}
tree[node] += 1;
if (in + 1 != fin) {
int mid = in + (fin - in) / 2;
update(2 * node + 1, in, mid, pos);
update(2 * node + 2, mid, fin, pos);
}
}
ll query(int node, int in, int fin, int l, int r) {
if (fin <= l || r <= in) {
return 0;
}
if (l <= in && fin <= r) {
return tree[node];
}
int mid = in + (fin - in) / 2;
return query(2 * node + 1, in, mid, l, r) +query(2 * node + 2, mid, fin, l, r);
}
ll count_swaps(vi a) {
int n = a.size() / 2;
ll ans = 0;
map<int, vi> last;
vi pr(2 * n, -1);
for (int i = 2 * n - 1; i >= 0; i--) {
last[a[i]].pb(i);
}
for (int i = 0; i < 2 * n; i++) {
if (pr[i] != -1)
continue;
pr[i] = last[-a[i]].back();
last[-a[i]].pop_back();
last[a[i]].pop_back();
ans += abs(pr[i] - i) - 1;
if ((pr[i] > i) != (a[pr[i]] > a[i])) {
ans++;
}
pr[pr[i]] = i;
}
vector<vi> q;
for (int i = 0; i < 2 * n; i++) {
q.pb({i});
if (pr[i] > i) {
q.pb({i, pr[i] + 1, -1});
q.pb({pr[i], pr[i] + 1, 1});
}
}
mem(tree, 0);
sort(all(q));
for (int i=0;i<q.size ();i++) {
vi v3=q[i];
if (v3.size() == 1) {
update(0, 0, 2 * n, pr[v3[0]]);
} else {
int x = query(0, 0, 2 * n, v3[1], 2 * n);
ans -= x * v3[2];
}
}
return ans;
}
/*
using namespace std;
int main() {
int n;
assert(1 == scanf("%d", &n));
vector<int> S(2 * n);
for (int i = 0; i < 2 * n; i++)
assert(1 == scanf("%d", &S[i]));
fclose(stdin);
long long result = count_swaps(S);
printf("%lld\n", result);
fclose(stdout);
return 0;
}
*/
Compilation message (stderr)
shoes.cpp: In function 'll count_swaps(vi)':
shoes.cpp:73:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i=0;i<q.size ();i++) {
~^~~~~~~~~~
# | 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... |