이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "shoes.h"
#include <bits/stdc++.h>
using namespace std;
using llong = long long;
#define BUGO(x) cerr << #x << " = " << x << '\n';
const int maxN = 200'005;
int t[maxN * 4];
int Get(int v, int vl, int vr, int i) {
if (vl == vr)
return t[v];
int vm = vl + (vr - vl) / 2;
if (i <= vm)
return t[v] + Get(v * 2, vl, vm, i);
return t[v] + Get(v * 2 + 1, vm + 1, vr, i);
}
void Add(int v, int vl, int vr, int l, int r, int val) {
if (l > vr || vl > r) return;
if (l <= vl && vr <= r) {
t[v] += val;
return;
}
int vm = vl + (vr - vl) / 2;
Add(v * 2, vl, vm, l, r, val);
Add(v * 2 + 1, vm + 1, vr, l, r, val);
}
vector<int> pos_[maxN * 2];
vector<int>* pos = pos_ + maxN;
long long count_swaps(vector<int> a) {
int n = a.size();
for (int i = 1; i < n; ++i)
Add(1, 0, n - 1, i, i, i);
for (int i = 0; i < n; ++i)
pos[a[i]].push_back(i);
for (int i = -n; i <= n; ++i)
reverse(pos[i].begin(), pos[i].end());
llong ans = 0;
set<int> s;
for (int i = 0; i < n; ++i)
s.insert(i);
for (int i = 0; i < n; i += 2)
{
int ai = a[*s.begin()];
int ai1 = a[*next(s.begin())];
if (abs(ai) == abs(ai1) && ai < 0 && ai1 > 0) {
s.erase(s.begin(), next(s.begin(), 2));
pos[ai].pop_back();
pos[ai1].pop_back();
continue;
}
int j = pos[-ai].back();
s.erase(s.begin());
s.erase(s.find(j));
pos[-ai].pop_back();
pos[ai].pop_back();
ans += (Get(1, 0, n - 1, j) - (i + 1));
if (i + 1 <= j - 1)
Add(1, 0, n - 1, i + 1, j - 1, 1);
if (ai > 0)
++ans;
}
return ans;
}
# | 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... |