Submission #583516

#TimeUsernameProblemLanguageResultExecution timeMemory
583516lcjArranging Shoes (IOI19_shoes)C++17
100 / 100
348 ms77512 KiB
#include <bits/stdc++.h>
#include "shoes.h"

#define LSOne(s) ((s) & -(s))
using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

struct FenwickTree {
    vector<ll> ft;
    FenwickTree(int n) : ft(n+1) {}
    ll rsq(int i) {
        ll su = 0;
        for (; i > 0; i -= LSOne(i)) {
            su += ft[i];
        }
        return su;
    }
    void update(int i, ll dv) {
        for (; i < (int)ft.size(); i += LSOne(i)) {
            ft[i] += dv;
        }
    }
};

ll count_swaps(std::vector<int> s) {
    FenwickTree ft(s.size());
    map<int, queue<int>> open;
    ll csum = 0;
    vector<int> mypartner(s.size());
    for (int i = 0; i < s.size(); i++)
    {
        if (open.find(-s[i]) != open.end() && !open[-s[i]].empty()) {
            int j = open[-s[i]].front();
            open[-s[i]].pop();
            mypartner[j] = i;
            mypartner[i] = j;
            csum += (s[i] < 0);
        }
        else {
            if (!open.count(s[i])) {
                open[s[i]] = {};
            }
            open[s[i]].push(i);
        }
    }
    for (int i = 0; i < s.size(); i++)
    {
        int j = mypartner[i];
        if (j < i) continue;
        csum += (j+ft.rsq(j+1))-(i+ft.rsq(i+1))-1;
        ft.update(1, 1);
        ft.update(j+1, -1);
    }
    return csum;
}

Compilation message (stderr)

shoes.cpp: In function 'll count_swaps(std::vector<int>)':
shoes.cpp:33:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   33 |     for (int i = 0; i < s.size(); i++)
      |                     ~~^~~~~~~~~~
shoes.cpp:49:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |     for (int i = 0; i < s.size(); i++)
      |                     ~~^~~~~~~~~~
#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...