Submission #769453

#TimeUsernameProblemLanguageResultExecution timeMemory
769453nninArranging Shoes (IOI19_shoes)C++14
100 / 100
120 ms138612 KiB
#include<bits/stdc++.h>
using namespace std;

queue<int> posi[200002];
int n;

int fenwick[200002];
void update(int x, int k) {
    while(x<=n*2) {
        fenwick[x] += k;
        x += (x & -x);
    }
}
int query(int x) {
    int ret = 0;
    while(x>0) {
        ret += fenwick[x];
        x -= (x & -x);
    }
    return ret;
}

long long count_swaps(vector<int> S) {
    n = S.size()/2;
    long long ans = 0;
    for(int i=0;i<2*n;i++) {
        if(!posi[n-S[i]].empty()) {
            ans += i-query(posi[n-S[i]].front())+(S[i]<0);
            update(posi[n-S[i]].front(), 1);
            posi[n-S[i]].pop();
        } else {
            posi[n+S[i]].push(i+1);
            update(i+1, 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...