답안 #418740

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
418740 2021-06-05T19:38:57 Z Hegdahl 코끼리 (Dancing Elephants) (IOI11_elephants) C++17
0 / 100
1 ms 332 KB
#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
#include "elephants.h"
#define ar array
using namespace std;
using ll = long long;

const int mxN = 15e4;
int n, l, R, a[mxN], idxs[mxN], where[mxN];

struct Chunk {
  int size;
  vector<ll> xs;
  vector<ar<ll, 2>> dp;

  Chunk(vector<ll> &&_xs) : size((int)_xs.size()), xs(move(_xs)) {
    recalc();
  }

  void recalc() {
    dp.resize(size);
    for (int i = size-1; i >= 0; --i) {
      int j = lower_bound(xs.begin(), xs.end(), xs[i]+l) - xs.begin();
      if (j == size) dp[i] = {1, xs[i]+l};
      else dp[i] = {dp[j][0]+1, dp[j][1]};
    }
  }

  void insert(int x) {
    ++size;
    auto it = upper_bound(xs.begin(), xs.end(), x);
    xs.insert(it, x);
    recalc();
  }

  void erase(int x) {
    --size;
    auto it = lower_bound(xs.begin(), xs.end(), x);
    assert(it != xs.end());
    xs.erase(it);
    recalc();
  }

  ar<ll, 2> qry(int x) {
    int j = lower_bound(xs.begin(), xs.end(), x) - xs.begin();
    if (j == size) return {0, x};
    return dp[j];
  }
};

vector<Chunk> chunks;

void recalc() {
  iota(idxs, idxs+n, 0);
  sort(idxs, idxs+n, [&](int i, int j){ return a[i] < a[j]; });

  chunks.clear();
  for (int i0 = 0; i0 < n; i0 += R) {
    vector<ll> xs;
    for (int i = i0; i < min(i0+R, n); ++i) {
      xs.push_back(a[idxs[i]]);
      where[idxs[i]] = chunks.size();
    }
    chunks.emplace_back(move(xs));
  }
}

void init(int N, int L, int X[]) {
  n = N, l = L+1;
  for (int i = 0; i < n; ++i)
    a[i] = X[i];

  R = sqrt(N);

  recalc();
}

int solve() {
  ll x = -1e9-10, ans = 0;

  for (int c = 0; c < chunks.size(); ++c) {
    auto [used, new_x] = chunks[c].qry(x);
    ans += used;
    x = new_x;
  }

  return ans;
}

int update(int i, int y) {
  static int update_cnt = 0;
  if (++update_cnt%(2*R) == 0) {
    a[i] = y;
    recalc();
  } else {
    int c = where[i];
    chunks[c].erase(a[i]);

    a[i] = y;

    for (c = 1; c < chunks.size(); ++c)
      if (chunks[c].size && chunks[c].xs[0] > a[i]) break;
    --c;
    chunks[c].insert(a[i]);
    where[i] = c;
  }

  return solve();
}

Compilation message

elephants.cpp: In function 'int solve()':
elephants.cpp:81:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Chunk>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   81 |   for (int c = 0; c < chunks.size(); ++c) {
      |                   ~~^~~~~~~~~~~~~~~
elephants.cpp: In function 'int update(int, int)':
elephants.cpp:101:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Chunk>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  101 |     for (c = 1; c < chunks.size(); ++c)
      |                 ~~^~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 332 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 332 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 332 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 332 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 1 ms 332 KB Output isn't correct
2 Halted 0 ms 0 KB -