제출 #1011036

#제출 시각아이디문제언어결과실행 시간메모리
1011036rythm_of_knightArranging Shoes (IOI19_shoes)C++14
10 / 100
34 ms27472 KiB
#include "shoes.h" #include <bits/stdc++.h> #define ll long long using namespace std; struct Fenwick{ vector<int> tr; int sz; Fenwick(int N){ tr.resize(N+1); sz=N; } void add(ll i, ll x){ for (; i<=sz; i+=(-i&i)) tr[i]+=x; } ll get(ll i){ ll sum=0; for (; i; i-=(i&-i)) sum+=tr[i]; return sum; } }; long long count_swaps(vector<int> s) { int n = s.size(), x; ll ans = 0; // sgt st; // st.init(n); Fenwick tr(n+2); vector <set <int>> p(n + 1, set <int>()), m(n + 1, set <int>()); for (int i = 0; i < n; i++) { x = s[i]; if (x > 0) { if (!m[x].empty()) { auto it = m[x].begin(); int l = *it + tr.get(*it+1), r = i; ans += r - l - 1; m[x].erase(it); tr.add(l+1, 1); if (r != n - 1) tr.add(r+2, -1); } else { p[x].insert(i); } } else { x = -x; if (!p[x].empty()) { auto it = p[x].begin(); int l = *it + tr.get(*it+1), r = i; ans += r - l; p[x].erase(it); tr.add(l+1, 1); if (r != n - 1) tr.add(r+2, -1); } else { m[x].insert(i); } } } 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...