제출 #202545

#제출 시각아이디문제언어결과실행 시간메모리
202545batmend_khArranging Shoes (IOI19_shoes)C++14
10 / 100
108 ms135160 KiB
#include "shoes.h" #include<bits/stdc++.h> using namespace std; const int N = 100005; queue <int> pos[2 * N]; bool staying[2 * N]; long long ans; int tree[4 * N]; void build_tree(int l, int r, int k); void change_val(int l, int r, int pos, int k); int find_sum(int l, int r, int ll, int rr, int k); long long count_swaps(std::vector<int> s) { memset(staying, 1, sizeof staying); int n2 = s.size(); build_tree(0, n2 - 1, 1); for (int i = 0; i < n2; i++) { int val = N + s[i]; pos[val].push(i); } for (int i = 0; i < n2; i++) { if (staying[i] == 0) continue; pos[N + s[i]].pop(); int srchval = N - s[i]; int ind = pos[srchval].front(); pos[srchval].pop(); ans += find_sum(0, n2 - 1, i + 1, ind - 1, 1); if (s[i] > 0) ans++; staying[ind] = 0; change_val(0, n2, ind, 1); } return ans; } void change_val(int l, int r, int pos, int k) { if (l == pos && r == pos) { tree[k] = 0; return; } if (pos < l || r < pos) return; int mid = (l + r) / 2; change_val(l, mid, pos, 2*k); change_val(mid + 1, r, pos, 2*k+1); tree[k] = tree[2*k] + tree[2*k+1]; } void build_tree(int l, int r, int k) { if (l == r) { tree[k] = staying[l]; return; } int mid = (l + r) / 2; build_tree(l, mid, 2 * k); build_tree(mid + 1, r, 2 * k + 1); tree[k] = tree[2*k] + tree[2*k + 1]; } int find_sum(int l, int r, int ll, int rr, int k) { if (rr < ll) return 0; if (r < ll || rr < l) return 0; if (ll <= l && r <= rr) return tree[k]; int mid = (l + r) / 2; int ff = find_sum(l, mid, ll, rr, 2*k); int ss = find_sum(mid + 1, r, ll, rr, 2*k + 1); return ff + ss; }
#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...