Cod sursa(job #2224826)

Utilizator linia_intaiConstantinescu Iordache Ciobanu Noi cei din linia intai linia_intai Data 25 iulie 2018 11:53:40
Problema Curcubeu Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.4 kb
#include <cstdio>
#include <algorithm>

using namespace std;

int a[1000002], b[1000002], c[1000002];
int t[1000002], nxt[1000002], h[1000002];
int ans[1000002];

inline int f(int x) {
  int y = x;
  while (x != t[x])
    x = t[x];
  while (y != t[y]) {
    int aux = t[y];
    t[y] = x;
    y = aux;
  }
  return x;
}

inline void u(int x, int y) {
  x = f(x);
  y = f(y);
  if (x == y)
    return ;
  if (h[y] > h[x])
    swap(x, y);
  t[y] = x;
  if (h[x] == h[y]) ++h[x];
  nxt[x] = max(nxt[x], nxt[y]);
}

int get(int x) {
  if (ans[x + 1] == 0)
    return x + 1;
  return nxt[f(x)];
}

int main() {
  freopen("curcubeu.in", "r", stdin);
  freopen("curcubeu.out", "w", stdout);

  int n, x, y;
  scanf("%d", &n);
  scanf("%d%d%d", &a[1], &b[1], &c[1]);
  t[1] = 1;
  nxt[1] = 2;
  for (register int i = 2; i < n; ++i) {
    a[i] = 1LL * a[i - 1] * i % n;
    b[i] = 1LL * b[i - 1] * i % n;
    c[i] = 1LL * c[i - 1] * i % n;
    t[i] = i;
    nxt[i] = i + 1;
  }
  for (register int i = n - 1; i >= 1; --i) {
    x = min(a[i], b[i]);
    y = a[i] ^ b[i] ^ x;
    if (ans[x] != 0)
      x = get(x);
    for (register int j = x; j <= y; j = get(j)) {
      ans[j] = c[i];
      if (ans[j - 1] != 0)
        u(j - 1, j);
      if (ans[j + 1] != 0)
        u(j + 1, j);
    }
  }

  for (register int i = 1; i < n; ++i)
    printf("%d\n", ans[i]);

  return 0;
}