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<queue>
#define LL long long
#define MAXN 200005
using namespace std;
LL N, BIT[MAXN], ans;
priority_queue<int> PQ[MAXN>>1][2]; //only BIT is 1 indexed
bool del[MAXN];
int abs(int i) {return(i > 0 ? i : -i);}
void upd(int p, int v) {
for (; p<=N+5; p+=p&(-p)) {BIT[p]+=v;}
}
LL ask(int p) {
LL res=0;
for (; p>0; p-=p&(-p)) {res+=BIT[p];}
return(res);
}
long long count_swaps(std::vector<int> s) {
N=s.size();
for (int i=1; i<=N; i++) {
upd(i,1);
PQ[abs(s[i-1])][s[i-1]<0].push(1-i);
}
for (int i=0; i<N; i++) {
if (del[i]) {continue;}
LL v;
while (true) {
v=-PQ[abs(s[i])][s[i]>0].top();
PQ[abs(s[i])][s[i]>0].pop();
if (del[v]) {continue;}
else {break;}
}
ans+=ask(v)-ask(i+1);
if (s[i] > 0) {ans++;}
del[i]=del[v]=true;
upd(v+1, -1);
upd(i+1, -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... |