제출 #298967

#제출 시각아이디문제언어결과실행 시간메모리
298967TMJNArranging Shoes (IOI19_shoes)C++17
10 / 100
72 ms27256 KiB
#include <bits/stdc++.h> #include "shoes.h" using namespace std; int N,tree[1<<18]; vector<int>vl[100001],vr[100001]; void add(int l,int r){ l+=(1<<17); r+=(1<<17); while(l<r){ tree[l]++; tree[r]++; l=(l+1)/2; r=(r-1)/2; } if(l==r)tree[l]++; } int calc(int x){ int a=0; x+=(1<<17); while(x){ a+=tree[x]; x/=2; } return a; } long long count_swaps(vector<int>v) { N=v.size(); long long res=0; for(int i=N-1;i>=0;i--){ if(v[i]<0){ vl[-v[i]].push_back(i); } else{ vr[v[i]].push_back(i); } } for(int i=0;i<N;i++){ if(v[i]<0){ if(vl[-v[i]].back()!=i)continue; vl[-v[i]].pop_back(); res+=vr[-v[i]].back()-i-1; res-=calc(i)-calc(vr[-v[i]].back()); add(i,vr[-v[i]].back()); vr[-v[i]].pop_back(); } else{ if(vr[v[i]].back()!=i)continue; vr[v[i]].pop_back(); res+=vl[v[i]].back()-i; res-=calc(i)-calc(vl[v[i]].back()); add(i,vl[v[i]].back()); vl[v[i]].pop_back(); } } return res; }
#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...