Submission #1030785

#TimeUsernameProblemLanguageResultExecution timeMemory
1030785ZicrusArranging Shoes (IOI19_shoes)C++17
100 / 100
785 ms15196 KiB
#include <bits/stdc++.h> #include "shoes.h" using namespace std; typedef long long ll; ll n; ll pow2; vector<ll> seg; void segSet(ll id, ll val) { id += pow2; seg[id] = val; id /= 2; while (id) { seg[id] = seg[2*id] + seg[2*id+1]; id /= 2; } } ll segGet(ll low, ll high) { low += pow2; high += pow2; ll ans = 0; while (low <= high) { if (low & 1) ans += seg[low++]; if (!(high & 1)) ans += seg[high--]; low /= 2; high /= 2; } return ans; } ll count_swaps(vector<int> a) { n = a.size() / 2; vector<vector<ll>> seenQNeg(n+10); vector<vector<ll>> seenQPos(n+10); ll res = 0; pow2 = 2ll << (ll)ceil(log2(n)); seg = vector<ll>(2*pow2); for (int i = 0; i < a.size(); i++) { auto &q = a[i] < 0 ? seenQNeg[-a[i]] : seenQPos[a[i]]; auto &pairQ = a[i] > 0 ? seenQNeg[a[i]] : seenQPos[-a[i]]; if (pairQ.empty()) { q.insert(q.begin(), i); } else { ll id = pairQ.back(); pairQ.pop_back(); if (a[i] < 0) { res += abs(id - i) + segGet(id, i); segSet(i, -1); segSet(id, 1); } else { res += abs(id - i) - 1 + segGet(id, i); segSet(i, -1); segSet(id, 1); } } } return res; }

Compilation message (stderr)

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