제출 #361866

#제출 시각아이디문제언어결과실행 시간메모리
361866Matteo_VerzArranging Shoes (IOI19_shoes)C++17
100 / 100
120 ms9692 KiB
#include "shoes.h"
#include <bits/stdc++.h>

using namespace std;
const int N = 1e5;

class Fenwick {
  private:
    vector <int> a;

    int lsb(int i) {
      return (i & (-i));
    }

  public:
    Fenwick(int n) {
      a.resize(1 + n);
    }

    void Update(int pos, int val) {
      for(; pos < a.size(); pos += lsb(pos))
        a[pos] += val;
    }

    int Query(int pos) {
      int ans(0);
      for(; pos > 0; pos -= lsb(pos))
        ans += a[pos];

      return ans;
    }
};

int rightpos[1 + 2 * N];
long long count_swaps(vector<int> s) {
  set <pair <int, int>> myset;
  s.insert(s.begin(), 0);
  int n = s.size() - 1;
  Fenwick aib(n);

  for(int i = 1; i <= n; i++) {
    set <pair <int, int>> :: iterator it = myset.lower_bound(make_pair(-s[i], 0));

    if(it != myset.end() && (*it).first == -s[i]) {
      rightpos[(*it).second] = i;
      rightpos[i] = i;
      myset.erase(it);
    } else myset.insert(make_pair(s[i], i));
    aib.Update(i, 1);
  }

  long long ans(0);
  for(int i = 1; i <= n; i++) {
    int pereche = rightpos[i];
    if(pereche != i) {
      aib.Update(i, -1); aib.Update(pereche, -1);

      if(s[i] < 0) ans += 1LL * aib.Query(pereche);
      else ans += 1LL * (aib.Query(pereche) + 1);
    }
  }

  return ans;
}

컴파일 시 표준 에러 (stderr) 메시지

shoes.cpp: In member function 'void Fenwick::Update(int, int)':
shoes.cpp:21:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   21 |       for(; pos < a.size(); pos += lsb(pos))
      |             ~~~~^~~~~~~~~~
#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...