Submission #115346

#TimeUsernameProblemLanguageResultExecution timeMemory
115346model_codeMatching (CEOI11_mat)C++17
Compilation error
0 ms0 KiB
/* Veryfing solution for the task MAT (Matching)
 * Author: Jakub Radoszewski
 * Date: July 2011
 * Time complexity: O(nlogn + m)
 */

#include <iostream>
#include <vector>
#include <utility>
using namespace std;

#define MAX 1000000
#define INFTY 100000000

/* p1 is the original pattern from the problem statement, while
 * p is given from left to right, like the text t. */
int n, m;
int p1[MAX + 1], p[MAX + 1], t[MAX + 1];

void read()
{
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
  {
    cin >> p1[i];
  }
  for (int i = 1; i <= n; i++)
    p[p1[i]] = i;
  for (int i = 1; i <= m; i++)
    cin >> t[i];
}


/* Segment trees, efficient implementation. */
pair<int, int> min_tree[1 << 21], max_tree[1 << 21];
int size;

void init_trees()
{
  size = 1;
  while (size <= n)
    size *= 2;
  for (int i = 0; i <= 2 * size - 1; i++)
  {
    min_tree[i] = make_pair(INFTY, -1);
    max_tree[i] = make_pair(-1, -1);
  }
}

int max_query(int v)
{
  int ind = size + v;
  pair<int, int> res = make_pair(-1, -1);
  while (ind != 1)
  {
    if (ind % 2 == 1 && max_tree[ind - 1].first > res.first)
      res = max_tree[ind - 1];
    ind /= 2;
  }
  return res.second;
}

int min_query(int v)
{
  int ind = size + v;
  pair<int, int> res = make_pair(INFTY, -1);
  while (ind != 1)
  {
    if (ind % 2 == 0 && min_tree[ind + 1].first < res.first)
      res = min_tree[ind + 1];
    ind /= 2;
  }
  return res.second;
}

void min_insert(pair<int, int> p)
{
  int ind = size + p.first;
  min_tree[ind] = p;
  while (ind != 1)
  {
    ind /= 2;
    if (p.first < min_tree[ind].first)
      min_tree[ind] = p;
  }
}

void max_insert(pair<int, int> p)
{
  int ind = size + p.first;
  max_tree[ind] = p;
  while (ind != 1)
  {
    ind /= 2;
    if (p.first > max_tree[ind].first)
      max_tree[ind] = p;
  }
}


int g[MAX + 1], h[MAX + 1];

/* Computes the g and h functions, as defined in the solution description,
 * using a segment tree. */
void compute_gh()
{
  init_trees();
  for (int i = 1; i <= n; i++)
  {
    g[i] = max_query(p[i]);
    h[i] = min_query(p[i]);
    min_insert(make_pair(p[i], i));
    max_insert(make_pair(p[i], i));
  }
}


int f[MAX + 1];

inline bool matches(int pos, int pos2)
{
  if (g[pos] != -1 && p[pos2] <= p[pos2 - (pos - g[pos])])
    return false;
  if (h[pos] != -1 && p[pos2] >= p[pos2 - (pos - h[pos])])
    return false;
  return true;
}

inline bool matches_t(int pos, int pos2)
{
  if (g[pos] != -1 && t[pos2] <= t[pos2 - (pos - g[pos])])
    return false;
  if (h[pos] != -1 && t[pos2] >= t[pos2 - (pos - h[pos])])
    return false;
  return true;
}

/* Computes the failure function of the pattern. */
void compute_f()
{
  f[0] = f[1] = 0;
  int last = 0;
  for (int i = 2; i <= n; i++)
  {
    f[i] = last;
    while (last && !matches(last + 1, i))
      last = f[last];
    if (matches(last + 1, i))
      last++;
    f[i] = last;
  }
}


vector<int> result;

void matching()
{
  int last = 0;
  for (int i = 1; i <= m; i++)
  {
    while (last && !matches_t(last + 1, i))
      last = f[last];
    if (matches_t(last + 1, i))
      last++;
    if (last == n)
    {
      result.push_back(i - n + 1);
      last = f[last];
    }
  }
}

void write()
{
  cout << result.size() << endl;
  for (int i = 0; i < (int)result.size(); i++)
  {
    cout << result[i];
    if (i < (int)result.size() - 1)
      cout << " ";
  }
  cout << endl;
}

int main()
{
  ios_base::sync_with_stdio(0);
  read();
  compute_gh();
  compute_f();
  matching();
  write();
  return 0;
}

Compilation message (stderr)

mat.cpp: In function 'void init_trees()':
mat.cpp:40:3: error: reference to 'size' is ambiguous
   size = 1;
   ^~~~
mat.cpp:36:5: note: candidates are: int size
 int size;
     ^~~~
In file included from /usr/include/c++/7/string:51:0,
                 from /usr/include/c++/7/bits/locale_classes.h:40,
                 from /usr/include/c++/7/bits/ios_base.h:41,
                 from /usr/include/c++/7/ios:42,
                 from /usr/include/c++/7/ostream:38,
                 from /usr/include/c++/7/iostream:39,
                 from mat.cpp:7:
/usr/include/c++/7/bits/range_access.h:252:5: note:                 template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])
     size(const _Tp (&/*__array*/)[_Nm]) noexcept
     ^~~~
/usr/include/c++/7/bits/range_access.h:242:5: note:                 template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)
     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
     ^~~~
mat.cpp:41:10: error: reference to 'size' is ambiguous
   while (size <= n)
          ^~~~
mat.cpp:36:5: note: candidates are: int size
 int size;
     ^~~~
In file included from /usr/include/c++/7/string:51:0,
                 from /usr/include/c++/7/bits/locale_classes.h:40,
                 from /usr/include/c++/7/bits/ios_base.h:41,
                 from /usr/include/c++/7/ios:42,
                 from /usr/include/c++/7/ostream:38,
                 from /usr/include/c++/7/iostream:39,
                 from mat.cpp:7:
/usr/include/c++/7/bits/range_access.h:252:5: note:                 template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])
     size(const _Tp (&/*__array*/)[_Nm]) noexcept
     ^~~~
/usr/include/c++/7/bits/range_access.h:242:5: note:                 template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)
     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
     ^~~~
mat.cpp:42:5: error: reference to 'size' is ambiguous
     size *= 2;
     ^~~~
mat.cpp:36:5: note: candidates are: int size
 int size;
     ^~~~
In file included from /usr/include/c++/7/string:51:0,
                 from /usr/include/c++/7/bits/locale_classes.h:40,
                 from /usr/include/c++/7/bits/ios_base.h:41,
                 from /usr/include/c++/7/ios:42,
                 from /usr/include/c++/7/ostream:38,
                 from /usr/include/c++/7/iostream:39,
                 from mat.cpp:7:
/usr/include/c++/7/bits/range_access.h:252:5: note:                 template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])
     size(const _Tp (&/*__array*/)[_Nm]) noexcept
     ^~~~
/usr/include/c++/7/bits/range_access.h:242:5: note:                 template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)
     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
     ^~~~
mat.cpp:43:28: error: reference to 'size' is ambiguous
   for (int i = 0; i <= 2 * size - 1; i++)
                            ^~~~
mat.cpp:36:5: note: candidates are: int size
 int size;
     ^~~~
In file included from /usr/include/c++/7/string:51:0,
                 from /usr/include/c++/7/bits/locale_classes.h:40,
                 from /usr/include/c++/7/bits/ios_base.h:41,
                 from /usr/include/c++/7/ios:42,
                 from /usr/include/c++/7/ostream:38,
                 from /usr/include/c++/7/iostream:39,
                 from mat.cpp:7:
/usr/include/c++/7/bits/range_access.h:252:5: note:                 template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])
     size(const _Tp (&/*__array*/)[_Nm]) noexcept
     ^~~~
/usr/include/c++/7/bits/range_access.h:242:5: note:                 template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)
     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
     ^~~~
mat.cpp: In function 'int max_query(int)':
mat.cpp:52:13: error: reference to 'size' is ambiguous
   int ind = size + v;
             ^~~~
mat.cpp:36:5: note: candidates are: int size
 int size;
     ^~~~
In file included from /usr/include/c++/7/string:51:0,
                 from /usr/include/c++/7/bits/locale_classes.h:40,
                 from /usr/include/c++/7/bits/ios_base.h:41,
                 from /usr/include/c++/7/ios:42,
                 from /usr/include/c++/7/ostream:38,
                 from /usr/include/c++/7/iostream:39,
                 from mat.cpp:7:
/usr/include/c++/7/bits/range_access.h:252:5: note:                 template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])
     size(const _Tp (&/*__array*/)[_Nm]) noexcept
     ^~~~
/usr/include/c++/7/bits/range_access.h:242:5: note:                 template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)
     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
     ^~~~
mat.cpp: In function 'int min_query(int)':
mat.cpp:65:13: error: reference to 'size' is ambiguous
   int ind = size + v;
             ^~~~
mat.cpp:36:5: note: candidates are: int size
 int size;
     ^~~~
In file included from /usr/include/c++/7/string:51:0,
                 from /usr/include/c++/7/bits/locale_classes.h:40,
                 from /usr/include/c++/7/bits/ios_base.h:41,
                 from /usr/include/c++/7/ios:42,
                 from /usr/include/c++/7/ostream:38,
                 from /usr/include/c++/7/iostream:39,
                 from mat.cpp:7:
/usr/include/c++/7/bits/range_access.h:252:5: note:                 template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])
     size(const _Tp (&/*__array*/)[_Nm]) noexcept
     ^~~~
/usr/include/c++/7/bits/range_access.h:242:5: note:                 template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)
     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
     ^~~~
mat.cpp: In function 'void min_insert(std::pair<int, int>)':
mat.cpp:78:13: error: reference to 'size' is ambiguous
   int ind = size + p.first;
             ^~~~
mat.cpp:36:5: note: candidates are: int size
 int size;
     ^~~~
In file included from /usr/include/c++/7/string:51:0,
                 from /usr/include/c++/7/bits/locale_classes.h:40,
                 from /usr/include/c++/7/bits/ios_base.h:41,
                 from /usr/include/c++/7/ios:42,
                 from /usr/include/c++/7/ostream:38,
                 from /usr/include/c++/7/iostream:39,
                 from mat.cpp:7:
/usr/include/c++/7/bits/range_access.h:252:5: note:                 template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])
     size(const _Tp (&/*__array*/)[_Nm]) noexcept
     ^~~~
/usr/include/c++/7/bits/range_access.h:242:5: note:                 template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)
     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
     ^~~~
mat.cpp: In function 'void max_insert(std::pair<int, int>)':
mat.cpp:90:13: error: reference to 'size' is ambiguous
   int ind = size + p.first;
             ^~~~
mat.cpp:36:5: note: candidates are: int size
 int size;
     ^~~~
In file included from /usr/include/c++/7/string:51:0,
                 from /usr/include/c++/7/bits/locale_classes.h:40,
                 from /usr/include/c++/7/bits/ios_base.h:41,
                 from /usr/include/c++/7/ios:42,
                 from /usr/include/c++/7/ostream:38,
                 from /usr/include/c++/7/iostream:39,
                 from mat.cpp:7:
/usr/include/c++/7/bits/range_access.h:252:5: note:                 template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])
     size(const _Tp (&/*__array*/)[_Nm]) noexcept
     ^~~~
/usr/include/c++/7/bits/range_access.h:242:5: note:                 template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)
     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
     ^~~~