46682023-03-30 18:53:36gortomiLegkisebb nem oszthatócpp17Wrong answer 45/1001.511s43488 KiB
#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > g;
int tim = 0;
vector<int> l, r, par;
void dfs(int n, int p)
{
    par[n] = p;
    l[n] = tim++;
    for(auto x : g[n]) if(x != p) dfs(x, n);
    r[n] = tim++;
}
struct val
{
    int l, r, ind, ty;
};
bool comp(val a, val b)
{
    return a.l / 350 < b.l / 350 || (a.l / 350 == b.l / 350 && a.r < b.r);
}
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);
    l.resize(n + 1);
    r.resize(n + 1);
    par.resize(n + 1);
    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, 0);
    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};
    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].ty = 1;
        }
        else if(l[x] > l[y] && r[x] > r[y])
        {
            que[i].l = r[y];
            que[i].r = l[x];
            que[i].ty = 1;
        }
        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].ty = 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]--;
            }
        }
        if(que[i].ty == 1)
        {
            int x = v[par[path[que[i].l]]];
            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(que[i].ty == 1)
        {
            int x = v[par[path[que[i].l]]];
            if(x <= n)
            {
                db[x]--;
                if(db[x] == 0) sq[x / b]--;
            }
        }
    }
    for(auto x : ans) cout << prim[x] << "\n";
}

SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted19ms11076 KiB
subtask25/5
2Accepted20ms11292 KiB
3Accepted20ms11788 KiB
4Accepted19ms11880 KiB
5Accepted20ms12100 KiB
6Accepted20ms12556 KiB
7Accepted20ms12660 KiB
8Accepted21ms12636 KiB
9Accepted20ms12800 KiB
10Accepted20ms13004 KiB
11Accepted20ms13272 KiB
subtask30/5
12Accepted19ms13304 KiB
13Accepted19ms13252 KiB
14Wrong answer20ms13480 KiB
15Accepted20ms13468 KiB
16Accepted20ms13352 KiB
17Accepted21ms13436 KiB
18Accepted21ms13488 KiB
19Accepted21ms13660 KiB
20Wrong answer21ms13572 KiB
21Wrong answer21ms13572 KiB
subtask410/10
22Accepted35ms15416 KiB
23Accepted133ms19804 KiB
24Accepted207ms28396 KiB
25Accepted296ms35404 KiB
26Accepted340ms39820 KiB
27Accepted342ms39464 KiB
subtask50/10
28Wrong answer48ms15296 KiB
29Wrong answer158ms19236 KiB
30Wrong answer566ms26484 KiB
31Wrong answer1.195s31896 KiB
32Wrong answer1.511s33976 KiB
33Wrong answer1.511s34032 KiB
subtask610/10
34Accepted21ms14216 KiB
35Accepted32ms16188 KiB
36Accepted81ms21132 KiB
37Accepted164ms29924 KiB
38Accepted238ms38196 KiB
39Accepted263ms39052 KiB
40Accepted263ms42452 KiB
41Accepted280ms39496 KiB
subtask70/15
42Accepted21ms14384 KiB
43Accepted37ms15768 KiB
44Accepted97ms19724 KiB
45Accepted254ms26904 KiB
46Accepted465ms32512 KiB
47Accepted555ms34440 KiB
48Accepted551ms34404 KiB
49Accepted545ms34316 KiB
50Wrong answer597ms34284 KiB
51Wrong answer130ms37972 KiB
52Accepted165ms30660 KiB
53Accepted164ms36464 KiB
54Wrong answer131ms37964 KiB
55Accepted168ms30596 KiB
56Accepted1.113s30548 KiB
subtask820/20
57Accepted35ms16116 KiB
58Accepted76ms21084 KiB
59Accepted158ms25664 KiB
60Accepted263ms31484 KiB
61Accepted272ms40692 KiB
62Accepted273ms42912 KiB
63Accepted397ms39664 KiB
64Accepted342ms40228 KiB
65Accepted179ms43488 KiB
66Accepted175ms43452 KiB
subtask90/25
67Wrong answer43ms15804 KiB
68Accepted90ms19348 KiB
69Wrong answer194ms23448 KiB
70Wrong answer624ms27284 KiB
71Accepted529ms34244 KiB
72Accepted546ms34424 KiB
73Wrong answer1.166s34412 KiB
74Wrong answer822ms34416 KiB
75Wrong answer675ms34380 KiB
76Wrong answer837ms34376 KiB
77Wrong answer906ms34428 KiB
78Wrong answer749ms34388 KiB
79Wrong answer925ms34412 KiB
80Accepted374ms38024 KiB
81Accepted404ms38040 KiB
82Accepted175ms43356 KiB
83Accepted852ms39188 KiB