Cod sursa(job #3379231)

Utilizator mbazacliuMihnea Gabriel Bazacliu mbazacliu Data 29 noiembrie 2026 20:04:18
Problema Curcubeu Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.34 kb
#include <fstream>
#include <vector>
#include <queue>
using namespace std;

ifstream cin("curcubeu.in");
ofstream cout("curcubeu.out");

struct DSU {
    vector<int> t;
    vector<int> s;
    vector<int> e;

    DSU(int n): t(n), s(n), e(n) {
        for (int i = 0; i < n; i++){
            t[i] = i;
            s[i] = 1;
            e[i] = i+1;
        }
    }

    int root(int x){
        return t[x] == x ? x : t[x] = root(t[x]);
    }

    void merge(int x, int y){
        x = root(x);
        y = root(y);

        if (x == y) return;
        if (s[x] > s[y]) swap(x, y);

        t[y] = x;
        s[x] += s[y];
        e[x] = max(e[x], e[y]);
    }
};

int main(){
    int n;
    cin >> n;

    vector<int> a(n-1), b(n-1), c(n-1);
    cin >> a[0] >> b[0] >> c[0];

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

    DSU dsu(n-1);

    vector<int> v(n-1);
    for (int i = n-2; i >= 0; i--){
        if (a[i] > b[i]) swap(a[i], b[i]);
        for (int j = v[a[i]-1] ? dsu.e[dsu.root(a[i]-1)] : a[i]-1; j < b[i]; j = dsu.e[dsu.root(j)]){
            v[j] = c[i];
            if (j && v[j-1]) dsu.merge(j, j-1);
            if (j < n-2 && v[j+1]) dsu.merge(j, j+1);
        }
    }

    for (int x : v) cout << x << '\n';

    return 0;
}