Submission #514065

#TimeUsernameProblemLanguageResultExecution timeMemory
514065PoPularPlusPlusArranging Shoes (IOI19_shoes)C++17
100 / 100
86 ms21828 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define ull unsigned long long #define pb(e) push_back(e) #define sv(a) sort(a.begin(),a.end()) #define sa(a,n) sort(a,a+n) #define mp(a,b) make_pair(a,b) #define vf first #define vs second #define ar array #define all(x) x.begin(),x.end() const int inf = 0x3f3f3f3f; const int mod = 1000000007; const double PI=3.14159265358979323846264338327950288419716939937510582097494459230; bool remender(ll a , ll b){return a%b;} #include "shoes.h" struct Fen { vector<ll> tree; void init(int n){ tree.assign(n + 1 , 0LL); } void set(int i , ll val , int n){ i++; while(i <= n){ tree[i] += val; i += (i & (-i)); } } ll sum(int i){ i++; ll ans = 0; while(i > 0){ ans += tree[i]; i -= (i & (-i)); } return ans; } void build(vector<int>& a){ int n = a.size(); for(int i = 0; i < n; i++){ set(i , a[i] , n); } } }; ll count_swaps(vector<int> arr){ int n = arr.size(); vector<int> v[n + 1] , v1[n + 1]; int pos1[n] , pos[n]; for(int i = 0; i < n; i++){ if(arr[i] < 0){ v1[-arr[i]].pb(i); pos1[i] = v1[-arr[i]].size()-1; } else { v[arr[i]].pb(i); pos[i] = v[arr[i]].size() - 1; } } Fen ft; ft.init(n + 3); ll ans = 0; ll cur = 0; for(int i = 0; i < n; i++){ if(arr[i] < 0 && v[-arr[i]][pos1[i]] > i){ int p = pos1[i]; ans += ((ft.sum(n) - ft.sum(i)) + i) - cur; ft.set(i,1LL , n + 2); cur++; ans += (ft.sum(n) - ft.sum(v[-arr[i]][p])) + v[-arr[i]][p] - cur; ft.set(v[-arr[i]][p],1LL , n + 2); cur++; } else if(arr[i] > 0 && v1[arr[i]][pos[i]] > i){ int p = v1[arr[i]][pos[i]]; ans += ((ft.sum(n) - ft.sum(p)) + p) - cur; ft.set(p,1LL , n + 2); cur++; ans += (ft.sum(n) - ft.sum(i)) + i - cur; ft.set(i , 1LL , n + 2); cur++; } } 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...