Submission #670046

#TimeUsernameProblemLanguageResultExecution timeMemory
670046illyakrArranging Shoes (IOI19_shoes)C++14
50 / 100
1094 ms15068 KiB
#include <bits/stdc++.h> #include "shoes.h" using namespace std; int t[401010]; int tAdd[401010]; void build(int v, int vl, int vr) { if (vl == vr) { t[v] = vl; return; } int vm = (vl + vr) / 2; build(2 * v, vl, vm); build(2 * v + 1, vm + 1, vr); } void push(int v, int vl, int vr) { if (tAdd[v] == 0)return; t[v] += tAdd[v]; if (vl != vr) { tAdd[2 * v] += tAdd[v]; tAdd[2 * v + 1] += tAdd[v]; } tAdd[v] = 0; return; } void upd(int v, int vl, int vr, int l, int r, int val) { push(v, vl, vr); if (l <= vl && vr <= r) { tAdd[v] += val; push(v, vl, vr); return; } if (r < vl || vr < l)return; int vm = (vl + vr) / 2; upd(2 * v, vl, vm, l, r, val); upd(2 * v + 1, vm + 1, vr, l, r, val); } int gt(int v, int vl, int vr, int pos) { push(v, vl, vr); if (vl == vr) return t[v]; int vm = (vl + vr) / 2; if (pos <= vm) return gt(2 * v, vl, vm, pos); return gt(2 * v + 1, vm + 1, vr, pos); } bool used[201010]; vector<int> have[201010]; int p[201010]; long long count_swaps(vector<int> s) { int n = s.size(); build(1, 0, n - 1); for (int i = 0; i < n; i++) have[s[i] + 100000].push_back(i); for (int i = 0; i < n; i++) sort(have[s[i] + 100000].begin(), have[s[i] + 100000].end()); long long ans = 0; for (int i = 0; i < n; i++) { if (used[i])continue; long long val = -s[i] + 100000; long long sc = have[val][p[val]++]; p[s[i] + 100000]++; long long cur = gt(1, 0, n - 1, i); long long id = gt(1, 0, n - 1, sc); // cout << i << ", " << cur << " << " << sc << ", " << id << endl; used[sc] = true; ans += (id - cur); if (s[i] < 0) ans--; upd(1, 0, n - 1, i, sc, 1); } return ans; } /** 2 2 1 -1 -2 3 -2 2 2 -2 -2 2 */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...