# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
852549 | 2023-09-22T04:56:31 Z | LucaDantas | Swap Swap Sort (CCO21_day1problem1) | C++17 | 27 ms | 128092 KB |
#include <bits/stdc++.h> using namespace std; #define int long long constexpr int B = 1, maxn = 5010; vector<int> pos[maxn]; int a[maxn], id[maxn], cnt; long long resp[maxn/B+10][maxn]; struct BIT { int bit[maxn]; void upd(int x) { for(; x > 0; x-=x&-x) ++bit[x]; } int query(int x) { int ans = 0; for(; x < maxn; x+=x&-x) ans += bit[x]; return ans; } } bit; long long inversoes(int n) { long long ans = 0; for(int i = 1; i <= n; i++) { ans += bit.query(a[i]+1); bit.upd(a[i]); } return ans; } int32_t main() { int n, k, q; scanf("%lld %lld %lld", &n, &k, &q); for(int i = 1; i <= n; i++) scanf("%lld", a+i), pos[a[i]].push_back(i); for(int cor = 1; cor <= k; cor++) { if(pos[cor].size() >= B) { id[cor] = ++cnt; int qtd = 0; for(int i = n; i >= 1; i--) { if(a[i] == cor) ++qtd; else resp[cnt][a[i]] += qtd; } } } long long ans = inversoes(n); vector<int> perm(k); iota(perm.begin(), perm.end(), 1); while(q--) { auto get = [&](int x, int y) -> long long { if(pos[x].size() >= B) return resp[id[x]][y]; if(pos[y].size() >= B) return 1ll * pos[x].size() * pos[y].size() - resp[id[y]][x]; long long aq = 0; for(int i = (int)(pos[y].size())-1, ptr = (int)(pos[x].size())-1; i >= 0; i--) { int now = 0; for(; ptr >= 0 && pos[x][ptr] > pos[y][i]; ptr--) ++now; aq += now; } return aq; }; int index; scanf("%lld", &index); --index; ans -= get(perm[index], perm[index+1]); swap(perm[index], perm[index+1]); ans += get(perm[index], perm[index+1]); printf("%lld\n", ans); } }
Compilation message
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 344 KB | Output is correct |
2 | Correct | 2 ms | 604 KB | Output is correct |
3 | Correct | 2 ms | 600 KB | Output is correct |
4 | Correct | 2 ms | 2652 KB | Output is correct |
5 | Correct | 2 ms | 4696 KB | Output is correct |
6 | Correct | 5 ms | 21080 KB | Output is correct |
7 | Correct | 9 ms | 39768 KB | Output is correct |
8 | Correct | 27 ms | 128092 KB | Output is correct |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Runtime error | 5 ms | 856 KB | Execution killed with signal 11 |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Runtime error | 5 ms | 856 KB | Execution killed with signal 11 |
2 | Halted | 0 ms | 0 KB | - |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
1 | Correct | 1 ms | 344 KB | Output is correct |
2 | Correct | 2 ms | 604 KB | Output is correct |
3 | Correct | 2 ms | 600 KB | Output is correct |
4 | Correct | 2 ms | 2652 KB | Output is correct |
5 | Correct | 2 ms | 4696 KB | Output is correct |
6 | Correct | 5 ms | 21080 KB | Output is correct |
7 | Correct | 9 ms | 39768 KB | Output is correct |
8 | Correct | 27 ms | 128092 KB | Output is correct |
9 | Runtime error | 5 ms | 856 KB | Execution killed with signal 11 |
10 | Halted | 0 ms | 0 KB | - |