题解
2023-07-17 09:51:01
发布于:广东
23阅读
0回复
0点赞
#include<bits/stdc++.h>
#define ll long long
#define re register
#define cs const
namespace IO{
inline char gc(){
static cs int Rlen=1<<22|1;static char buf[Rlen],*p1,*p2;
return (p1==p2)&&(p2=(p1=buf)+fread(buf,1,Rlen,stdin),p1==p2)?EOF:*p1++;
}template<typename T>T get_integer(){
char c;bool f=false;while(!isdigit(c=gc()))f=c=='-';T x=c^48;
while(isdigit(c=gc()))x=((x+(x<<2))<<1)+(c^48);return f?-x:x;
}inline int gi(){return get_integer<int>();}
char obuf[(int)(3e7+7)],*oh=obuf,ch[23];
template<typename T>void print(T a,char c){
if(a<0)a=-a,*oh++='-';int tl=0;
do ch[++tl]=a%10; while(a/=10);
while(tl)*oh++=ch[tl--]^48;*oh++=c;
}struct obuf_flusher{~obuf_flusher(){fwrite(obuf,1,oh-obuf,stdout);}}Flusher;
}using namespace IO;
using std::cerr;
using std::cout;
cs int N=2e5+7,LOG=19;
cs double eps=1e-7;
int n,A[N],id[N];
inline bool cmp_id(int i,int j){
return A[i]<A[j];
}inline double inter_x(int i,int j){
return 1.*(A[i]-A[j])/(i-j);
}
int up[LOG][N],dn[LOG][N];
void build(){
static int q[N],qn;q[qn=0]=0;
for(int re i=n;i;--i){
int p=id[i];
while((qn&&inter_x(q[qn],p)<0)||(qn>1&&inter_x(p,q[qn])>inter_x(q[qn-1],q[qn])))--qn;
dn[0][p]=q[qn];q[++qn]=p;
}q[qn=0]=0;
for(int re i=1;i<=n;++i){
int p=id[i];
while((qn&&inter_x(q[qn],p)<0)||(qn>1&&inter_x(p,q[qn])>inter_x(q[qn-1],q[qn])))--qn;
up[0][p]=q[qn];q[++qn]=p;
}
for(int re i=1;i<LOG;++i)
for(int re j=1;j<=n;++j){
up[i][j]=up[i-1][up[i-1][j]];
dn[i][j]=dn[i-1][dn[i-1][j]];
}
}
bool go_up(int u,int v){
if(!up[0][u])return false;
double x=inter_x(up[0][u],u);
double y=A[u]-u*x;
return y+eps<=A[v]-x*v;
}
bool go_dn(int u,int v){
if(!dn[0][u])return false;
double x=inter_x(dn[0][u],u);
double y=A[u]-u*x;
return y-eps>=A[v]-x*v;
}
void Main(){n=gi();
for(int re i=1;i<=n;++i)
A[i]=gi(),id[i]=i;
std::sort(id+1,id+n+1,cmp_id);build();
for(int re i=1;i<=n;++i){
int q=gi(),u=i;
if(A[u]<A[q]){
if(go_up(u,q)){
for(int re i=LOG-1;~i;--i)
if(go_up(up[i][u],q))u=up[i][u];
u=up[0][u];
if(u>q)print(-1,'\n');
else {
int x=A[q]-A[u],y=q-u;
int g=std::__gcd(x,y);
print(x/g,'/');print(y/g,'\n');
}
}else {
if(u>q)print(-1,'\n');
else {
int x=A[q]-A[u],y=q-u;
int g=std::__gcd(x,y);
print(x/g,'/');print(y/g,'\n');
}
}
}else {
if(go_dn(u,q)){
for(int re i=LOG-1;~i;--i)
if(go_dn(dn[i][u],q))u=dn[i][u];
u=dn[0][u];
if(u<q)print(-1,'\n');
else {
int x=A[u]-A[q],y=u-q;
int g=std::__gcd(x,y);
print(x/g,'/');print(y/g,'\n');
}
}else {
if(u<q)print(-1,'\n');
else {
int x=A[u]-A[q],y=u-q;
int g=std::__gcd(x,y);
print(x/g,'/');print(y/g,'\n');
}
}
}
}
}
inline void file(){
#ifdef zxyoi
freopen("falling.in","r",stdin);
#else
#ifndef ONLINE_JUDGE
freopen("falling.in","r",stdin);
freopen("falling.out","w",stdout);
#endif
#endif
}signed main(){file();Main();return 0;}
这里空空如也
有帮助,赞一个