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 <iostream>
#include <vector>
#include <queue>
#include <cstdlib>
#include <algorithm>
#include "shoes.h"
using namespace std;
#define ll long long
struct Node {
int l, r, offset;
Node *lft, *rht;
Node (int tl, int tr): l(tl), r(tr), offset(0) {
if (l + 1 != r) {
lft = new Node(l, (l + r) / 2);
rht = new Node((l + r) / 2, r);
} else {
lft = rht = NULL;
}
}
};
void update(Node *x, int l, int r, int v) {
if (r <= x->l || x->r <= l) {
return;
}
if (l <= x->l && x->r <= r) {
x->offset += v;
return;
}
update(x->lft, l, r, v);
update(x->rht, l, r, v);
}
int query(Node *x, int pos) {
if (pos < x->l || x->r <= pos) {
return 0;
}
if (x->l + 1 == x->r) {
return x->offset;
}
return query(x->lft, pos) + query(x->rht, pos) + x->offset;
}
Node *root;
queue<int> unpaired[100005][2];
ll count_swaps(vector<int> s) {
root = new Node(0, s.size() + 5);
ll ans = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] < 0) {
if (unpaired[-s[i]][1].size()) {
ans += i - (unpaired[-s[i]][1].front() + query(root, unpaired[-s[i]][1].front()));
update(root, unpaired[-s[i]][1].front(), i + 1, 1);
unpaired[-s[i]][1].pop();
} else {
unpaired[-s[i]][0].push(i);
}
} else {
if (unpaired[s[i]][0].size()) {
ans += i - (unpaired[s[i]][0].front() + query(root, unpaired[s[i]][0].front())) - 1;
update(root, unpaired[s[i]][0].front(), i + 1, 1);
unpaired[s[i]][0].pop();
} else {
unpaired[s[i]][1].push(i);
}
}
}
return ans;
}
Compilation message (stderr)
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:52:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for (int i = 0; i < s.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... |