Submission #748182

#TimeUsernameProblemLanguageResultExecution timeMemory
748182MinaRagy06Swap Swap Sort (CCO21_day1problem1)C++17
11 / 25
5016 ms176272 KiB
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace std; using namespace __gnu_pbds; typedef int64_t ll; tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag, tree_order_statistics_node_update> invs; set<pair<int, int>> s; vector<pair<int, int>> pairs; vector<pair<int, int>> check[100'005]; int val[2'000'005], q[1'000'005], a[100'005], p[100'005]; vector<int> frq[100'005]; int main() { ios_base::sync_with_stdio(0), cin.tie(0); int n, k, m; cin >> n >> k >> m; for (int i = 0; i < k; i++) { p[i] = i + 1; } ll ans = 0; for (int i = 0; i < n; i++) { cin >> a[i]; frq[a[i]].push_back(i); ans += invs.size() - invs.order_of_key({a[i] + 1, 0}); invs.insert({a[i], i}); } for (int i = 0; i < m; i++) { cin >> q[i]; q[i]--; s.insert({p[q[i]], p[q[i] + 1]}); swap(p[q[i]], p[q[i] + 1]); s.insert({p[q[i]], p[q[i] + 1]}); } for (auto i : s) pairs.push_back(i); for (int i = 0; i < pairs.size(); i++) { check[pairs[i].first].push_back({pairs[i].second, i}); } assert(pairs.size() <= 2'000'005); for (int i = 1; i <= k; i++) { for (auto &[j, idx] : check[i]) { if (frq[i].empty() || frq[j].empty()) continue; if (frq[i].size() <= frq[j].size()) { int prv = 0; for (auto l : frq[i]) { val[idx] += frq[j].size() - (prv = lower_bound(frq[j].begin() + prv, frq[j].end(), l) - frq[j].begin()); } } else { int prv = 0; for (auto l : frq[j]) { val[idx] += (prv = lower_bound(frq[i].begin() + prv, frq[i].end(), l) - frq[i].begin()); } } } } for (int i = 0; i < k; i++) { p[i] = i + 1; } for (int i = 0; i < m; i++) { ans += val[lower_bound(pairs.begin(), pairs.end(), make_pair(p[q[i]], p[q[i] + 1])) - pairs.begin()]; swap(p[q[i]], p[q[i] + 1]); ans -= val[lower_bound(pairs.begin(), pairs.end(), make_pair(p[q[i]], p[q[i] + 1])) - pairs.begin()]; cout << ans << '\n'; } return 0; }

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:37:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   37 |     for (int i = 0; i < pairs.size(); i++) {
      |                     ~~^~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...