이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "shoes.h"
#include <vector>
#define N 100005
#define pb push_back
#define ls p<<1
#define rs p<<1|1
#define vi std::vector<int>
#define rep(i, a, b) for(int i = a; i < b; i++)
int t[N << 2];
void build(int p, int l, int r){
if(l == r){
t[p] = 1;
return;
}
int m = (l + r) >> 1;
build(ls, l, m);
build(rs, m+1, r);
t[p] = t[ls] + t[rs];
}
void upd(int p, int l, int r, int x, int v){
t[p] += v;
if(l == r){
return;
}
int m = (l+r) >> 1;
if(x <= m)upd(ls, l, m, x, v);
else upd(rs, m+1, r, x, v);
}
int query(int p, int l, int r, int x, int y){
if(x > y)return 0;
if(l >= x && r <= y)return t[p];
int m = (l+r) >> 1, ans = 0;
if(x <= m)ans = query(ls, l, m, x, y);
if(y > m)ans += query(rs, m+1, r, x, y);
return ans;
}
vi a[N], b[N];
long long count_swaps(std::vector<int> s) {
int n = s.size();
build(1, 0, n-1);
rep(i, 1, n+1)a[i].clear(), b[i].clear();
rep(i, 0, n){
if(s[i] < 0)a[-s[i]].pb(i);
else b[s[i]].pb(i);
}
long long ans = 0;
rep(i, 1, n+1){
int K = 0;
for(auto &x : a[i]){
int k = b[i][K++], v;
if(k < x){
v = query(1, 0, n-1, k+1, x-1);
ans += v + 1;
}
else{
v = query(1, 0, n-1, x+1, k-1);
ans += v;
}
upd(1, 0, n-1, k, -1);
upd(1, 0, n-1, x, 1);
}
}
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... |