4631 | 2023-03-30 13:12:59 | gortomi | Legkisebb nem osztható | cpp17 | Wrong answer 45/100 | 1.504s | 42072 KiB |
#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > g;
int tim = 0;
vector<int> l, r;
void dfs(int n, int p)
{
l[n] = tim++;
for(auto x : g[n]) if(x != p) dfs(x, n);
r[n] = tim++;
}
struct val
{
int l, r, ind;
};
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);
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];
}
else if(l[x] > l[y] && r[x] > r[y])
{
que[i].l = r[y];
que[i].r = l[x];
}
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].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]--;
}
}
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;
}
}
}
for(auto x : ans) cout << prim[x] << "\n";
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 18ms | 10948 KiB | ||||
subtask2 | 5/5 | ||||||
2 | Accepted | 17ms | 11164 KiB | ||||
3 | Accepted | 18ms | 11384 KiB | ||||
4 | Accepted | 17ms | 11352 KiB | ||||
5 | Accepted | 17ms | 11688 KiB | ||||
6 | Accepted | 18ms | 11992 KiB | ||||
7 | Accepted | 18ms | 12088 KiB | ||||
8 | Accepted | 18ms | 12308 KiB | ||||
9 | Accepted | 18ms | 12548 KiB | ||||
10 | Accepted | 18ms | 12492 KiB | ||||
11 | Accepted | 19ms | 12496 KiB | ||||
subtask3 | 0/5 | ||||||
12 | Accepted | 17ms | 12336 KiB | ||||
13 | Accepted | 17ms | 12696 KiB | ||||
14 | Wrong answer | 17ms | 12796 KiB | ||||
15 | Accepted | 18ms | 12944 KiB | ||||
16 | Accepted | 18ms | 12800 KiB | ||||
17 | Accepted | 18ms | 13284 KiB | ||||
18 | Accepted | 19ms | 13240 KiB | ||||
19 | Accepted | 18ms | 13204 KiB | ||||
20 | Wrong answer | 19ms | 13332 KiB | ||||
21 | Wrong answer | 19ms | 13212 KiB | ||||
subtask4 | 10/10 | ||||||
22 | Accepted | 35ms | 14696 KiB | ||||
23 | Accepted | 136ms | 18652 KiB | ||||
24 | Accepted | 203ms | 27408 KiB | ||||
25 | Accepted | 294ms | 34148 KiB | ||||
26 | Accepted | 330ms | 38220 KiB | ||||
27 | Accepted | 330ms | 37760 KiB | ||||
subtask5 | 0/10 | ||||||
28 | Wrong answer | 46ms | 14940 KiB | ||||
29 | Wrong answer | 165ms | 18036 KiB | ||||
30 | Wrong answer | 569ms | 25076 KiB | ||||
31 | Wrong answer | 1.19s | 30228 KiB | ||||
32 | Wrong answer | 1.496s | 32192 KiB | ||||
33 | Wrong answer | 1.504s | 32196 KiB | ||||
subtask6 | 10/10 | ||||||
34 | Accepted | 18ms | 13976 KiB | ||||
35 | Accepted | 32ms | 15944 KiB | ||||
36 | Accepted | 79ms | 20048 KiB | ||||
37 | Accepted | 164ms | 28272 KiB | ||||
38 | Accepted | 232ms | 36556 KiB | ||||
39 | Accepted | 266ms | 37280 KiB | ||||
40 | Accepted | 266ms | 40548 KiB | ||||
41 | Accepted | 275ms | 37412 KiB | ||||
subtask7 | 0/15 | ||||||
42 | Accepted | 18ms | 13992 KiB | ||||
43 | Accepted | 35ms | 15168 KiB | ||||
44 | Accepted | 97ms | 18896 KiB | ||||
45 | Accepted | 252ms | 25208 KiB | ||||
46 | Accepted | 455ms | 30452 KiB | ||||
47 | Accepted | 547ms | 32228 KiB | ||||
48 | Accepted | 544ms | 32500 KiB | ||||
49 | Accepted | 537ms | 32392 KiB | ||||
50 | Wrong answer | 584ms | 32452 KiB | ||||
51 | Wrong answer | 119ms | 36588 KiB | ||||
52 | Accepted | 152ms | 29248 KiB | ||||
53 | Accepted | 155ms | 35272 KiB | ||||
54 | Wrong answer | 119ms | 36472 KiB | ||||
55 | Accepted | 152ms | 29120 KiB | ||||
56 | Accepted | 1.1s | 29176 KiB | ||||
subtask8 | 20/20 | ||||||
57 | Accepted | 34ms | 15940 KiB | ||||
58 | Accepted | 78ms | 20764 KiB | ||||
59 | Accepted | 150ms | 24768 KiB | ||||
60 | Accepted | 256ms | 30432 KiB | ||||
61 | Accepted | 257ms | 39264 KiB | ||||
62 | Accepted | 261ms | 41424 KiB | ||||
63 | Accepted | 379ms | 37912 KiB | ||||
64 | Accepted | 333ms | 38760 KiB | ||||
65 | Accepted | 173ms | 42072 KiB | ||||
66 | Accepted | 166ms | 41984 KiB | ||||
subtask9 | 0/25 | ||||||
67 | Wrong answer | 41ms | 15584 KiB | ||||
68 | Accepted | 90ms | 18684 KiB | ||||
69 | Wrong answer | 194ms | 22216 KiB | ||||
70 | Wrong answer | 620ms | 26032 KiB | ||||
71 | Accepted | 523ms | 32484 KiB | ||||
72 | Accepted | 542ms | 32640 KiB | ||||
73 | Wrong answer | 1.148s | 32616 KiB | ||||
74 | Wrong answer | 810ms | 32628 KiB | ||||
75 | Wrong answer | 669ms | 32676 KiB | ||||
76 | Wrong answer | 828ms | 32624 KiB | ||||
77 | Wrong answer | 892ms | 32752 KiB | ||||
78 | Wrong answer | 736ms | 32732 KiB | ||||
79 | Wrong answer | 903ms | 32624 KiB | ||||
80 | Accepted | 367ms | 36284 KiB | ||||
81 | Accepted | 398ms | 36284 KiB | ||||
82 | Accepted | 175ms | 41636 KiB | ||||
83 | Accepted | 833ms | 37732 KiB |