QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#682517 | #2637. Factor-Free Tree | m5588ohammed | WA | 425ms | 186216kb | C++20 | 3.3kb | 2024-10-27 15:49:27 | 2024-10-27 15:49:29 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mod 1000000007
int L[1000005],R[1000005],prime[10000005],arr[1000005],parent[1000005];
int n;
unordered_map <int,int> mp;
set <int> st;
vector <int> v[1000005];
void buildL(int i){
int a=arr[i];
while(a!=1){
int div=prime[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];
while(a!=1){
int div=prime[a];
if(R[i]==0) R[i]=mp[div];
else R[i]=min(R[i],mp[div]);
while(a%div==0) a/=div;
mp[div]=i;
}
if(R[i]==0) R[i]=n+1;
return;
}
void build(){
for(int i=2;i<=10000000;i++){
if(prime[i]!=0) continue;
for(int j=i;j<=10000000;j+=i) if(prime[j]==0) prime[j]=i;
}
//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: 425ms
memory: 186216kb
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: 168ms
memory: 85788kb
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: 183ms
memory: 85848kb
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: 178ms
memory: 86192kb
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: -100
Wrong Answer
time: 169ms
memory: 88436kb
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:
impossible
result:
wrong output format Expected integer, but "impossible" found