이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |