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>
#include "shoes.h"
using namespace std;
typedef long long ll;
ll sol = 0, k = 1;
vector <ll> T;
void build(ll n){
while (k < n)k *= 2;
T.resize(2*k, 0);
}
void update(ll x){
x += k;
T[x] = 1;
for (x >>= 1; x; x >>= 1)T[x] = T[2*x]+T[2*x+1];
}
ll range_sum(ll x, ll y){
if (y <= x)return 0;
ll sum = 0;
for (x += k, y += k; y > x; x >>= 1, y >>= 1){
if (x & 1)sum += T[x++];
if (y & 1)sum += T[--y];
}
return sum;
}
long long count_swaps(std::vector<int> s) {
ll n = s.size()/2;
vector <vector <ll>> loc_pos(n+1), loc_neg(n+1);
for (int i = 0; i < s.size(); i++){
if (s[i] > 0)loc_pos[s[i]].push_back(i);
else loc_neg[-s[i]].push_back(i);
}
vector <ll> point(n+1);
for (int i = 0; i <= n; i++)point[i] = loc_pos[i].size()-1;
vector <pair<ll, ll>> couples;
vector <bool> taken(s.size(), 0);
for (int i = s.size()-1; i >= 0; i--){
if (taken[i])continue;
ll x;
if (s[i] > 0){
x = loc_neg[s[i]][point[s[i]]];
point[s[i]]--;
}
else {
sol++;
x = loc_pos[-s[i]][point[-s[i]]];
point[-s[i]]--;
}
taken[x] = 1;
couples.push_back({x, i});
}
build(s.size());
reverse(couples.begin(), couples.end());
for (auto p : couples){
//cerr << p.first << " " << p.second << "\n";
sol += range_sum(p.first, p.second);
update(p.first);
update(p.second);
}
return sol;
}
Compilation message (stderr)
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:36:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | for (int i = 0; i < s.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... |