Submission #301858

#TimeUsernameProblemLanguageResultExecution timeMemory
301858lohachoArranging Shoes (IOI19_shoes)C++14
100 / 100
536 ms36072 KiB
#include "shoes.h"
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
const int NS = (int)2e5 + 4;
map < int, vector < int > > mp;
int chk[NS];
map < int, int > pos;
struct fenwick{
    vector < int > fenw;
    int N;
    fenwick(){}
    fenwick(int n):N(n){
        fenw.resize(N);
    }
    void push(int x, int val){
        for(int i = x; i < N; i += (i & -i)){
            fenw[i] += val;
        }
    }
    int get(int x){
        int rv = 0;
        for(int i = x; i; i -= (i & -i)){
            rv += fenw[i];
        }
        return rv;
    }
}tree(NS);

long long count_swaps(std::vector<int> s) {
    int N = (int)s.size();
    s.push_back(0);
    for(int i = N; i >= 1; --i){
        s[i] = s[i - 1];
    }
    for(int i = 1; i <= N; ++i){
        mp[s[i]].push_back(i);
        if(i > 1) tree.push(i, 1);
    }
    LL ans = 0;
    for(int i = 1; i <= N; ++i){
        if(chk[i]){
            continue;
        }
        ans += tree.get(i) + tree.get(mp[-s[i]][pos[-s[i]]]) - (s[i] < 0);
        tree.push(i + 1, -1), tree.push(mp[-s[i]][pos[-s[i]]] + 1, -1);
        chk[mp[-s[i]][pos[-s[i]]]] = 1;
        ++pos[s[i]], ++pos[-s[i]];
    }
	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...