제출 #639079

#제출 시각아이디문제언어결과실행 시간메모리
639079LeonaRagingSwap Swap Sort (CCO21_day1problem1)C++14
3 / 25
204 ms33452 KiB
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define ll long long #define pb push_back #define db(val) "[" #val " = " << (val) << "] " const ll mod = 1e9 + 7; const int maxn = 1e5 + 4; const int INF = 1e9; const int block = 5000; struct seg_tree { vector<int> t; seg_tree() { t.resize(4 * maxn); } void update(int pos, int v = 1, int l = 1, int r = maxn - 1) { if (l == r) { t[v]++; return; } int m = (l + r) / 2; if (pos <= m) update(pos, 2 * v, l, m); else update(pos, 2 * v + 1, m + 1, r); t[v] = t[2 * v] + t[2 * v + 1]; } int get(int l, int r, int v = 1, int tl = 1, int tr = maxn - 1) { if (tl > r || tr < l) return 0; if (tl >= l && tr <= r) return t[v]; int m = (tl + tr) / 2; int L = get(l, r, 2 * v, tl, m); int R = get(l, r, 2 * v + 1, m + 1, tr); return L + R; } } IT; int n, k, q, a[maxn], p[maxn], sum[maxn], delta[maxn]; vector<int> pos[maxn]; vector<pair<int,int>> adj[maxn]; int main() { // ios::sync_with_stdio(0); // cin.tie(0); cout.tie(0); //freopen(".INP", "r", stdin); //freopen(".OUT", "w", stdout); cin >> n >> k >> q; int res = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; res += IT.get(a[i] + 1, n); pos[a[i]].pb(i); IT.update(a[i]); } for (int i = 1; i <= n; i++) p[i] = i; for (int i = 1; i <= q; i++) { int j; cin >> j; // clog << db(j); int x = p[j], y = p[j + 1]; if (pos[x].size() <= block && pos[y].size() <= block) { int l = 0, inv = 0; for (int r = 0; r < int(pos[x].size()); r++) { while (l + 1 < int(pos[y].size()) && pos[y][l + 1] < pos[x][r]) l++; if (l < int(pos[y].size()) && pos[y][l] < pos[x][r]) inv += l + 1; } delta[i] = -2 * inv + pos[x].size() * pos[y].size(); // if (i == 2) clog << inv << '\n'; } else { if (pos[x].size() > block) adj[x].pb({y, i}); else adj[x].pb({y, -i}); } swap(p[j], p[j + 1]); } for (int x = 1; x <= n; x++) { if (pos[x].size() <= block) continue; memset(sum, 0, sizeof sum); for (auto i : pos[x]) sum[i] = 1; for (int i = 1; i <= n; i++) sum[i] += sum[i - 1]; sort(adj[x].begin(), adj[x].end()); int i = 0; while (i < int(adj[x].size())) { int y = adj[x][i].fi, inv = pos[x].size() * pos[y].size(); for (int j : pos[y]) inv -= 2 * sum[j]; while (adj[x][i].fi == y && i < int(adj[x].size())) { if (adj[x][i].se > 0) delta[adj[x][i].se] = inv; else delta[adj[x][i].se] = -inv; i++; } } } for (int i = 1; i <= q; i++) res += delta[i], cout << res << '\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...