Cod sursa(job #2660245)

Utilizator Razvan48Capatina Razvan Nicolae Razvan48 Data 18 octombrie 2020 16:45:27
Problema Curcubeu Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.36 kb
#include <fstream>

using namespace std;

const int NMAX = 1000000;

int culoare[NMAX];
int a[NMAX];
int b[NMAX];
int c[NMAX];

int tata[1 + NMAX];
int h[NMAX];
int dr[NMAX];

int find_(int idx)
{
  if (tata[idx] == idx)
    return idx;

  tata[idx] = find_(tata[idx]);

  return tata[idx];
}

void union_(int a, int b)
{
  a = find_(a);
  b = find_(b);

  if (h[a] > h[b])
    swap(a, b);

  tata[a] = b;
  if (h[a] == h[b])
    h[b]++;
  dr[b] = max(dr[b], dr[a]);
}

void adauga(int idx)
{
  tata[idx] = idx;
  h[idx] = 1;
  dr[idx] = idx;

  if (tata[idx - 1] != 0)
  {
    union_(idx, idx - 1);
  }
  if (tata[idx + 1] != 0)
  {
    union_(idx, idx + 1);
  }
}

int main()
{
  ifstream in("curcubeu.in");
  ofstream out("curcubeu.out");
  int n;

  in >> n >> a[1] >> b[1] >> c[1];
  for (int i = 2; i <= n - 1; i++)
  {
    a[i] = (a[i - 1] * i) % n;
    b[i] = (b[i - 1] * i) % n;
    c[i] = (c[i - 1] * i) % n;
  }

  for (int i = n - 1; i >= 1; i--)
  {
    if (a[i] > b[i])
      swap(a[i], b[i]);

    for (int j = a[i]; j <= b[i]; j++)
    {
      if (tata[j] == 0)
      {
        culoare[j] = c[i];
        adauga(j);
      }
      else
      {
        j = dr[find_(j)];
      }
    }
  }

  for (int i = 1; i <= n - 1; i++)
  {
    out << culoare[i] << '\n';
  }

  return 0;

}