제출 #386929

#제출 시각아이디문제언어결과실행 시간메모리
386929danielcm585Arranging Shoes (IOI19_shoes)C++14
10 / 100
5 ms4972 KiB
#include "shoes.h"

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int N = 2e5;
const int ADD = 1e5;
int n;
int pr[N+2], bit[N+2];
vector<int> ls[N+2];

struct node {
    node *lf, *rg;
    int l, r, val, lz;

    node(int x, int y): l(x), r(y), lz(0) {}
    
    void build() {
        if (l == r) {
            val = l;
            return;
        }
        int mid = (l+r)/2;
        lf = new node(l,mid); lf->build();
        rg = new node(mid+1,r); rg->build();
    }

    void apply(int v) {
        val += v;
        lz += v;
    }

    void prop() {
        if (lz == 0) return;
        if (l < r) {
            lf->apply(lz);
            rg->apply(lz);
        }
        lz = 0;
    }

    void update(int x, int y, int v) {
        if (l > y || r < x) return;
        if (l >= x && r <= y) {
            apply(v);
            return;
        }
        prop();
        lf->update(x,y,v); rg->update(x,y,v);
    }

    int query(int x) {
        if (l == r) return val;
        prop();
        int mid = (l+r)/2;
        if (x <= mid) return lf->query(x);
        return rg->query(x);
    }
    
} *st;

ll count_swaps(vector<int> s) {
    n = s.size();
    for (int i = 0; i < n; i++) {
        if (!ls[-s[i]+ADD].empty()) {
            int x = ls[-s[i]+ADD].back();
            ls[-s[i]+ADD].pop_back();
            pr[x] = i;
        }
        else ls[s[i]+ADD].push_back(i);
    }
    st = new node(0,n-1); st->build();
    ll ans = 0;
    for (int i = 0; i < n; i++) {
        if (!pr[i]) continue;
        int dis = st->query(pr[i])-i-1;
        ans += dis + (s[i] > 0);
        st->update(i+1,pr[i]-1,1);
    }
    return ans;
}

/*
3
-2 2 2 -2 -2 2
*/
#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...