Cod sursa(job #3225224)

Utilizator matei__bBenchea Matei matei__b Data 17 aprilie 2024 09:52:34
Problema Atac Scor 20
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.63 kb
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ld long double
#define chad char
#define mod 1'000'000'007
#define dim 100005
#define lim 1000000
#define BASE 67
#define NMAX 1505
#define FOR(i,a,b) for(int i=(a); i<=(b); i++)
#define pli pair<ll,int>
#define pil pair<int,ll>
#define piii pair<int,pair<int,int> >
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define mp make_pair
#define nr_biti __builtin_popcount
using namespace std;

ifstream fin("atac.in");
ofstream fout("atac.out");

int n,q,p;
vector<pii> g[dim];

int xcurr,ycurr,a,b,c,d;

int st[20][dim],dp[20][dim];
int h[dim];

void dfs(int nod,int daddy=0)
{
    st[0][nod]=daddy;
    h[nod]=1+h[daddy];

    for(auto it:g[nod])
    {
        if(it.first==daddy)
            continue;

        dp[0][it.first]=it.second;
        dfs(it.first,nod);
    }
}

void pre()
{
    for(int i=1; (1<<i)<=n; i++)
        for(int j=1; j<=n; j++)
        {
            st[i][j]=st[i-1][st[i-1][j]];
            dp[i][j]=min(dp[i-1][j],dp[i-1][st[i-1][j]]);
        }
}

int bl(int nod,int niv)
{
    int z=0;

    while(niv)
    {
        if(niv&1)
            nod=st[z][nod];

        z++;
        niv/=2;
    }

    return nod;
}

int lca(int x,int y)
{
    if(h[x]>h[y])
        swap(x,y);

    int dif=h[y]-h[x];

    y=bl(y,dif);

    if(x==y)
        return x;

    for(int i=19; i>=0; i--)
    {
        if(st[i][x]!=st[i][y])
        {
            x=st[i][x];
            y=st[i][y];
        }
    }

    return st[0][x];
}

int cauta_minim(int x,int y)
{
    int mn=2e9;

    int dif=h[x]-h[y];
    int z=0;

    while(dif)
    {
        if(dif&1)
        {
            mn=min(mn,dp[z][x]);
            x=st[z][x];
        }

        z++;
        dif/=2;
    }

    return mn;
}

int main()
{
    ios_base::sync_with_stdio(false);
    fin.tie(nullptr);
    fout.tie(nullptr);

    fin >> n >> q >> p;

    for(int i=2; i<=n; i++)
    {
        int x,y;

        fin >> x >> y;

        g[x].pb(mp(i,y));
        g[i].pb(mp(x,y));
    }

    fin >> xcurr >> ycurr >> a >> b >> c >> d;

    dfs(1);
    pre();

    int pos=q-p+1;

    for(int i=1; i<=q; i++)
    {
        int u=lca(xcurr,ycurr);

        int ans=min(cauta_minim(xcurr,u),cauta_minim(ycurr,u));

        //cout << xcurr << " " << ycurr << " ";
        //cout << u << " " << ans << "\n";

        if(i>=pos) 
            fout << ans << "\n";

        xcurr=((xcurr*a+ycurr*b)%n)+1;
        ycurr=((ycurr*c+ans*d)%n)+1;
    }

    return 0;
}