이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "shoes.h"
#include <bits/stdc++.h>
#define ll long long
#define fast cin.tie(0);cout.tie(0);
#define order ios::sync_with_stdio(0);ios_base::sync_with_stdio(0);
#define pb push_back
#define ln 2*x+1
#define rn 2*x+2
#define m (l+r)/2
using namespace std;
int n;
vector<int>v[200009][2];
int cass(int x)
{
if(x>0)return 1;
else return 0;
}
ll seg[1000009];
void bld(int x,int l,int r)
{
if(l==r)
{
seg[x]=1;
return;
}
bld(ln,l,m);
bld(rn,m+1,r);
seg[x]=seg[ln]+seg[rn];
}
void upd(int x,int l,int r,int w)
{
//if(w==0)cout<<x<<" "<<l<<" "<<r<<"\n";
if(l==r)
{
seg[x]=0;
return;
}
if(m>=w)upd(ln,l,m,w);
else upd(rn,m+1,r,w);
seg[x]=seg[ln]+seg[rn];
}
ll qu(int x,int l,int r,int b,int e)
{
if(b>e)return 0;
if(b<=l&&r<=e)return seg[x];
ll ret=0;
if(m>=b)ret+=qu(ln,l,m,b,e);
if(m<e)ret+=qu(rn,m+1,r,b,e);
return ret;
}
int vis[200009];
long long count_swaps(vector<int> s)
{
n=s.size()/2;
int en=2*n-1;
for(int i=n*2-1;i>=0;i--)
{
v[abs(s[i])][cass(s[i])].push_back(i);
}
bld(0,0,en);
ll ans=0;
for(int i=0;i<2*n;i++)
{
if(vis[i])continue;
int x=s[i];
if(x<0)x*=-1;
else ans++;
int v1=v[x][0].back(),v2=v[x][1].back();
//cout<<v1<<" "<<v2<<"\n";
v[x][0].pop_back();
v[x][1].pop_back();
ans+=qu(0,0,en,min(v1,v2)+1,max(v1,v2)-1);
upd(0,0,en,v1);upd(0,0,en,v2);
vis[v1]=1;
vis[v2]=1;
//cout<<seg[0]<<" ";
}
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... |