46802023-03-31 08:29:19gortomiLegkisebb nem oszthatócpp17Wrong answer 85/1001.585s67000 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(1e6 + 1, 1);
    vector<int> pl(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 <= 1e6; i++)
    {
        if(ip[i])
        {
            pl[i] = prim.size();
            prim.push_back(i);
            for(int j = 2 * i; j <= 1e6; j += i)
            {
                ip[j] = 0;
            }
        }
    }
    vector<int> v(n + 1, 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
1Accepted18ms11148 KiB
subtask25/5
2Accepted17ms11364 KiB
3Accepted18ms11584 KiB
4Accepted18ms11544 KiB
5Accepted19ms11812 KiB
6Accepted18ms12148 KiB
7Accepted19ms12404 KiB
8Accepted18ms12556 KiB
9Accepted19ms12712 KiB
10Accepted19ms12988 KiB
11Accepted19ms13240 KiB
subtask35/5
12Accepted17ms12964 KiB
13Accepted18ms13140 KiB
14Accepted18ms13368 KiB
15Accepted19ms13564 KiB
16Accepted19ms13536 KiB
17Accepted19ms13588 KiB
18Accepted20ms14032 KiB
19Accepted19ms14124 KiB
20Accepted20ms13988 KiB
21Accepted20ms13984 KiB
subtask410/10
22Accepted41ms17916 KiB
23Accepted155ms23044 KiB
24Accepted277ms41632 KiB
25Accepted402ms56228 KiB
26Accepted495ms63468 KiB
27Accepted504ms62904 KiB
subtask510/10
28Accepted52ms17400 KiB
29Accepted178ms22268 KiB
30Accepted610ms39616 KiB
31Accepted1.254s52804 KiB
32Accepted1.562s57636 KiB
33Accepted1.585s57648 KiB
subtask610/10
34Accepted19ms14564 KiB
35Accepted37ms18648 KiB
36Accepted98ms27036 KiB
37Accepted228ms42944 KiB
38Accepted307ms59124 KiB
39Accepted386ms62644 KiB
40Accepted347ms66160 KiB
41Accepted395ms62920 KiB
subtask70/15
42Accepted20ms14552 KiB
43Accepted39ms18032 KiB
44Accepted111ms25728 KiB
45Accepted289ms39940 KiB
46Accepted513ms53392 KiB
47Accepted606ms57772 KiB
48Accepted609ms57860 KiB
49Accepted601ms57840 KiB
50Accepted651ms57760 KiB
51Wrong answer152ms56412 KiB
52Accepted232ms49088 KiB
53Accepted214ms55152 KiB
54Wrong answer152ms56644 KiB
55Accepted217ms49308 KiB
56Accepted1.182s49376 KiB
subtask820/20
57Accepted37ms18784 KiB
58Accepted87ms26452 KiB
59Accepted189ms33896 KiB
60Accepted308ms44888 KiB
61Accepted345ms64016 KiB
62Accepted337ms66552 KiB
63Accepted504ms63308 KiB
64Accepted430ms64000 KiB
65Accepted206ms67000 KiB
66Accepted199ms66976 KiB
subtask925/25
67Accepted45ms18200 KiB
68Accepted101ms24700 KiB
69Accepted217ms31620 KiB
70Accepted670ms40536 KiB
71Accepted587ms57484 KiB
72Accepted610ms57920 KiB
73Accepted1.21s57884 KiB
74Accepted875ms57912 KiB
75Accepted737ms57980 KiB
76Accepted897ms57980 KiB
77Accepted954ms57912 KiB
78Accepted796ms57900 KiB
79Accepted957ms57900 KiB
80Accepted421ms61660 KiB
81Accepted456ms61652 KiB
82Accepted216ms66956 KiB
83Accepted953ms62828 KiB