제출 #1330732

#제출 시각아이디문제언어결과실행 시간메모리
1330732sodbayrArranging Shoes (IOI19_shoes)C++20
0 / 100
0 ms344 KiB
#include "shoes.h"
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define ff first
#define ss second
#define endl "\n"
using namespace std;
	ll tree[800005];bool bb[200005];
void update(ll node,ll l,ll r,ll id,ll val){
    if(id<l || id>r) return;
    if(l==r){
        tree[node]=val;
        return;
    }
    ll mid=(l+r)/2;
    update(node/2,l,mid,id,val);
    update(node/2+1,mid+1,r,id,val);
    tree[node]=tree[node/2]+tree[node/2+1];
}
ll query(ll node,ll l,ll r,ll L,ll R){
    if(R<l || r<L) return 0;
    if(L<=l && r<=R) return tree[node];
    ll mid=(l+r)/2;
    return query(node/2,l,mid,L,R)+query(node/2+1,mid+1,r,L,R);
}
ll count_swaps(vector<int> s){
    ll n=s.size(),ans=0;
    vector<pair<ll,ll>> v;
    for(int i=0;i<n;i++){
        update(1,1,n,i+1,1);
        v.pb({s[i],i+1});
    }
    sort(v.begin(),v.end());
    for(int i=0;i<n;i++){
        if(bb[i]) continue;
        ll x=s[i];
        ll y=-x;
        pair<ll,ll> p={y,0};
        ll o=lower_bound(v.begin(),v.end(),p)-v.begin();
        while(o<n && (v[o].ff!=y || v[o].ss==0)){
			o++;
    	}
		ll e=i+1,e1=v[o].ss;
        if(e1<e) swap(e,e1);
        ll g=query(1,1,n,e+1,e1-1);
        if(x>0) g++;
        ans+=g;
        update(1,1,n,e,0);
        update(1,1,n,e1,0);
        bb[i]=1;
        v[o].ss=0;
    }
    return ans;
}
#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...