제출 #847644

#제출 시각아이디문제언어결과실행 시간메모리
847644manhlinh1501Arranging Shoes (IOI19_shoes)C++17
10 / 100
1 ms348 KiB
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;

#define lch id << 1
#define rch id << 1 | 1

const int N = 2e5 + 5;

int bit[N];
int n;

void update(int x, int k) {
    for(; x <= n; x += (x & -x))
        bit[x] += k;
}

int calc(int x) {
    int res = 0;

    for(; x; x -= (x & -x))
        res += bit[x];

    return res;
}

int get(int x) {
    return calc(x) - calc(x - 1);
}

i64 count_swaps(vector<int> a) {
    i64 ans = 0;
    map<int, deque<int>> res;
    n = a.size();

    for(int i = 0; i < n; i++) {
        if(res[-a[i]].empty())
            res[a[i]].emplace_back(i);

        else {
            int l = res[-a[i]].front() + 1;
            res[-a[i]].pop_front();
            int r = i + 1;
            ans += (r - l) - (get(r) - get(l)) - (a[i] > 0);
            update(l, 1);
            update(r, -1);
        }
    }

    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...