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 "shoes.h"
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5;
vector<int> lef, rig[N + 5];
int tree[2*N + 5];
void update(int n, int k, int v) {
for (; k <= n; k += k&-k) tree[k] += v;
}
int query(int k) {
int sum = 0;
for (; k; k -= k&-k) sum += tree[k];
return sum;
}
long long count_swaps(std::vector<int> s) {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n = s.size()/2;
s.insert(s.begin(), 0);
for (int i = 1; i <= 2*n; i++) {
if (s[i] < 0) lef.push_back(i);
else rig[s[i]].push_back(i);
}
sort(lef.begin(), lef.end());
for (int i = 1; i <= N; i++) sort(rig[i].begin(), rig[i].end(), greater<int>());
int index = 0;
long long ans = 0;
for (auto&i : lef) {
index++;
int new_i = i + query(2*n) - query(i);
ans += (long long)new_i - index;
update(2*n, i, 1);
index++;
int j = rig[-s[i]].back();
rig[-s[i]].pop_back();
int new_j = j + query(2*n) - query(j);
ans += (long long)new_j - index;
update(2*n, j, 1);
}
return ans;
}
/*signed main() {
int n;
cin >> n;
vector<signed> arr(2*n);
for (int i = 0; i < 2*n; i++) cin >> arr[i];
cout << count_swaps(arr) << '\n';
return 0;
}*/
# | 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... |