46822023-03-31 08:47:00gortomiLegkisebb nem oszthatócpp17Accepted 100/1001.585s75600 KiB
#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > g, par;
int tim = 0;
vector<int> l, r, d;
void dfs(int n, int p)
{
    par[n][0] = p;
    d[n] = d[p] + 1;
    l[n] = tim++;
    for(int i = 1; i <= 19; i++)
    {
        par[n][i] = par[par[n][i - 1]][i - 1];
    }
    for(auto x : g[n]) if(x != p) dfs(x, n);
    r[n] = tim++;
}
struct val
{
    int l, r, ind, lca;
};
bool comp(val a, val b)
{
    return a.l / 350 < b.l / 350 || (a.l / 350 == b.l / 350 && a.r < b.r);
}
int calc(int a, int b)
{
    if(d[a] < d[b]) swap(a, b);
    int v = d[a] - d[b];
    for(int i = 0; i <= 19; i++)
    {
        if((v >> i) & 1) a = par[a][i];
    }
    for(int i = 19; i >= 0; i--)
    {
        if(par[a][i] != par[b][i])
        {
            a = par[a][i];
            b = par[b][i];
        }
    }
    return par[a][0];
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    vector<int> prim;
    vector<bool> ip(2 * 1e6 + 1, 1);
    vector<int> pl(2 * 1e6 + 1);
    g.resize(n + 1);
    d.resize(n + 1);
    l.resize(n + 1);
    r.resize(n + 1);
    par.resize(n + 1, vector<int>(20));
    for(int i = 2; i <= 2 * 1e6; i++)
    {
        if(ip[i])
        {
            pl[i] = prim.size();
            prim.push_back(i);
            for(int j = 2 * i; j <= 2 * 1e6; j += i)
            {
                ip[j] = 0;
            }
        }
    }
    vector<int> v(n + 1, 2 * 1e6 + 1);
    for(int i = 1; i <= n; i++)
    {
        int x;
        cin >> x;
        if(ip[x]) v[i] = pl[x];
    }
    for(int i = 0; i < n - 1; i++)
    {
        int x, y;
        cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }
    dfs(1, 1);
    vector<int> path(2 * n);
    for(int i = 1; i <= n; i++)
    {
        path[l[i]] = i;
        path[r[i]] = i;
    }
    int q;
    cin >> q;
    vector<val> que(q + 1);
    que[0] = {0, -1, 0, 0};
    vector<int> ans(q);
    for(int i = 1; i <= q; i++)
    {
        int x, y;
        cin >> x >> y;
        if(l[x] < l[y] && r[x] < r[y])
        {
            que[i].l = r[x];
            que[i].r = l[y];
            que[i].lca = calc(x, y);
        }
        else if(l[x] > l[y] && r[x] > r[y])
        {
            que[i].l = r[y];
            que[i].r = l[x];
            que[i].lca = calc(x, y);
        }
        else
        {
            que[i].l = l[x];
            que[i].r = l[y];
            if(que[i].l > que[i].r) swap(que[i].l, que[i].r);
            que[i].lca = 0;
        }
        que[i].ind = i - 1;
    }
    sort(que.begin(), que.end(), comp);
    const int b = 350;
    vector<int> sq(b);
    vector<int> db(n + 1);
    vector<bool> act(n + 1);
    for(int i = 1; i <= q; i++)
    {
        for(int j = que[i - 1].l - 1; j >= que[i].l; j--)
        {
            int y = path[j];
            int x = v[y];
            if(x > n) continue;
            if(!act[y])
            {
                act[y] = 1;
                db[x]++;
                if(db[x] == 1) sq[x / b]++;
            }
            else
            {
                act[y] = 0;
                db[x]--;
                if(db[x] == 0) sq[x / b]--;
            }
        }
        for(int j = que[i - 1].r + 1; j <= que[i].r; j++)
        {
            int y = path[j];
            int x = v[y];
            if(x > n) continue;
            if(!act[y])
            {
                act[y] = 1;
                db[x]++;
                if(db[x] == 1) sq[x / b]++;
            }
            else
            {
                act[y] = 0;
                db[x]--;
                if(db[x] == 0) sq[x / b]--;
            }
        }
        for(int j = que[i - 1].l; j <= que[i].l - 1; j++)
        {
            int y = path[j];
            int x = v[y];
            if(x > n) continue;
            if(!act[y])
            {
                act[y] = 1;
                db[x]++;
                if(db[x] == 1) sq[x / b]++;
            }
            else
            {
                act[y] = 0;
                db[x]--;
                if(db[x] == 0) sq[x / b]--;
            }
        }
        for(int j = que[i - 1].r; j >= que[i].r + 1; j--)
        {
            int y = path[j];
            int x = v[y];
            if(x > n) continue;
            if(!act[y])
            {
                act[y] = 1;
                db[x]++;
                if(db[x] == 1) sq[x / b]++;
            }
            else
            {
                act[y] = 0;
                db[x]--;
                if(db[x] == 0) sq[x / b]--;
            }
        }
        int x = v[que[i].lca];
        if(x <= n)
        {
            db[x]++;
            if(db[x] == 1) sq[x / b]++;
        }
        for(int j = 0; j < b; j++)
        {
            if(sq[j] < b)
            {
                for(int k = 0; k < b; k++)
                {
                    if(db[j * b + k] == 0)
                    {
                        ans[que[i].ind] = j * b + k;
                        break;
                    }
                }
                break;
            }
        }
        if(x <= n)
        {
            db[x]--;
            if(db[x] == 0) sq[x / b]--;
        }
    }
    for(auto x : ans) cout << prim[x] << "\n";
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted35ms20048 KiB
subtask25/5
2Accepted35ms20428 KiB
3Accepted35ms20672 KiB
4Accepted35ms20604 KiB
5Accepted35ms20612 KiB
6Accepted35ms20820 KiB
7Accepted35ms21232 KiB
8Accepted37ms21500 KiB
9Accepted37ms21448 KiB
10Accepted37ms21760 KiB
11Accepted37ms21916 KiB
subtask35/5
12Accepted35ms21828 KiB
13Accepted34ms22076 KiB
14Accepted34ms21964 KiB
15Accepted35ms22064 KiB
16Accepted37ms22336 KiB
17Accepted37ms22276 KiB
18Accepted37ms22588 KiB
19Accepted35ms22816 KiB
20Accepted35ms22872 KiB
21Accepted37ms22832 KiB
subtask410/10
22Accepted56ms26576 KiB
23Accepted166ms31684 KiB
24Accepted280ms50860 KiB
25Accepted398ms65456 KiB
26Accepted449ms72532 KiB
27Accepted476ms71908 KiB
subtask510/10
28Accepted68ms26316 KiB
29Accepted196ms30840 KiB
30Accepted635ms48492 KiB
31Accepted1.281s61428 KiB
32Accepted1.578s66192 KiB
33Accepted1.585s66196 KiB
subtask610/10
34Accepted37ms23476 KiB
35Accepted52ms27248 KiB
36Accepted118ms35648 KiB
37Accepted243ms51684 KiB
38Accepted324ms67864 KiB
39Accepted439ms71264 KiB
40Accepted342ms74840 KiB
41Accepted384ms71692 KiB
subtask715/15
42Accepted35ms23620 KiB
43Accepted54ms26776 KiB
44Accepted127ms34492 KiB
45Accepted298ms48496 KiB
46Accepted513ms62036 KiB
47Accepted602ms66452 KiB
48Accepted606ms66560 KiB
49Accepted600ms66448 KiB
50Accepted648ms66460 KiB
51Accepted171ms65064 KiB
52Accepted234ms57708 KiB
53Accepted229ms63676 KiB
54Accepted164ms65152 KiB
55Accepted250ms57824 KiB
56Accepted1.2s57728 KiB
subtask820/20
57Accepted56ms27336 KiB
58Accepted103ms34960 KiB
59Accepted204ms42492 KiB
60Accepted326ms53552 KiB
61Accepted361ms72480 KiB
62Accepted356ms74928 KiB
63Accepted500ms71684 KiB
64Accepted474ms72284 KiB
65Accepted216ms75600 KiB
66Accepted216ms75572 KiB
subtask925/25
67Accepted61ms26844 KiB
68Accepted119ms33320 KiB
69Accepted232ms40336 KiB
70Accepted680ms49184 KiB
71Accepted606ms66016 KiB
72Accepted625ms66452 KiB
73Accepted1.231s66452 KiB
74Accepted884ms66624 KiB
75Accepted754ms66684 KiB
76Accepted890ms66504 KiB
77Accepted987ms66592 KiB
78Accepted796ms66452 KiB
79Accepted995ms66444 KiB
80Accepted437ms70116 KiB
81Accepted470ms70108 KiB
82Accepted240ms75436 KiB
83Accepted970ms71264 KiB