4680 | 2023-03-31 08:29:19 | gortomi | Legkisebb nem osztható | cpp17 | Wrong answer 85/100 | 1.585s | 67000 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";
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 18ms | 11148 KiB | ||||
subtask2 | 5/5 | ||||||
2 | Accepted | 17ms | 11364 KiB | ||||
3 | Accepted | 18ms | 11584 KiB | ||||
4 | Accepted | 18ms | 11544 KiB | ||||
5 | Accepted | 19ms | 11812 KiB | ||||
6 | Accepted | 18ms | 12148 KiB | ||||
7 | Accepted | 19ms | 12404 KiB | ||||
8 | Accepted | 18ms | 12556 KiB | ||||
9 | Accepted | 19ms | 12712 KiB | ||||
10 | Accepted | 19ms | 12988 KiB | ||||
11 | Accepted | 19ms | 13240 KiB | ||||
subtask3 | 5/5 | ||||||
12 | Accepted | 17ms | 12964 KiB | ||||
13 | Accepted | 18ms | 13140 KiB | ||||
14 | Accepted | 18ms | 13368 KiB | ||||
15 | Accepted | 19ms | 13564 KiB | ||||
16 | Accepted | 19ms | 13536 KiB | ||||
17 | Accepted | 19ms | 13588 KiB | ||||
18 | Accepted | 20ms | 14032 KiB | ||||
19 | Accepted | 19ms | 14124 KiB | ||||
20 | Accepted | 20ms | 13988 KiB | ||||
21 | Accepted | 20ms | 13984 KiB | ||||
subtask4 | 10/10 | ||||||
22 | Accepted | 41ms | 17916 KiB | ||||
23 | Accepted | 155ms | 23044 KiB | ||||
24 | Accepted | 277ms | 41632 KiB | ||||
25 | Accepted | 402ms | 56228 KiB | ||||
26 | Accepted | 495ms | 63468 KiB | ||||
27 | Accepted | 504ms | 62904 KiB | ||||
subtask5 | 10/10 | ||||||
28 | Accepted | 52ms | 17400 KiB | ||||
29 | Accepted | 178ms | 22268 KiB | ||||
30 | Accepted | 610ms | 39616 KiB | ||||
31 | Accepted | 1.254s | 52804 KiB | ||||
32 | Accepted | 1.562s | 57636 KiB | ||||
33 | Accepted | 1.585s | 57648 KiB | ||||
subtask6 | 10/10 | ||||||
34 | Accepted | 19ms | 14564 KiB | ||||
35 | Accepted | 37ms | 18648 KiB | ||||
36 | Accepted | 98ms | 27036 KiB | ||||
37 | Accepted | 228ms | 42944 KiB | ||||
38 | Accepted | 307ms | 59124 KiB | ||||
39 | Accepted | 386ms | 62644 KiB | ||||
40 | Accepted | 347ms | 66160 KiB | ||||
41 | Accepted | 395ms | 62920 KiB | ||||
subtask7 | 0/15 | ||||||
42 | Accepted | 20ms | 14552 KiB | ||||
43 | Accepted | 39ms | 18032 KiB | ||||
44 | Accepted | 111ms | 25728 KiB | ||||
45 | Accepted | 289ms | 39940 KiB | ||||
46 | Accepted | 513ms | 53392 KiB | ||||
47 | Accepted | 606ms | 57772 KiB | ||||
48 | Accepted | 609ms | 57860 KiB | ||||
49 | Accepted | 601ms | 57840 KiB | ||||
50 | Accepted | 651ms | 57760 KiB | ||||
51 | Wrong answer | 152ms | 56412 KiB | ||||
52 | Accepted | 232ms | 49088 KiB | ||||
53 | Accepted | 214ms | 55152 KiB | ||||
54 | Wrong answer | 152ms | 56644 KiB | ||||
55 | Accepted | 217ms | 49308 KiB | ||||
56 | Accepted | 1.182s | 49376 KiB | ||||
subtask8 | 20/20 | ||||||
57 | Accepted | 37ms | 18784 KiB | ||||
58 | Accepted | 87ms | 26452 KiB | ||||
59 | Accepted | 189ms | 33896 KiB | ||||
60 | Accepted | 308ms | 44888 KiB | ||||
61 | Accepted | 345ms | 64016 KiB | ||||
62 | Accepted | 337ms | 66552 KiB | ||||
63 | Accepted | 504ms | 63308 KiB | ||||
64 | Accepted | 430ms | 64000 KiB | ||||
65 | Accepted | 206ms | 67000 KiB | ||||
66 | Accepted | 199ms | 66976 KiB | ||||
subtask9 | 25/25 | ||||||
67 | Accepted | 45ms | 18200 KiB | ||||
68 | Accepted | 101ms | 24700 KiB | ||||
69 | Accepted | 217ms | 31620 KiB | ||||
70 | Accepted | 670ms | 40536 KiB | ||||
71 | Accepted | 587ms | 57484 KiB | ||||
72 | Accepted | 610ms | 57920 KiB | ||||
73 | Accepted | 1.21s | 57884 KiB | ||||
74 | Accepted | 875ms | 57912 KiB | ||||
75 | Accepted | 737ms | 57980 KiB | ||||
76 | Accepted | 897ms | 57980 KiB | ||||
77 | Accepted | 954ms | 57912 KiB | ||||
78 | Accepted | 796ms | 57900 KiB | ||||
79 | Accepted | 957ms | 57900 KiB | ||||
80 | Accepted | 421ms | 61660 KiB | ||||
81 | Accepted | 456ms | 61652 KiB | ||||
82 | Accepted | 216ms | 66956 KiB | ||||
83 | Accepted | 953ms | 62828 KiB |