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 <bits/stdc++.h>
#include "shoes.h"
using namespace std;
#define sp " "
#define endl "\n"
#define pb push_back
#define pii pair<int, int>
#define st first
#define nd second
#define fileio() freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout)
#define fastio() cin.tie(0), ios_base::sync_with_stdio(0)
#define LL node * 2
#define RR node * 2 + 1
#define N 300005
#define ll long long
int tree[N], n;
vector<int> lft[N], rgt[N];
void update(int x, int val){
while(x <= n){
tree[x] += val;
int lsb = x & -x;
x += lsb;
}
}
int query(int x){
int ans = 0;
while(x > 0){
ans += tree[x];
int lsb = x & -x;
x -= lsb;
}
return ans;
}
long long count_swaps(vector<int> s) {
n = s.size();
vector<pii> ranges;
for (int i = 1; i <= n; i++){
if (s[i - 1] < 0) lft[-s[i - 1]].pb(i);
else rgt[s[i - 1]].pb(i);
}
ll ans = 0;
for (int i = 1; i <= n; i++){
while(!lft[i].empty()){
int a = lft[i].back(), b = rgt[i].back();
if (a > b) ans++, swap(a, b);
lft[i].pop_back(), rgt[i].pop_back();
ranges.pb({a, b});
}
}
sort(ranges.begin(), ranges.end());
for (auto i : ranges){
ans += i.nd - i.st - 1 - (query(i.nd) - query(i.st));
update(i.nd, 1);
}
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... |