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 <iostream>
#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
#define s second
#define f first
using namespace std;
const int N = 200010;
vector <pair<int,int> > post,neg;
ll t[4*N],add[4*N];
pair<int,pair<int,int> > d[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);
}
bool comp(pair<int,int> a,pair<int,int> b){
if (a.f > b.f) return 1;
if (a.f == b.f && a.s < b.s) return 1;
return 0;
}
ll count_swaps(vector <int> a) {
int n = a.size();
for (int i = 0; i < a.size(); i++){
if (a[i] < 0) neg.pb({a[i],i});
else post.pb({a[i],i});
}
sort(neg.begin(),neg.end(),comp);
sort(post.begin(),post.end());
int cnt = 0;
for (int i=0;i<neg.size();i++){
int l = post[i].s,r = neg[i].s;
d[++cnt] = {abs(r-l),{l,r}};
}
sort(d + 1,d + cnt + 1);
ll ans=0;
for (int i = 1; i <= cnt; i++){
int l = d[i].s.f,r = d[i].s.s;
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: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++){
| ~~^~~~~~~~~~
shoes.cpp:63:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for (int i=0;i<neg.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... |