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 <bits/stdc++.h>
using namespace std;
#define pb push_back
vector<pair<int, int> > pairs;
const int N = 1e6;
long long seg[4*N];
bool done[N];
void build(int p, int s, int e){
if(s == e){
seg[p] = 1;
return;
}
build(2*p, s, (s + e)/2);
build(2*p + 1, (s + e)/2 + 1, e);
seg[p] = seg[2*p] + seg[2*p + 1];
}
void update(int p, int s, int e, int i){
if(s == e){
if(s == i){
seg[p] = 0;
}
return;
}
if(i <= (s + e)/2)update(2*p, s, (s + e)/2, i);
else update(2*p + 1, (s + e)/2 + 1, e, i);
seg[p] = seg[2*p] + seg[2*p + 1];
}
long long get(int p, int s, int e, int a, int b){
if(a <= s && b >= e){
return seg[p];
}
else if(b < s || a > e){
return 0;
}
return get(2 * p, s, (s + e)/2, a, b) + get(2 * p + 1, (s + e)/2 + 1, e, a, b);
}
long long count_swaps(vector<int> s) {
int n = s.size();
queue<int> left[2*n + 1];
queue<int> right[2*n + 1];
for(int i = 0; i < n; i++){
if(s[i] < 0){
if(!right[abs(s[i])].empty()){
pairs.pb({right[abs(s[i])].front(), i});
right[abs(s[i])].pop();
}
else{
left[abs(s[i])].push(i);
}
}
else{
if(!left[s[i]].empty()){
pairs.pb({left[s[i]].front(), i});
left[s[i]].pop();
}
else{
right[s[i]].push(i);
}
}
}
build(1, 0, n - 1);
long long ans = 0;
sort(pairs.begin(), pairs.end());
for(int i = 0; i < pairs.size(); i++){
if(s[pairs[i].first] > 0){
ans++;
}
}
memset(done, 0, sizeof(done));
for(int i = 0; i < pairs.size(); i++){
update(1, 0, n - 1, pairs[i].second);
ans += get(1, 0, n - 1, pairs[i].first + 1, pairs[i].second - 1);
}
return ans;
}
Compilation message (stderr)
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:73:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | for(int i = 0; i < pairs.size(); i++){
| ~~^~~~~~~~~~~~~~
shoes.cpp:79:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
79 | for(int i = 0; i < pairs.size(); i++){
| ~~^~~~~~~~~~~~~~
# | 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... |