# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
978424 | alexdd | popa (BOI18_popa) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "popa.h"
#include<bits/stdc++.h>
using namespace std;
int _s[1005],_n;
int query(int a, int b, int c, int d)
{
int gc1=_s[a],gc2=_s[c];
for(int i=a;i<=b;i++)
gc1 = __gcd(gc1,_s[i]);
for(int i=c;i<=d;i++)
gc2 = __gcd(gc2,_s[i]);
if(gc1==gc2)
return 1;
return 0;
}
int myquery(int a, int b, int c, int d)
{
return query(min(a,b),max(a,b),min(c,d),max(c,d));
}
vector<int> ord;
void precalc(int limle[1005])
{
for(int i=0;i<ord.size();i++)
{
int st=0,dr=i,ans=i;
while(st<=dr)
{
int mij=(st+dr)/2;
if(myquery(ord[mij],ord[i],ord[i],ord[i]))
{
ans=mij;
dr=mij-1;
}
else
st=mij+1;
}
limle[ord[i]] = ord[ans];
}
}
int tole[1005],tori[1005];
int limle[1005],limri[1005];
int calc(int le, int ri)
{
if(le>ri)
return -1;
if(le==ri)
{
tole[le]=tori[le]=-1;
return le;
}
for(int root=le;root<=ri;root++)
{
if(limle[root]<=le && limri[root]>=ri)
{
tole[root] = calc(le,root-1);
tori[root] = calc(root+1,ri);
return root;
}
}
return -5;
}
int solve(int N, int* Left, int* Right)
{
ord.clear();
for(int i=0;i<N;i++)
ord.push_back(i);
precalc(limle);
reverse(ord.begin(),ord.end());
precalc(limri);
//for(int i=0;i<N;i++) cout<<i<<" "<<limle[i]<<" "<<limri[i]<<" lim\n";
for(int i=0;i<N;i++)
tole[i]=tori[i]=-1;
int root = calc(0,N-1);
for(int i=0;i<N;i++)
{
Left[i]=tole[i];
Right[i]=tori[i];
}
return root;
}
/**
6
12 4 16 2 2 20
*/
int _left[1005],_right[1005];
signed main()
{
cin>>_n;
for(int i=0;i<_n;i++)
cin>>_s[i];
int root = solve(_n,_left,_right);
cout<<root<<" the root\n";
for(int i=0;i<_n;i++)
{
cout<<i<<" "<<_left[i]<<" "<<_right[i]<<" left and right\n";
}
return 0;
}