제출 #143725

#제출 시각아이디문제언어결과실행 시간메모리
143725shdutArranging Shoes (IOI19_shoes)C++14
10 / 100
42 ms10588 KiB
#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, b[N]; int c[N]; long long count_swaps(std::vector<int> s) { int n = s.size(); build(1, 0, n-1); rep(i, 1, n+1)b[i].clear(); a.clear(); rep(i, 0, n){ if(s[i] < 0)a.pb(i); else b[s[i]].pb(i); } long long ans = 0; rep(i, 1, n+1)c[i] = 0; for(auto &x : a){ int i = -s[x]; int k = b[i][c[i]++], 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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...