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"
#pragma once
#include <deque>
#include <algorithm>
using namespace std;
deque<int> left[200005];
deque<int> right[200005];
int aib[200005];
bool viz[200005];
void update(int pos,int val){
for(;pos <= 2e5 + 1;pos += (-pos) & pos){
aib[pos] += val;
}
}
int query(int pos){
int ans = 0;
for(;pos;pos -= (-pos) & pos){
ans += aib[pos];
}
return ans;
}
long long count_swaps(vector<int> s) {
reverse(s.begin(),s.end());
s.push_back(0);
reverse(s.begin(),s.end());
for(int i = 1;i < (int)s.size();i++){
if(s[i] < 0){
left[-s[i]].push_back(i);
}
else{
right[s[i]].push_back(i);
}
update(i,1);
}
long long ans = 0;
for(int i = 1;i < (int)s.size();i++){
if(viz[i] == true){
continue;
}
if(s[i] < 0){
int pos = right[-s[i]].front();
right[-s[i]].pop_front();
left[-s[i]].pop_front();
update(pos,-1);
viz[pos] = true;
update(i,-1);
viz[i] = true;
ans += query(pos) - query(i);
}
else{
int pos = left[s[i]].front();
left[s[i]].pop_front();
right[s[i]].pop_front();
update(pos,-1);
viz[pos] = true;
ans += query(pos) - query(i - 1);
update(i,-1);
viz[i] = true;
}
}
return ans;
}
Compilation message (stderr)
shoes.cpp:3:9: warning: #pragma once in main file
#pragma once
^~~~
# | 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... |