Cod sursa(job #2601107)

Utilizator FrostfireMagirescu Tudor Frostfire Data 13 aprilie 2020 19:04:58
Problema Curcubeu Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.5 kb
#include <fstream>
#include <iostream>

using namespace std;

ifstream f("curcubeu.in");

int n, a[1000010], b[1000010], c[1000010], sol[1000010], tata[1000010], nxt[1000010];

class OutParser {
private:
    FILE *fout;
    char *buff;
    int sp;

    void write_ch(char ch) {
        if (sp == 50000) {
            fwrite(buff, 1, 50000, fout);
            sp = 0;
            buff[sp++] = ch;
        } else {
            buff[sp++] = ch;
        }
    }


public:
    OutParser(const char* name) {
        fout = fopen(name, "w");
        buff = new char[50000]();
        sp = 0;
    }
    ~OutParser() {
        fwrite(buff, 1, sp, fout);
        fclose(fout);
    }

    OutParser& operator << (int vu32) {
        if (vu32 <= 9) {
            write_ch(vu32 + '0');
        } else {
            (*this) << (vu32 / 10);
            write_ch(vu32 % 10 + '0');
        }
        return *this;
    }

    OutParser& operator << (long long vu64) {
        if (vu64 <= 9) {
            write_ch(vu64 + '0');
        } else {
            (*this) << (vu64 / 10);
            write_ch(vu64 % 10 + '0');
        }
        return *this;
    }

    OutParser& operator << (char ch) {
        write_ch(ch);
        return *this;
    }
    OutParser& operator << (const char *ch) {
        while (*ch) {
            write_ch(*ch);
            ++ch;
        }
        return *this;
    }
};

int findDaddy(int x)
{   int r = x, y = x;
    while(r != tata[r]) r = tata[r];
    while(x != tata[x])
        {   x = tata[x];
            tata[y] = r;
            y = x;
        }
    return r;
}

int main()
{
    OutParser g("curcubeu.out");

    f >> n >> a[1] >> b[1] >> c[1];
    pair <int, int> x;
    for(int i=1; i<n; i++) tata[i] = i, nxt[i] = i+1;
    for(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;
    for(int i=n-1; i; i--)
        {   x.first = a[i];
            x.second = b[i];
            if(x.first > x.second) swap(x.first, x.second);
            x.first = max(1, x.first);
            x.first = findDaddy(x.first);
            for(int j=x.first; j<=x.second; j=nxt[j])
                {   if(!sol[j]) sol[j] = c[i];
                    tata[j] = x.first;
                    if(nxt[j] != j+1 && j != x.second) tata[j+1] = x.first;
                }
            nxt[x.first] = max(nxt[x.first], x.second+1);
        }
    for(int i=1; i<n; i++) g << sol[i] << '\n';
    return 0;
}