이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#include "shoes.h"
#define ll long long
#define pb push_back
#define x first
#define y second
using namespace std;
int n;
vector<int> a,bit;
vector<vector<int>> p1,p2;
void update(int x, int val)
{
for(;x<=n;x+=x&-x)
bit[x]+=val;
}
ll pre(int x)
{
ll ret=0;
for(;x>0;x-=x&-x)
ret+=bit[x];
return ret;
}
ll sm(int l, int r)
{
return pre(r)-pre(l-1);
}
ll count_swaps(vector<int> S)
{
ll ans=0;
n=S.size();
bit.resize(n+1);
a.pb(0);
for(int i=1;i<=n;i++) a.pb(1), update(i,1);
p1.resize(n/2+1), p2=p1;
for(int i=0;i<n;i++)
{
if(S[i]>0) p1[S[i]].pb(i+1);
else p2[-S[i]].pb(i+1);
}
vector<int> pt(n/2+1,0);
for(int i=1;i<=n;i++)
{
if(a[i]==0) continue;
if(S[i-1]>0)
{
ans+=sm(i+1,p2[S[i-1]][pt[S[i-1]]]-1)+1;
a[p2[S[i-1]][pt[S[i-1]]]]=0;
update(i,-1), update(p2[S[i-1]][pt[S[i-1]]],-1);
pt[S[i-1]]++;
}
else
{
ans+=sm(i+1,p1[-S[i-1]][pt[-S[i-1]]]-1);
a[p1[-S[i-1]][pt[-S[i-1]]]]=0;
update(i,-1), update(p1[-S[i-1]][pt[-S[i-1]]],-1);
pt[-S[i-1]]++;
}
}
return ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |