이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "shoes.h"
#include <bits/stdc++.h>
#define pb push_back
#define all(v) ((v).begin(), (v).end())
#define sortv(v) sort(all(v))
#define sz(v) ((int)(v).size())
#define uniqv(v) (v).erase(unique(all(v)), (v).end())
#define umax(a, b) (a)=max((a), (b))
#define umin(a, b) (a)=min((a), (b))
#define FOR(i,a,b) for(int i = (a); i <= (b); i++)
#define rep(i,n) FOR(i,1,n)
#define rep0(i,n) FOR(i,0,(int)(n)-1)
#define FI first
#define SE second
#define INF 2000000000
#define INFLL 1000000000000000000LL
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAX_N = 100000;
int N;
vector<int> vt[2][MAX_N+1];
int l[MAX_N+1], r[MAX_N+1];
bool chk[MAX_N+1];
struct SEG{
struct NODE{
int l, r;
int sum;
};
int SZ;
vector<NODE> seg;
void Init(int x){
SZ = x;
seg.pb({-1, -1, 0});
init(0, 0, SZ-1);
}
void init(int idx, int s, int e){
if(s==e) return;
seg[idx].l = seg.size(); seg.pb({-1, -1, 0});
seg[idx].r = seg.size(); seg.pb({-1, -1, 0});
init(seg[idx].l, s, (s+e)/2); init(seg[idx].r, (s+e)/2+1, e);
}
void Update(int x, int y){
update(0, 0, SZ-1, x, y);
}
void update(int idx, int s, int e, int x, int y){
seg[idx].sum+=y;
if(s==e) return;
if(x<=(s+e)/2) update(seg[idx].l, s, (s+e)/2, x, y);
else update(seg[idx].r, (s+e)/2+1, e, x, y);
}
int Sum(int x, int y){
return sum(0, 0, SZ-1, x, y);
}
int sum(int idx, int s, int e, int x, int y){
if(x<=s && e<=y) return seg[idx].sum;
else if(x>e || y<s) return 0;
return sum(seg[idx].l, s, (s+e)/2, x, y) + sum(seg[idx].r, (s+e)/2+1, e, x, y);
}
};
SEG Seg;
int zero(int x){
return (x>0) ? x : -x;
}
long long count_swaps(std::vector<int> S) {
N = S.size();
Seg.Init(N);
for(int i=0; i<N; i++){
Seg.Update(i+1, 1);
if(S[i]<0){
vt[0][-S[i]].pb(i);
}else{
vt[1][S[i]].pb(i);
}
}
int idx = 1;
for(int i=1; i<=N/2; i++){
while(!vt[1][i].empty()){
r[idx] = vt[1][i].back();
l[idx] = vt[0][i].back();
S[vt[1][i].back()] = idx;
S[vt[0][i].back()] = -idx;
idx++; vt[1][i].pop_back(); vt[0][i].pop_back();
}
}
ll ans = 0;
for(int i=0; i<N; i++){
if(chk[zero(S[i])]) continue;
int now = zero(S[i]); chk[now] = true;
if(S[i]<0){
ans += (ll)Seg.Sum(i+1, r[now]-1);
Seg.Update(r[now], -1);
}else{
ans += (ll)Seg.Sum(i+1, l[now]-1);
Seg.Update(l[now], -1);
ans++;
}
}
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... |