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 <vector>
#include <algorithm>
#include <set>
#define pb push_back
#define ll long long
#define left (h<<1),l,((l+r)>>1)
#define right ((h<<1)|1),((l+r)>>1) + 1,r
using namespace std;
const int N = 100005;
set <int> neg[N],post[N];
bool f[N];
ll t[4*N],add[4*N];
pair<int,pair<int,int> > d[2*N];
void push(int h,int l,int r){
if (!add[h]) return;
t[h*2] += add[h]*((l+r)/2 - l + 1);
t[h*2 + 1] += add[h]*(r - (l+r)/2);
add[h*2] += add[h];
add[h*2 + 1] += add[h];
add[h] = 0;
}
void upd(int h,int l,int r,int L,int R,int val){
if (r < L || R < l) return ;
if (L <= l && r <= R){
t[h] += val*(r-l+1);
add[h] += val;
return;
}
push(h,l,r);
upd(left,L,R,val),upd(right,L,R,val);
t[h] = t[h*2] + t[h*2 + 1];
}
ll get(int h,int l,int r,int idx){
if (l == r) return t[h];
push(h,l,r);
if (idx > (l+r)/2) return get(right,idx);
return get(left,idx);
}
ll count_swaps(vector <int> a) {
int n = a.size();
for (int i = 0; i < a.size(); i++){
if (a[i] < 0) neg[-a[i]].insert(i);
else post[a[i]].insert(i);
}
ll cnt = 0;
for (int i = 0; i < a.size(); i++){
if (f[i]) continue;
if (a[i] < 0){
neg[-a[i]].erase(neg[-a[i]].begin());
int ind = *post[-a[i]].begin();
f[ind] = 1;
post[-a[i]].erase(post[-a[i]].begin());
d[++cnt] = {abs(ind - i),{i,ind}};
}
else{
post[a[i]].erase(post[a[i]].begin());
int ind = *neg[a[i]].begin();
f[ind] = 1;
neg[a[i]].erase(neg[a[i]].begin());
d[++cnt] = {abs(ind - i),{ind,i}};
}
}
sort(d + 1,d + cnt + 1);
ll ans=0;
for (int i = 1; i <= cnt; i++){
int l = d[i].second.first,r = d[i].second.second;
if (l > r) swap(l,r);
ans += r - l - 1 - get(1,1,n, l + 1) - get(1,1,n,r + 1);
if (a[l] > 0 && a[r] < 0) ans++;
upd(1,1,n,l + 2, r,1);
}
return ans;
}
Compilation message (stderr)
shoes.cpp: In function 'long long int count_swaps(std::vector<int>)':
shoes.cpp:48:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | for (int i = 0; i < a.size(); i++){
| ~~^~~~~~~~~~
shoes.cpp:54:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for (int i = 0; i < a.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... |