답안 #401696

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
401696 2021-05-10T17:37:28 Z rama_pang Floppy (RMI20_floppy) C++17
100 / 100
143 ms 11156 KB
#include <bits/stdc++.h>
using namespace std;

#include "floppy.h"

void read_array(int subtask_id, const vector<int> &v) {
  string save;
  vector<int> st;
  for (int i = 0; i < int(v.size()); i++) {
    while (!st.empty() && v[st.back()] <= v[i]) {
      st.pop_back();
      save.push_back('0');
    }
    st.emplace_back(i);
    save.push_back('1');
  }
  while (!st.empty()) {
    st.pop_back();
    save.push_back('0');
  }
  assert(save.size() == 2 * v.size());
  return save_to_floppy(save);
}

vector<int> solve_queries(int subtask_id, int N,
                          const string &bits,
                          const vector<int> &a, const vector<int> &b) {

  assert(bits.size() == 2 * N);
  vector<int> st;
  vector<int> L(N);
  for (int i = 0, s = 0; s < 2 * N; s++) {
    if (bits[s] == '0') {
      st.pop_back();
    } else if (bits[s] == '1') {
      L[i] = st.empty() ? -1 : st.back();
      st.emplace_back(i++);
    }
  }

  const int Z = 1;
  int root = -1;
  vector<int> prv(N + 1, -1);
  vector<array<int, 2>> adj(N, {-1, -1});
  for (int i = N - 1; i >= 0; i--) {
    if (prv[Z + L[i]] == -1) {
      if (L[i] == -1) {
        root = i;
      } else {
        adj[L[i]][1] = i;
      }
    } else {
      adj[prv[Z + L[i]]][0] = i;
    }
    prv[Z + L[i]] = i;
  }

  assert(root != -1);
  vector<pair<int, int>> tree(N * 2);

  function<void(int, int)> Dfs = [&](int i, int v) -> void {
    if (i == -1) return;
    tree[N + i] = {v, i};
    Dfs(adj[i][0], v - 1);
    Dfs(adj[i][1], v - 1);
  };
  Dfs(root, N);

  for (int i = N - 1; i > 0; i--) {
    tree[i] = max(tree[i * 2], tree[i * 2 + 1]);
  }

  vector<int> ans;
  for (int i = 0; i < int(a.size()); i++) {
    int l = a[i], r = b[i];
    pair<int, int> res = {0, 0};
    for (l += N, r += N + 1; l < r; l /= 2, r /= 2) {
      if (l & 1) res = max(res, tree[l++]);
      if (r & 1) res = max(res, tree[--r]);
    }
    ans.emplace_back(res.second);
  }

  return ans;
}

Compilation message

In file included from /usr/include/c++/10/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
                 from floppy.cpp:1:
floppy.cpp: In function 'std::vector<int> solve_queries(int, int, const string&, const std::vector<int>&, const std::vector<int>&)':
floppy.cpp:29:22: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   29 |   assert(bits.size() == 2 * N);
      |          ~~~~~~~~~~~~^~~~~~~~
stub.cpp: In function 'void run2()':
stub.cpp:101:30: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  101 |     if (query_answers.size() != M) {
      |         ~~~~~~~~~~~~~~~~~~~~~^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 500 KB Output is correct
3 Correct 3 ms 760 KB Output is correct
4 Correct 3 ms 760 KB Output is correct
5 Correct 3 ms 640 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 29 ms 2740 KB Output is correct
2 Correct 28 ms 2680 KB Output is correct
3 Correct 30 ms 3140 KB Output is correct
4 Correct 28 ms 2920 KB Output is correct
5 Correct 28 ms 2860 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 114 ms 9224 KB Output is correct
2 Correct 112 ms 9192 KB Output is correct
3 Correct 110 ms 11156 KB Output is correct
4 Correct 111 ms 10188 KB Output is correct
5 Correct 143 ms 9208 KB Output is correct