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;
#define all(x) x.begin(), x.end()
#define pb push_back
#define ll long long
#define vout(v) for(int i=0; i<v.size(); i++) cout<<v[i]<<' ';
int tt[10];
ll t[1000100], b[1001000], a[400100];
void build(int v, int l, int r){
if(l == r){
t[v] = 1;
return;
}
int mid=(l+r)/2;
build(v+v, l, mid);
build(v+v+1, mid+1, r);
t[v] = t[v+v]+t[v+v+1];
}
void upd(int v, int l, int r, int pos, int x){
if(l == r){
t[v] = x;
return;
}
int mid=(l+r)/2;
if(mid>=pos){
upd(v+v, l, mid, pos, x);
}
else{
upd(v+v+1, mid+1, r, pos, x);
}
t[v] = t[v+v]+t[v+v+1];
}
ll get(int v, int l, int r, int L, int R){
if(r < L or R < l){
return 0;
}
if(L <= l and r <= R){
return t[v];
}
int mid=(l+r)/2;
return get(v+v, l, mid, L, R) + get(v+v+1, mid+1, r, L, R);
}
int pos1[400100], pos0[400100];
long long count_swaps(vector<int> S){
int n = S.size();
for(int i=1; i<=n; i++){
a[i] = S[i-1];
}
for(int i=1; i<=n; i++){
if(a[i] > 0) pos0[a[i]] = i;
else pos1[-a[i]] = i;
b[i] = 1;
}
ll ans=0;
build(1, 1, n);
for(int i=1; i<=n; i++){
if(b[i] == 0){
continue;
}
if(a[i] > 0){
//cout<<i<<' '<<a[i]<<' '<<pos1[a[i]]<<'\n';
ans += get(1, 1, n, 1, pos1[a[i]])-1;
b[i] = 0;
b[pos1[a[i]]] = 0;
upd(1, 1, n, i, 0);
upd(1, 1, n, pos1[a[i]], 0);
}
else{
b[i] = 0;
upd(1, 1, n, i, 0);
ans += get(1, 1, n, 1, pos0[-a[i]])-1;
b[pos0[-a[i]]] = 0;
upd(1, 1, n, pos0[-a[i]], 0);
}
}
return ans;
}
| # | 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... |