[THUWC 2017]在美妙的數學王國中暢遊

傳送門

每個函數泰勒展開,階乘預處理

後面的項對答案的影響很小可以忽略

LCTLCT板子維護路徑和

有關導數

以後交題解不敢壓行了

#include<bits/stdc++.h>
#define il inline
using namespace std;
const int N=1e5+5,L=11;
int n,m,T,stk[N],ch[N][2],fa[N],w[N],rev[N];
double a[N][L],sum[N][L];
long long fc[L];
char s[20];
il void psu(int x) {
    for(int i=0; i<L; ++i) sum[x][i]=sum[ch[x][0]][i]+sum[ch[x][1]][i]+a[x][i];
}
il bool isr(int x) {
    return ch[fa[x]][0]!=x&&ch[fa[x]][1]!=x;
}
il void psd(int x) {
    if(rev[x]) swap(ch[x][0],ch[x][1]),rev[ch[x][0]]^=1,rev[ch[x][1]]^=1,rev[x]^=1;
}
il void rtt(int x) {
    int y=fa[x],z=fa[y],d=(ch[y][1]==x);
    if(!isr(y)) ch[z][ch[z][1]==y]=x;
    fa[x]=z,fa[y]=x;
    fa[ch[x][d^1]]=y,ch[y][d]=ch[x][d^1],ch[x][d^1]=y;
    psu(y),psu(x);
}
il void spl(int x) {
    stk[T=1]=x;
    for(int i=x; !isr(i); i=fa[i]) stk[++T]=fa[i];
    for(int i=T; i>=1; --i) psd(stk[i]);
    for(int y=fa[x],z=fa[y]; !isr(x); y=fa[x],z=fa[y]) {
        if(!isr(y)) ((ch[z][1]==y)^(ch[y][1]==x))?rtt(x):rtt(y);
        rtt(x);
    }
}
il void acs(int x) {
    int t=0;
    while(x) {
        spl(x),ch[x][1]=t,psu(x),t=x,x=fa[x];
    }
}
il void mkr(int x) {
    acs(x),spl(x),rev[x]^=1;
}
il int fdr(int x) {
    acs(x);
    spl(x);
    psd(x);
    while(ch[x][0]) psd(x=ch[x][0]);
    return x;
}
il void lk(int x,int y) {
    mkr(x);
    if(fdr(y)!=x) fa[x]=y;
}
il void cut(int x,int y) {
    mkr(x);
    if(fdr(y)==x&&fa[x]==y&&ch[y][0]==x&&ch[x][1]==0) fa[x]=ch[y][0]=0;
    psu(y);
}
il double qry(int x,int y,double v) {
    mkr(x),acs(y),spl(y);
    double t=1,ss=0;
    for(int i=0; i<L; ++i) ss+=t*sum[y][i],t*=v;
    return ss;
}
il void trs(int op,double *a,double A,double B) {
    if(op==1) {
        double S=sin(B),C=cos(B),t=1;
        int f;
        for(int i=0; i<L; ++i) f=((i>>1)&1)?-1:1,a[i]=f*t*(i&1?C:S)/fc[i],t*=A;
    } else if(op==2) {
        double eb=exp(B),t=1;
        for(int i=0; i<L; ++i) a[i]=t*eb/fc[i],t*=A;
    } else {
        a[0]=B,a[1]=A;
        for(int i=2; i<L; ++i) a[i]=0;
    }
}
int main() {
    fc[0]=1;
    for(int i=1; i<L; ++i) fc[i]=fc[i-1]*i;
    scanf("%d%d%s",&n,&m,s);
    double A,B,v;
    int x,y;
    for(int i=1,op; i<=n; ++i) scanf("%d",&op),scanf("%lf%lf",&A,&B),trs(op,a[i],A,B);
    while(m--) {
        scanf("%s%d%d",s,&x,&y);
        ++x,++y;
        if(s[0]=='a') lk(x,y);
        else if(s[0]=='d') cut(x,y);
        if(s[0]=='m') scanf("%lf%lf",&A,&B),mkr(x),trs(--y,a[x],A,B),psu(x);
        if(s[0]=='t') scanf("%lf",&v),fdr(x)!=fdr(y)?puts("unreachable"):printf("%.9lf\n",qry(x,y,v));
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章