Submission #715822

#TimeUsernameProblemLanguageResultExecution timeMemory
715822TheSahibArranging Shoes (IOI19_shoes)C++17
100 / 100
389 ms31308 KiB
#include <bits/stdc++.h> #define ll long long #define i128 __int128_t #define pii pair<int, int> #define oo 1e9 using namespace std; int n; vector<int> tree; void update(int pos, int val){ while(pos <= n){ tree[pos] += val; pos += (pos & (-pos)); } } int ask(int l, int r){ if(l > r) return 0; if(l != 1) return ask(1, r) - ask(1, l - 1); int ans = 0; while(r > 0){ ans += tree[r]; r -= (r & (-r)); } return ans; } ll count_swaps(vector<int> S){ n = S.size(); tree.resize(n + 5); fill(tree.begin(), tree.end(), 0); map<int, set<int>> mp; for (int i = 0; i < n; i++) { update(i + 1, 1); mp[S[i]].insert(i + 1); } ll ans = 0; for (int i = 1; i <= n; i++) { if(ask(i, i) == 0){ continue; } set<int> &s = mp[-S[i - 1]]; auto itr = s.upper_bound(i); if(S[i - 1] >= 0){ ans += ask(i, (*itr) - 1); } else{ ans += ask(i + 1, (*itr) - 1); } update(i, 1); update(*itr, -1); s.erase(itr); mp[S[i]].erase(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...