Submission #379626

#TimeUsernameProblemLanguageResultExecution timeMemory
379626SuhaibSawalha1Arranging Shoes (IOI19_shoes)C++14
100 / 100
129 ms19652 KiB
#include "shoes.h" #include <bits/stdc++.h> using namespace std; vector<int> fen; int n; void update (int i, int x) { for (++i; i <= n; i += i&-i) { fen[i] += x; } } int query (int i) { int ans = 0; for (++i; i; i -= i&-i) { ans += fen[i]; } return ans; } int query (int i, int j) { return query(j) - query(i - 1); } long long count_swaps(vector<int> a) { n = a.size(); vector<vector<int>> p(n + 1), pn(n + 1); for (int i = n - 1; ~i; --i) { if (a[i] > 0) { p[a[i]].push_back(i); } else { pn[-a[i]].push_back(i); } } fen.resize(n + 1); long long ans = 0; for (int i = 0; i < n; ++i) { if (a[i] < 0 && pn[-a[i]].back() == i) { int newi = i - query(i), newp = p[-a[i]].back() - query(p[-a[i]].back()); ans += abs(newi - newp) - (newi < newp); update(i + 1, -1); update(p[-a[i]].back(), 1); p[-a[i]].pop_back(); pn[-a[i]].pop_back(); } else if (a[i] > 0 && p[a[i]].back() == i) { int newi = i - query(i), newp = pn[a[i]].back() - query(pn[a[i]].back()); ans += abs(newp - newi) - (newp < newi); if (i) { update(i - 1, -1); } update(pn[a[i]].back(), 1); pn[a[i]].pop_back(); p[a[i]].pop_back(); } } 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...