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"
using namespace std;
#define pb push_back
#define ll long long
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
const int N = 3e5+5;
set<int> S[N];
int bel[N], en[N], st[N], cnt[N], vis[N];
ll count_swaps(vector<int> v){
int n = (int)v.size(), ad = sqrt(n), _ = -1;
for(int i = 0; i < n; ++i){
int pk = i/ad;
bel[i] = pk, en[pk] = i, cnt[pk]++;
if(pk > _)
st[pk] = i;
_ = pk;
}
for(int i = 0; i < n; ++i)
S[v[i]+n].insert(i);
ll ans = 0;
for(int i = 0; i < n-1; ++i){
if(vis[i])
continue;
ans += (v[i] > 0 ? 1 : 0);
auto a = S[-v[i]+n].upper_bound(i);
S[-v[i]+n].erase(a);
int j = *a;
vis[j] = 1, cnt[bel[j]]--;
if(bel[i+1] == bel[j-1] or i+1 > j-1){
for(int k = i+1; k < j; ++k)
ans += (!vis[k]);
continue;
}
for(int k = i+1; k <= en[bel[i+1]]; ++k)
ans += (!vis[k]);
for(int k = st[bel[j-1]]; k <= j-1; ++k)
ans += (!vis[k]);
for(int k = bel[i+1]+1; k < bel[j-1]; ++k)
ans += cnt[k];
}
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... |