(UPD: 2024-12-04 14:48 UTC) Judge is not working due to Cloudflare incident. (URL) We can do nothing about it, sorry. After the incident is resolved, we will grade all submissions.

Submission #635261

#TimeUsernameProblemLanguageResultExecution timeMemory
635261jhnah917Swap Swap Sort (CCO21_day1problem1)C++14
0 / 25
157 ms19648 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; int N, K, Q, A[101010], B[101010], T[101010]; vector<int> P[101010]; ll R; void Add(int x, int v){ for(x+=3; x<101010; x+=x&-x) T[x] += v; } int Get(int x){ int ret = 0; for(x+=3; x; x-=x&-x) ret += T[x]; return ret; } map<pair<int,int>, ll> Cache; ll Swap(int &u, int &v){ auto it = Cache.lower_bound(make_pair(u, v)); if(it != Cache.end() && it->first == make_pair(u, v)) return it->second; ll res = 0; if(P[u].size() < P[v].size()){ for(auto i : P[u]){ int cnt = P[v].end() - lower_bound(P[v].begin(), P[v].end(), i); res += cnt - (P[v].size() - cnt); } } else{ for(auto i : P[v]){ int cnt = lower_bound(P[u].begin(), P[u].end(), i) - P[u].begin(); res += cnt - (P[u].size() - cnt); } } Cache.emplace_hint(it, make_pair(u, v), res); swap(u, v); return res; } int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> N >> K >> Q; iota(B+1, B+K+1, 1); for(int i=1; i<=N; i++) cin >> A[i], P[A[i]].push_back(i); for(int i=N; i>=1; i--) R += Get(A[i]-1), Add(A[i], 1); for(int q=1; q<=Q; q++){ int t; cin >> t; R += Swap(B[t], B[t+1]); cout << R << "\n"; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...