제출 #284543

#제출 시각아이디문제언어결과실행 시간메모리
284543ScarletSArranging Shoes (IOI19_shoes)C++17
100 / 100
151 ms17016 KiB
#include <bits/stdc++.h> #define ll long long #define sz(x) (int)(x).size() using namespace std; int segTree[1000000]; void update(int i, int cL, int cR, int r) { if (cR<=r) { ++segTree[i]; return; } if (r<cL) return; int m=cL+((cR-cL)>>1); update((i<<1)+1,cL,m,r); update((i<<1)+2,m+1,cR,r); } int query(int i, int cL, int cR, int x) { if (x<cL||cR<x) return 0; if (cL==cR) return segTree[i]; int m=cL+((cR-cL)>>1); return segTree[i]+query((i<<1)+1,cL,m,x)+query((i<<1)+2,m+1,cR,x); } vector<int> neg[100001]; vector<int> pos[100001]; ll count_swaps(vector<int> a) { int n=sz(a),c=-1; ll ans=0; for (int i=0;i<n;++i) { if (a[i]<0) neg[-a[i]].push_back(i); else pos[a[i]].push_back(i); } n>>=1; pair<int,int> p[n]; for (int i=1;i<100001;++i) for (int j=0;j<sz(pos[i]);++j) p[++c]={min(neg[i][j],pos[i][j]),max(neg[i][j],pos[i][j])}; sort(p,p+n); for (int i=0;i<n;++i) { if (a[p[i].first]<0) { //cout<<p[i].first<<" "<<p[i].second<<"\n"; //cout<<query(0,0,200000,p[i].first)<<" + "<<p[i].first<<" - "<<((i<<1))<<"\n"; ans+=query(0,0,200000,p[i].first)+p[i].first-(i<<1); update(0,0,200000,p[i].first); //cout<<query(0,0,200000,p[i].second)<<" + "<<p[i].second<<" - "<<((i<<1)+1)<<"\n"; ans+=query(0,0,200000,p[i].second)+p[i].second-(i<<1)-1; update(0,0,200000,p[i].second); //cout<<ans<<"\n"; } else { //cout<<p[i].first<<" "<<p[i].second<<"\n"; //cout<<query(0,0,200000,p[i].first)<<" + "<<p[i].first<<" - "<<((i<<1))<<"\n"; ans+=query(0,0,200000,p[i].second)+p[i].second-(i<<1); update(0,0,200000,p[i].second); //cout<<query(0,0,200000,p[i].second)<<" + "<<p[i].second<<" - "<<((i<<1)+1)<<"\n"; ans+=query(0,0,200000,p[i].first)+p[i].first-(i<<1)-1; update(0,0,200000,p[i].first); //cout<<ans<<"\n"; } } return ans; } /**int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin>>n; vector<int> a((n<<1)); for (int i=0;i<(n<<1);++i) cin>>a[i]; cout<<count_swaps(a); return 0; }**/
#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...