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;
using ll = long long;
const int N = 2e5+100;
ll bit[N];
void upd(int pos, ll x) {
while(pos < N) bit[pos] += x, pos += pos&-pos;
}
ll pref(int pos, ll r = 0) {
while(pos > 0) r += bit[pos], pos -= pos&-pos;
return r;
}
ll get(int l, int r) {
if(r < l) return 0;
return pref(r) - pref(l - 1);
}
vector<int> le[N], ri[N]; int ryt[N];
long long count_swaps(vector<int> s) {
int n = s.size();
s.insert(s.begin(), 0);
for(int i = 1; i <= n; i++) {
ryt[i] = -1;
upd(i, +1);
if(s[i] < 0) le[-s[i]].push_back(i);
else ri[s[i]].push_back(i);
}
for(int i = 1; i <= n; i++) {
for(int j = 0; j < (int)le[i].size(); j++) {
int a = le[i][j], b = ri[i][j];
ryt[min(a, b)] = max(a, b);
}
}
ll ret = 0;
for(int i = 1; i <= n; i++) if(ryt[i] + 1) {
ret += get(i + 1, ryt[i] - 1);
ret += (s[ryt[i]] < 0);
upd(ryt[i], -1);
}
return ret;
}
# | 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... |