Submission #852524

#TimeUsernameProblemLanguageResultExecution timeMemory
852524LucaDantasSwap Swap Sort (CCO21_day1problem1)C++17
5 / 25
273 ms412240 KiB
#include <bits/stdc++.h> using namespace std; constexpr int B = 100, maxn = 1e5+10; vector<int> pos[maxn]; int a[maxn], id[maxn], cnt; long long resp[maxn/B+5][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; } int main() { int n, k, q; scanf("%d %d %d", &n, &k, &q); for(int i = 1; i <= n; i++) scanf("%d", 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("%d", &index); --index; ans += 1ll * pos[perm[index]].size() * pos[perm[index+1]].size() - 2*get(perm[index], perm[index+1]); swap(perm[index], perm[index+1]); printf("%lld\n", ans); } }

Compilation message (stderr)

Main.cpp: In function 'int main()':
Main.cpp:36:23: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   36 |     int n, k, q; scanf("%d %d %d", &n, &k, &q);
      |                  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:38:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   38 |         scanf("%d", a+i), pos[a[i]].push_back(i);
      |         ~~~~~^~~~~~~~~~~
Main.cpp:69:25: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   69 |         int index; scanf("%d", &index); --index;
      |                    ~~~~~^~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...