Submission #306033

#TimeUsernameProblemLanguageResultExecution timeMemory
306033talant117408Arranging Shoes (IOI19_shoes)C++17
100 / 100
259 ms18928 KiB
#include "shoes.h" #include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 2e5+7; ll tree[N*4], lz[N*4]; void push(int v, int l, int r){ if(l > r) return; if(lz[v]){ tree[v] += ll(r-l+1)*lz[v]; if(l != r){ lz[v<<1] += lz[v]; lz[(v<<1)+1] += lz[v]; } lz[v] = 0; } } ll get(int v, int l, int r, int ql, int qr){ push(v, l, r); if(l > qr || r < ql) return 0; if(ql <= l && r <= qr) return tree[v]; int mid = (l+r)>>1; return get(v<<1, l, mid, ql, qr)+get((v<<1)+1, mid+1, r, ql, qr); } void update(int v, int l, int r, int ql, int qr){ push(v, l, r); if(l > qr || r < ql) return; if(ql <= l && r <= qr){ tree[v] += ll(r-l+1); if(l != r){ lz[v<<1]++; lz[(v<<1)+1]++; } return; } int mid = (l+r)>>1; update(v<<1, l, mid, ql, qr); update((v<<1)+1, mid+1, r, ql, qr); tree[v] = tree[v<<1]+tree[(v<<1)+1]; } /* ll count_swaps(vector <int> s); int main() { int n; assert(1 == scanf("%d", &n)); vector<int> S(2 * n); for (int i = 0; i < 2 * n; i++) assert(1 == scanf("%d", &S[i])); fclose(stdin); long long result = count_swaps(S); printf("%lld\n", result); fclose(stdout); return 0; } */ ll count_swaps(vector<int> s){ int n = s.size(), i; vector <pair <int, int>> order[N]; for(i = 0; i < n; i++){ order[abs(s[i])].emplace_back(make_pair(s[i], i+1)); } ll ans = 0; vector <pair<int, int>> qu; for(i = 1; i <= n/2; i++){ sort(order[abs(i)].begin(), order[abs(i)].end()); for(int j = 0; j < (int)order[i].size()/2; j++){ int l = order[i][j].second; int r = order[i][j+(int)order[i].size()/2].second; if(l > r){ swap(l, r); } else{ ans--; } qu.push_back(make_pair(l, r)); } } sort(qu.begin(), qu.end()); for(auto to : qu){ int l = to.first, r = to.second; ans += (r+get(1, 1, n, r, r))-(l+get(1, 1, n, l, l)); if(l+1 != r){ update(1, 1, n, l+1, r-1); } } return ans; }
#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...