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 debug printf
#define ll long long
#define maxn 200200
queue<int> st[maxn];
int par[maxn];
int foi[maxn];
int T[maxn];
void upd(int idx,int val){
idx++;
while(idx < maxn){
T[idx] += val;
idx += (idx&-idx);
}
}
int get(int idx){ //debug("get %d\n",idx);
idx++;;
int r = 0;
while(idx){
r += T[idx];
idx -= (idx&-idx);
}
//debug("-> %d\n",r);
return r;
}
long long count_swaps(std::vector<int> S){
int n = S.size();
ll ans = 0;
for(int i=0;i<n;i++){
//debug("i %d %d\n",i,S[i]);
int sz = abs(S[i]);
if(st[sz].size() > 0 && S[st[sz].front()] == - S[i]){
if(S[i] < 0) ans++;
par[st[sz].front()] = i;
//debug("%d - %d\n",st[sz].front(),i);
st[sz].pop();
}
else {
st[sz].push(i);
//debug("add\n");
}
}
for(int i=0;i<n;i++) upd(i,1);
for(int i=0;i<n;i++) if(!foi[i]){
int u = par[i];
ans += get(u-1) - get(i);
upd(u,-1);
foi[u] = 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... |