제출 #938918

#제출 시각아이디문제언어결과실행 시간메모리
938918AriadnaArranging Shoes (IOI19_shoes)C++14
10 / 100
0 ms432 KiB
#include "shoes.h" #include <bits/stdc++.h> #define ll long long using namespace std; struct Segtree { int n; vector<int> st; Segtree(int N) { n = N; st = vector<int>(4*n, 0); } void change(int p, int l, int r, int i) { if (l == r) st[p] = 1-st[p]; else { int m = (l+r)/2; if (i <= m) change(2*p, l, m, i); else change(2*p+1, m+1, r, i); st[p] = st[2*p] + st[2*p+1]; } } int sum(int p, int l, int r, int i, int j) { if (i > j) return 0; if (l == i && r == j) return st[p]; int m = (l+r)/2; return sum(2*p, l, m, i, min(m, j)) + sum(2*p+1, m+1, r, max(m+1, i), j); } void change(int i) { change(1, 0, n-1, i); } int sum(int i, int j) { return sum(1, 0, n-1, i, j); } }; ll count_swaps(vector<int> s) { int n = (int)s.size(); Segtree St(n); ll ans = 0; map<int, int> pos; for (int i = 0; i < n; ++i) { if (s[i] < 0) { if (pos.find(-s[i]) != pos.end()) { s[i] *= -1; ans += i - pos[s[i]]; St.change(pos[s[i]]); ans -= St.sum(pos[s[i]], i); pos.erase(s[i]); } else { pos[s[i]] = i; St.change(i); } } else { if (pos.find(-s[i]) != pos.end()) { s[i] *= -1; ans += i - pos[s[i]] - 1; St.change(pos[s[i]]); ans -= St.sum(pos[s[i]], i); pos.erase(s[i]); } else { pos[s[i]] = i; St.change(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...