이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#include<shoes.h>
#define rep(i,x,n) for(int i=(x);i<(n);i++)
#define FOR(i,n) rep(i,0,n)
#define v(i) vector< i >
#define ll long long
#define pp push_back
using namespace std;
const ll inf=1LL<<62LL;
const ll mod=1e9+7;
const ll maxn=1000200;
ll st[4*maxn];
int n;
ll query(int ql,int qr,int l=0,int r=n-1,int inx=1)
{
if(ql<=l&&r<=qr)
return st[inx];
if(ql>r||l>qr)
return 0;
int mid=l+(r-l)/2;
return query(ql,qr,mid+1,r,inx*2+1)+query(ql,qr,l,mid,inx*2);
}
void update(int pos,int l=0,int r=n-1,int inx=1)
{
if(l==r)
{
st[inx]=0;
return ;
}
int mid=l+(r-l)/2;
if(pos<=mid)
update(pos,l,mid,inx*2);
else
update(pos,mid+1,r,inx*2+1);
st[inx]=st[inx*2]+st[inx*2+1];
}
int build(int l=0,int r=n-1,int inx=1)
{
if(l==r)
return st[inx]=1;
int mid=l+(r-l)/2;
return st[inx]=build(l,mid,inx*2)+build(mid+1,r,inx*2+1);
}
ll count_swaps(v(int)a)
{
ll ans=0;
n=a.size();
build();
v(int)l[maxn],r[maxn];
for(int i=n-1;i>=0;i--)
if(a[i]<0)
l[-a[i]].pp(i);
else
r[a[i]].pp(i);
v(bool)vis(maxn,0);
FOR(i,n)
{
if(vis[i])
continue;
if(a[i]>0)
ans++;
a[i]=abs(a[i]);
int left=l[a[i]].back();
l[a[i]].pop_back();
int right=r[a[i]].back();
r[a[i]].pop_back();
if(left>right)
swap(left,right);
ans+=(query(left,right)-2);
update(left);
update(right);
}
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... |