[THUWC2017]在美妙的数学王国中畅游

luogu
BZOJ

$$(sinx)'=cosx$$
$$(cosx)'=-sinx$$
$$(e^x)'=e^x$$
$$(ax+b)'=a$$
$$(a)'=0$$

#define db double
#include<bits/stdc++.h>
using namespace std;
const int _=1e5+5;
int n,m,top;
char type[20];
int f[_],jc[13],fa[_],rev[_],st[_],ch[2][_];
db a[_],b[_],g[13][_],h[13][_];
int son(int x){return x==ch[1][fa[x]];}
bool isrt(int x){return x^ch[0][fa[x]]&&x^ch[1][fa[x]];}
void rever(int x){if(x)swap(ch[0][x],ch[1][x]),rev[x]^=1;}
void pd(int x){if(rev[x])rever(ch[0][x]),rever(ch[1][x]),rev[x]=0;}
void pu(int x){
for(int i=0;i<=12;i++)
g[i][x]=g[i][ch[0][x]]+g[i][ch[1][x]]+h[i][x];
}
void rotate(int x){
int y=fa[x],z=fa[y],k=son(x);
ch[k][y]=ch[k^1][x];if(ch[k][y])fa[ch[k][y]]=y;
fa[x]=z;if(!isrt(y))ch[son(y)][z]=x;
fa[y]=x;ch[k^1][x]=y;pu(y);
}
void splay(int x){
st[top=1]=x;
for(int i=x;!isrt(i);i=fa[i])st[++top]=fa[i];
while(top)pd(st[top--]);
for(int y=fa[x];!isrt(x);rotate(x),y=fa[x])
if(!isrt(y))rotate(son(x)^son(y)?x:y);
pu(x);
}
void access(int x){for(int y=0;x;y=x,x=fa[x])splay(x),ch[1][x]=y,pu(x);}
void makert(int x){access(x);splay(x);rever(x);}
int findrt(int x){access(x);splay(x);while(ch[0][x])pd(x),x=ch[0][x];splay(x);return x;}
void split(int x,int y){makert(x);access(y);splay(y);}
void cut(int x,int y){split(x,y);ch[0][y]=fa[x]=0;}
db calc(int c,int x){
db res=pow(a[x],c);
if(f[x]==1){
if(c%4==0)return sin(b[x])*res;
if(c%4==1)return cos(b[x])*res;
if(c%4==2)return -sin(b[x])*res;
return -cos(b[x])*res;
}
if(f[x]==2)return exp(b[x])*res;
if(!c)return b[x];
if(c==1)return a[x];
return 0;
}
int main(){
jc[0]=1;
for(int i=1;i<=12;i++)jc[i]=jc[i-1]*i;
scanf("%d%d%s",&n,&m,type);
for(int i=1;i<=n;i++){
scanf("%d%lf%lf",&f[i],&a[i],&b[i]);
for(int j=0;j<=12;j++)
h[j][i]=calc(j,i);
}
int u,v;db x;
while(m--){
scanf("%s",type);
if(type[0]=='a'){
scanf("%d%d",&u,&v);
}
if(type[0]=='d'){
scanf("%d%d",&u,&v);
cut(u+1,v+1);
}
if(type[0]=='m'){
scanf("%d",&u);++u;
scanf("%d%lf%lf",&f[u],&a[u],&b[u]);
makert(u);
for(int i=0;i<=12;i++)h[i][u]=calc(i,u);
pu(u);
}
if(type[0]=='t'){
scanf("%d%d%lf",&u,&v,&x);
++u;++v;
if(findrt(u)^findrt(v)){puts("unreachable");continue;}
db ans=0,res=1;split(u,v);
for(int i=0;i<=12;i++){
ans+=g[i][v]*res/jc[i];
res*=x;
}
printf("%.8e\n",ans);
}
}
return 0;
}

posted @ 2018-12-13 14:29  faced  阅读(241)  评论(0编辑  收藏  举报