QOJ.ac
QOJ
The 2nd Universal Cup Finals is coming! Check out our event page, schedule, and competition rules!
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#682519 | #2637. Factor-Free Tree | m5588ohammed | TL | 5249ms | 174476kb | C++20 | 3.8kb | 2024-10-27 15:50:50 | 2024-10-27 15:50:51 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
int L[1000005],R[1000005],prime[100005],arr[1000005],parent[1000005];
int n;
map <int,int> mp;
set <int> st;
vector <int> v[1000005];
void buildL(int i){
int a=arr[i];
//cout<<arr[i]<<endl;
for(int j=1;j<=sqrt(arr[i]);j++){
if(a%j!=0||prime[j]==1) continue;
int div=j;
if(L[i]==0) L[i]=mp[div];
else L[i]=max(L[i],mp[div]);
while(a%div==0) a/=div;
mp[div]=i;
}
if(a!=1){
int div=a;
if(L[i]==0) L[i]=mp[div];
else L[i]=max(L[i],mp[div]);
while(a%div==0) a/=div;
mp[div]=i;
}
return;
}
void buildR(int i){
int a=arr[i];
for(int j=1;j<=sqrt(arr[i]);j++){
if(a%j!=0||prime[j]==1) continue;
int div=j;
if(R[i]==0) R[i]=mp[div];
else if(mp[div]!=0) R[i]=min(R[i],mp[div]);
while(a%div==0) a/=div;
mp[div]=i;
}
if(a!=1){
int div=a;
if(R[i]==0) R[i]=mp[div];
else if(mp[div]!=0) R[i]=min(R[i],mp[div]);
while(a%div==0) a/=div;
mp[div]=i;
}
if(R[i]==0) R[i]=n+1;
//if(i==6) exit(0);
return;
}
void build(){
prime[1]=prime[0]=1;
for(int i=2;i<=sqrt(10000000);i++){
for(int j=i+i;j<=sqrt(10000000);j+=i) prime[j]=1;
}
//for(int i=2;i<=sqrt(10000000);i++) if(prime[i]==0) prm.push_back(i);
cin>>n;
for(int i=1;i<=n;i++){
cin>>arr[i];
buildL(i);
}
mp.clear();
for(int i=n;i>=1;i--){
buildR(i);
//cout<<L[i]<<" "<<R[i]<<endl;
v[L[i]].push_back(i);
v[R[i]].push_back(i);
}
for(int i=1;i<=n;i++) sort(v[i].begin(),v[i].end());
return;
}
void imp(){
cout<<"impossible";
exit(0);
}
int check(int i){
if(L[i]==0&&R[i]==n+1) return 1;
else return 0;
}
void solve(int l,int r,int pre){
if(l>r) return;
if(st.size()==0) imp();
int rt=*st.begin(),siz1=rt-l,siz2=r-rt;
set <int> st2;
parent[rt]=pre;
st.erase(rt);
if(siz1<=siz2){
for(int i=l;i<rt;i++){
if(R[i]>=rt) R[i]=n+1;
if(check(i)==1){
st.erase(i);
st2.insert(i);
}
int l1=0,r1=v[i].size()-1,idx=v[i].size();
while(l1<=r1){
int mid=(l1+r1)/2;
if(v[i][mid]>rt){
idx=mid;
r1=mid-1;
}
else l1=mid+1;
}
for(int j=idx;j<v[i].size();j++){
if(v[i][j]>r) break;
L[v[i][j]]=0;
if(check(v[i][j])==1) st.insert(v[i][j]);
}
}
solve(rt+1,r,rt);
swap(st2,st);
solve(l,rt-1,rt);
}
else{
for(int i=rt+1;i<=r;i++){
if(L[i]<=rt) L[i]=0;
if(check(i)==1){
st.erase(i);
st2.insert(i);
}
int l1=0,r1=v[i].size()-1,idx=-1;
while(l1<=r1){
int mid=(l1+r1)/2;
if(v[i][mid]<rt){
idx=mid;
l1=mid+1;
}
else r1=mid-1;
}
for(int j=idx;j>=0;j--){
if(v[i][j]<l) break;
R[v[i][j]]=n+1;
if(check(v[i][j])==1) st.insert(v[i][j]);
}
}
solve(l,rt-1,rt);
swap(st2,st);
solve(rt+1,r,rt);
}
return;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
build();
for(int i=1;i<=n;i++) if(check(i)==1) st.insert(i);
solve(1,n,0);
for(int i=1;i<=n;i++) cout<<parent[i]<<" ";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2413ms
memory: 92896kb
input:
1000000 19 29 31 37 41 43 47 53 59 23 29 31 37 41 43 47 53 59 17 37 41 43 47 53 31 37 41 43 47 53 29 37 41 43 47 31 37 41 43 47 23 37 41 43 47 53 59 61 31 37 41 43 47 53 59 61 29 41 43 47 53 59 37 41 43 47 53 59 31 41 47 53 59 61 67 71 73 43 47 53 59 61 67 71 73 37 61 71 73 79 83 89 97 67 71 73 79 8...
output:
19 10 2 3 4 5 6 7 8 1 10 11 12 13 14 15 16 17 999895 25 20 21 22 23 31 25 26 27 28 29 41 36 32 33 34 31 36 37 38 39 999880 49 42 43 44 45 46 47 57 49 50 51 52 53 54 55 41 63 58 59 60 61 69 63 64 65 66 67 57 86 78 71 72 73 74 75 76 70 78 79 80 81 82 83 84 69 101 94 88 89 90 91 92 87 94 95 96 97 98 99...
result:
ok
Test #2:
score: 0
Accepted
time: 3ms
memory: 9656kb
input:
10 34033 56827 124799 25981 90371 129293 195581 175601 77867 102811
output:
0 1 2 3 4 5 6 7 8 9
result:
ok
Test #3:
score: 0
Accepted
time: 0ms
memory: 9892kb
input:
100 4007 58067 87683 39181 1319 17659 105871 128981 112241 153733 98419 148867 162119 3877 28627 26681 10211 64591 146701 91183 192307 179119 81283 4729 65419 143197 37649 92567 48473 31277 182141 196991 17387 47491 183499 59107 61687 21323 160079 150401 16477 92809 182561 39113 151787 83557 173293 ...
output:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
result:
ok
Test #4:
score: 0
Accepted
time: 0ms
memory: 10264kb
input:
1000 89417 156589 58309 7919 93497 100207 163981 176503 8887 69163 198823 102653 88609 13477 112603 94219 46831 34963 39113 190901 101723 121421 30553 51673 98129 17509 101021 28439 90023 16069 98299 66851 133321 587 154057 53381 67369 49277 1783 9109 7537 45137 32533 101627 137507 154789 29717 1386...
output:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 59 57 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 91 89 91 92 93 94 95 96 97 98 99 100 101 10...
result:
ok
Test #5:
score: 0
Accepted
time: 21ms
memory: 12332kb
input:
10000 53047 108499 32027 141353 17021 103067 91253 195259 43801 36451 24023 121067 185707 1361 69151 141131 37039 143873 166043 136861 155657 195739 106501 15973 10687 158699 148061 88919 156781 36677 52181 43133 118589 188603 41257 81799 21377 89897 43051 70921 65027 159569 197573 22709 20323 10453...
output:
2 0 2 5 3 5 6 10 8 7 13 11 10 13 14 15 18 16 18 19 20 24 22 21 24 27 25 27 28 29 36 31 32 33 34 30 38 36 40 38 40 41 44 42 46 44 48 46 50 48 50 53 51 53 57 55 54 57 58 59 60 63 61 66 64 63 66 67 68 69 72 70 72 73 77 75 74 77 78 79 80 81 85 83 82 85 86 89 87 89 92 90 92 96 94 93 101 97 98 99 96 104 1...
result:
ok
Test #6:
score: 0
Accepted
time: 195ms
memory: 23780kb
input:
100000 194891 2719 191353 14731 5897 124561 134593 56479 129401 61547 88289 2683 66239 40591 105397 83921 178261 91631 23539 198829 93529 98347 173053 63073 124981 37549 195047 21059 131947 34469 52249 42509 142183 140683 43117 57791 49823 167861 11471 49523 107999 23833 187217 53003 161527 75571 70...
output:
351 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 ...
result:
ok
Test #7:
score: 0
Accepted
time: 0ms
memory: 9680kb
input:
1 1
output:
0
result:
ok
Test #8:
score: 0
Accepted
time: 7ms
memory: 10072kb
input:
1000 3297617 3913837 1136897 3348581 323083 1900463 933931 4455221 504857 7477979 8972191 8565031 5216683 5933461 8936357 5464573 4607719 3821791 79537 5023471 4323611 3215039 305479 5544421 1129729 7893533 572683 5444237 8669237 8296627 592931 5292361 7848089 5677057 4641907 1270943 7611047 9740611...
output:
impossible
result:
ok
Test #9:
score: 0
Accepted
time: 2ms
memory: 9764kb
input:
13 2 3 2 5 7 5 11 5 7 5 2 3 2
output:
2 7 2 5 3 5 0 9 7 9 12 10 12
result:
ok
Test #10:
score: 0
Accepted
time: 87ms
memory: 18108kb
input:
49982 157273 19777 49369 134161 140401 161059 155887 140401 134161 46993 58757 136343 140893 58757 46993 140893 181081 58757 181081 106297 140893 53629 122033 34313 122033 8779 53629 180623 8779 122033 180623 41521 8779 92941 3407 92737 187177 92941 3407 111827 22901 51001 172561 22901 111827 19919 ...
output:
3 1 49980 6 4 49974 9 7 6 12 10 49968 15 13 12 18 16 15 21 19 18 24 22 49959 27 25 24 30 28 27 33 31 30 36 34 49950 39 37 36 42 40 49944 45 43 42 48 46 49941 51 49 49935 54 52 51 57 55 49926 60 58 57 63 61 49923 66 64 63 69 67 66 72 70 69 75 73 72 78 76 75 81 79 78 84 82 81 87 85 84 90 88 87 93 91 9...
result:
ok
Test #11:
score: 0
Accepted
time: 3ms
memory: 9736kb
input:
6 2 7 15 8 9 5
output:
2 0 4 2 4 5
result:
ok
Test #12:
score: 0
Accepted
time: 86ms
memory: 18264kb
input:
49982 153749 164771 156007 161159 164771 153749 701 80923 79367 176549 77509 701 77509 367 176549 16063 182099 31799 16063 6691 182099 183059 138683 6691 126311 183059 138683 48649 151507 184463 47933 23311 48757 19031 47933 23311 19031 112771 47933 156157 112771 19031 156157 138251 112771 161947 13...
output:
3 1 49980 6 4 3 9 7 49971 12 10 49968 15 13 12 18 16 49962 21 19 18 24 22 49959 27 25 24 30 28 49938 33 31 49926 36 34 33 39 37 36 42 40 39 45 43 42 48 46 45 51 49 49914 54 52 51 57 55 49911 60 58 49905 63 61 49902 66 64 49899 69 67 49896 72 70 49887 75 73 72 78 76 49884 81 79 49881 84 82 49878 87 8...
result:
ok
Test #13:
score: 0
Accepted
time: 74ms
memory: 14924kb
input:
49982 183871 116041 4639 28393 4639 116041 132859 116041 117539 68659 149239 55927 103963 55927 68659 55927 103963 117539 4639 117539 68659 55927 113171 121571 66533 177953 121571 113171 55213 113171 121571 104399 141439 14741 56333 14741 97879 14741 199 144941 61687 185873 168083 130261 115603 1142...
output:
7 4 2 1 4 5 49973 49968 11 9 19 13 15 13 11 15 16 17 8 49954 49943 29 25 23 22 25 26 27 21 49936 49928 49922 37 35 33 35 32 49906 49897 49888 49877 49870 48 45 43 45 46 42 58 54 52 50 52 49 54 55 56 48 62 59 60 58 49841 49826 49822 74 68 66 70 72 70 68 72 77 74 75 65 82 80 78 80 77 49813 88 86 84 86...
result:
ok
Test #14:
score: 0
Accepted
time: 82ms
memory: 14868kb
input:
49982 21143 129641 142873 20431 143827 37489 77489 143827 20431 98387 20431 143827 37489 20431 142873 143257 142873 37489 20431 98387 37489 77489 109397 145967 115153 145967 165133 115153 77489 143827 77489 115153 109397 145967 115153 165133 115153 109397 142453 121621 149143 142453 136093 149143 13...
output:
49960 16 7 3 4 5 2 10 8 7 12 10 12 13 14 1 20 17 18 16 30 23 21 25 27 25 23 27 28 49946 36 33 31 33 34 30 47 40 38 37 42 40 44 42 44 45 36 49937 52 49 50 48 56 53 54 62 58 56 58 59 60 52 74 65 63 67 69 67 71 69 65 71 72 62 81 78 76 75 78 79 74 49929 91 88 86 84 86 83 88 89 82 105 98 95 93 95 96 92 1...
result:
ok
Test #15:
score: 0
Accepted
time: 83ms
memory: 15000kb
input:
49982 65381 87257 148997 95569 159499 91249 159499 95569 54413 95569 159499 175979 121493 192043 99809 121493 168211 121493 99809 104471 172673 128621 165437 128621 104471 165437 99809 192043 99809 104471 128621 21101 179563 183167 149111 183167 179563 112067 179563 183167 199679 101111 5531 74923 5...
output:
49961 49953 9 6 4 3 6 7 2 49938 49924 49920 14 17 14 15 12 28 21 19 18 23 25 23 21 25 26 17 49910 49896 49887 38 35 33 32 35 36 31 49878 49873 48 44 42 41 44 45 46 40 54 51 49 51 52 48 62 58 56 55 58 59 60 54 49855 71 67 65 64 67 68 69 63 49843 49836 88 77 75 74 79 77 81 83 81 85 83 79 85 86 73 4982...
result:
ok
Test #16:
score: 0
Accepted
time: 1638ms
memory: 43416kb
input:
199982 8822797 2017831 8092661 5518399 8822797 2017831 5518399 9660557 8822797 4873903 9562589 2518121 7176457 2052187 3999971 7470457 7254319 2052187 2933753 3522653 7470457 5930887 8339677 5675491 7468999 7600613 8339677 2693443 3477263 6769181 2693443 3473611 3477263 4743587 8741347 3473611 38051...
output:
3 1 199977 6 4 3 9 7 6 12 10 199965 15 13 199959 18 16 199956 21 19 199953 24 22 199941 27 25 199938 30 28 199926 33 31 30 36 34 199923 39 37 199920 42 40 39 45 43 42 48 46 45 51 49 199911 54 52 51 57 55 199908 60 58 199905 63 61 60 66 64 199899 69 67 199896 72 70 199884 75 73 199878 78 76 199875 81...
result:
ok
Test #17:
score: 0
Accepted
time: 4254ms
memory: 80420kb
input:
499982 7008569 7583963 383963 1194059 4307741 1194059 9541871 383963 4078883 9541871 1194059 4078883 5363461 9541871 5192167 5363461 4078883 5192167 9629063 5363461 1997243 9629063 5192167 9203729 3379049 1997243 9203729 5238421 3379049 9797807 865807 8148229 5662169 9797807 865807 131101 5662169 97...
output:
249129 30000 5 3 2 8 6 5 11 9 8 14 12 11 17 15 14 20 18 17 23 21 20 26 24 29997 29 27 26 32 30 29985 35 33 32 38 36 35 41 39 38 44 42 29982 47 45 29976 50 48 29967 53 51 50 56 54 29946 59 57 56 62 60 29943 65 63 29940 68 66 29931 71 69 68 74 72 71 77 75 29928 80 78 77 83 81 29925 86 84 29922 89 87 8...
result:
ok
Test #18:
score: 0
Accepted
time: 4227ms
memory: 81596kb
input:
499982 2852963 1799107 4868233 1621637 9713947 1644421 2105921 9713947 1621637 2105921 2502293 9713947 2502293 6517543 2105921 5304263 8478671 9964979 5304263 8263169 8478671 216037 8263169 5304263 216037 6113453 8263169 4307323 6113453 216037 4307323 7347983 6113453 1493207 7347983 4307323 8378369 ...
output:
3 1 30003 6 4 14856 9 7 6 12 10 9 12 13 14 18 16 15 21 19 18 24 22 21 27 25 24 30 28 27 33 31 30 36 34 33 38 36 38 39 42 40 42 43 44 48 46 45 50 48 50 51 54 52 57 55 54 60 58 57 60 63 61 66 64 63 66 67 68 69 70 71 75 73 72 77 75 77 78 79 80 81 82 83 87 85 84 87 88 89 90 93 91 96 94 93 96 97 98 101 9...
result:
ok
Test #19:
score: 0
Accepted
time: 4233ms
memory: 99684kb
input:
499982 8222183 7306531 5659859 7306531 8781709 8222183 9018617 7510879 8781709 2169203 4679911 9018617 8301169 3324203 4679911 8753663 3324203 8301169 5029907 9590767 8753663 2524393 8463877 9590767 2524393 2344033 8463877 5087237 2344033 2524393 3922741 5087237 2344033 505613 5407639 3922741 991777...
output:
3 1 0 6 4 3 9 7 499980 12 10 499977 15 13 499974 18 16 15 21 19 499971 24 22 499968 27 25 24 30 28 27 33 31 30 36 34 499965 39 37 499962 42 40 499959 45 43 499956 48 46 499953 51 49 48 54 52 499950 57 55 54 60 58 499944 63 61 60 66 64 499935 69 67 66 72 70 69 75 73 499932 78 76 499929 81 79 499923 8...
result:
ok
Test #20:
score: 0
Accepted
time: 86ms
memory: 18164kb
input:
49982 12269 119299 86539 119299 64123 12269 85549 23333 93241 108877 119557 95107 108877 20441 119557 173819 20441 108877 53281 27827 173819 53281 128239 27827 113489 113969 128239 122279 138889 113489 138899 138889 122279 138899 104549 138889 169987 145259 194083 48619 39733 169987 39733 187871 486...
output:
3 1 49980 6 4 3 9 7 49974 12 10 49962 15 13 12 18 16 15 21 19 49959 24 22 21 27 25 49956 30 28 49953 33 31 30 36 34 33 39 37 49947 42 40 49944 45 43 42 48 46 49941 51 49 49932 54 52 49929 57 55 49920 60 58 57 63 61 49917 66 64 63 69 67 66 72 70 69 75 73 72 78 76 49914 81 79 49896 84 82 81 87 85 4989...
result:
ok
Test #21:
score: 0
Accepted
time: 0ms
memory: 9784kb
input:
6 2 7 15 8 9 6
output:
impossible
result:
ok
Test #22:
score: 0
Accepted
time: 4868ms
memory: 146080kb
input:
999995 5 3 11 7 17 13 23 19 31 29 41 37 47 43 59 53 67 61 73 71 83 79 97 89 103 101 109 107 127 113 137 131 149 139 157 151 167 163 179 173 191 181 197 193 211 199 227 223 233 229 241 239 257 251 269 263 277 271 283 281 307 293 313 311 331 317 347 337 353 349 367 359 379 373 389 383 401 397 419 409 ...
output:
2 499998 4 499996 6 499994 8 499992 10 499990 12 499988 14 499986 16 499984 18 499982 20 499980 22 499978 24 499976 26 499974 28 499972 30 499970 32 499968 34 499966 36 499964 38 499962 40 499960 42 499958 44 499956 46 499954 48 499952 50 499950 52 499948 54 499946 56 499944 58 499942 60 499940 62 4...
result:
ok
Test #23:
score: 0
Accepted
time: 5249ms
memory: 174476kb
input:
999999 5 3 11 7 17 13 23 19 31 29 41 37 47 43 59 53 67 61 73 71 83 79 97 89 103 101 109 107 127 113 137 131 149 139 157 151 167 163 179 173 191 181 197 193 211 199 227 223 233 229 241 239 257 251 269 263 277 271 283 281 307 293 313 311 331 317 347 337 353 349 367 359 379 373 389 383 401 397 419 409 ...
output:
2 697002 4 697000 6 696998 8 696996 10 696994 12 696992 14 696990 16 696988 18 696986 20 696984 22 696982 24 696980 26 696978 28 696976 30 696974 32 696972 34 696970 36 696968 38 696966 40 696964 42 696962 44 696960 46 696958 48 696956 50 696954 52 696952 54 696950 56 696948 58 696946 60 696944 62 6...
result:
ok
Test #24:
score: -100
Time Limit Exceeded
input:
999999 3 2 7 5 13 11 19 17 29 23 37 31 43 41 53 47 61 59 71 67 79 73 89 83 101 97 107 103 113 109 131 127 139 137 151 149 163 157 173 167 181 179 193 191 199 197 223 211 229 227 239 233 251 241 263 257 271 269 281 277 293 283 311 307 317 313 337 331 349 347 359 353 373 367 383 379 397 389 409 401 42...
output:
2 0 4 999998 6 999996 8 999994 10 999992 12 999990 14 999988 16 999986 18 999984 20 999982 22 999980 24 999978 26 999976 28 999974 30 999972 32 999970 34 999968 36 999966 38 999964 40 999962 42 999960 44 999958 46 999956 48 999954 50 999952 52 999950 54 999948 56 999946 58 999944 60 999942 62 999940...