[Bzoj5020] [THUWC 2017] swim in the wonderful realm of mathematics in

Description

Numbers and mathematical laws dominate the world.
Operation of the machine,
growth and decline of life,
the process of the universe,
the mysterious and wonderful process can be unfolded with all the language of mathematics.
This confirms an old saying:
"learn physics and chemistry, traveled world are not afraid."
Mathematics School at the University of slag is small R abuse was life can not take care of themselves, calculus score was the lowest on him in the classroom lesson Minute. However, his roommate someone surnamed Chen was able to easily get full marks in mathematics exam. In order to enhance their math lesson, one night (in his sleep), he came to the realm of mathematics.
Number of real realm of mathematics, each person's IQ can be a part of [0,1] FIG. The realm of mathematics there are n cities, numbered from 0 to n-1, these cities are connected by several bridges magic seats. The center of each city has a magic ball, magic ball each in possession of a math problem. Everyone After doing the maths problem will get a score in the [0,1] interval. A question may be a mapping from [0,1] to [0,1] of the function f (x) represented by. If after a person's IQ is x, then he finished this maths problem will get f (x) points. The function f has three forms:
a sine function sin (ax + b) (a∈ [0,1], b∈ [0, π], a + b∈ [0, π])
exponential function e ^ (ax + b) (a∈ [-1,1], b∈ [ -2,0], a + b∈ [-2,0])
a linear function ax + b (a∈ [-1,1] , b∈ [0, 1], a + b∈ [0,1 ]
magic Kingdom in mathematics bridge will change, sometimes there will be a magic bridge disappear, sometimes a magic bridge appear. but at any time, there is only one connection at most any two simple path cities (that is, all the city form a forest). in the initial case, the realm of mathematics does not exist any magic bridge.
King of the realm of mathematics Lagrange be happy to teach mathematics small R, but only a small R to answer questions first king. These questions have the same form, namely a IQ of people x travel from city u to city v (ie after u to all the cities on the v this path, including the u and v) and do a math problem in all cities his score is the sum of all how much.

Input

The first line of two positive integers n, m and a string type.
Represents the realm of mathematics there are n cities, occurred m events, the data type is type.
typet string is to make everyone better access to sub-section, you may not need to use this input.
The detailed meanings are explained in [] in the data range and tips.
Next n lines, i-th row represents the initial case numbers as a function of the city's i magic ball in.
Represents an integer with a magic type of function f, two real numbers a, b represents a parameter, if
f = 1, then the function f (x) = sin (ax + b) (a∈ [0,1], b ∈ [0, [pi]], A + b ∈ [0, [pi]])
F = 2, then the function f (x) = e ^ ( ax + b) (a∈ [-1,1], b∈ [ -2,0], A + b ∈ [-2,0])
F =. 3, the function f (x) = ax + b (a∈ [-1,1], b∈ [0,1], a + b∈ [0,1])
Subsequently m rows, each row describes an event, the event is divided into four categories.
appear uv represents the realm of mathematics appeared in magic bridge (0≤u, v <n, u ≠ v) a connecting u and v two cities, ensure before connecting u and v two cities can not reach each other.
disappear uv represents the realm of mathematics in connection with u and v two cities magic bridge disappeared, to ensure this magic bridge is there.
magic cfab c represents the city's magic ball magic into a type is f, the parameters a, b function
travel uvx IQ of a person presents to ask x u travel from city to city v
(ie v u to go through this All cities on the route, including the u and v) after he scored the sum is.
If you can not reach from v u, then the output line of a string unreachable.
1≤n≤100000,1≤m≤200000

Output

For each query, a real output line, represents the sum of the score.

Sample Input

3 7 C1

1 1 0

3 0.5 0.5

3 -0.5 0.7

appear 0 1

travel 0 1 0.3

appear 0 2

travel 1 2 0.5

disappear 0 1

appear 1 2

travel 1 2 0.5

Sample Output

9.45520207e-001

1.67942554e + 000

1.20000000e+000


idea

It is a combo title.
Even the tree edge, border erase operation reminiscent \ (LCT \)

The need to maintain and score on the path that \ (LCT \) in a \ (splay \) in scoring and.

For a linear function \ (AX + B \) , will be apparent to maintain \ (X \) of the first term and the constant term and the coefficients.
However \ (sin (ax + b) \) and \ (e ^ {ax + b } \) how to do it?

Taylor expansion!
Simply interpreted as $ f (x) = \ sum \ limits_ {i = 0} ^ n \ frac {f ^ {(i)} (x0) \ times (x-x0) ^ i} {i!} $
Order \ (X0 = 0 \) , \ (n-\) probably takes almost 13 near wildly.

Derivation "guide a guide" them. (Too lazy to hit [Behind his])
so maintenance \ (x \) is 0-13 times coefficient and can ~


Code

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
 
using namespace std;
 
int read(){
    int x=0;
    char ch=getchar();
    while(!isdigit(ch)) ch=getchar();
    while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();
    return x;
}
 
const int N = 100005;
typedef double db;
 
int n,m;
int ff[N];
db aa[N],bb[N],val[N][15],mul[15];
 
int rt[N],ch[N][2],pa[N],rev[N];
db sum[N][15];
 
void cal(int x){//rt[x]=1
    for(int i=0;i<15;i++) sum[x][i]-=val[x][i];
    if(ff[x]==3) {
        val[x][0]=bb[x]; val[x][1]=aa[x];
        for(int i=2;i<15;i++) val[x][i]=0.0;
    }
    else if(ff[x]==2){
        db ee=exp(bb[x]),sa=1.0;
        for(int i=0;i<15;i++){
            val[x][i]=sa*ee/mul[i];
            sa*=aa[x];
        }
    }
    else{
        db sa=1.0,ss[4];
        ss[0]=sin(bb[x]); ss[1]=cos(bb[x]); ss[2]=-sin(bb[x]); ss[3]=-cos(bb[x]);
        for(int i=0;i<15;i++){
            val[x][i]=sa/mul[i]*ss[i%4];
            sa*=aa[x];
        } 
    }
    for(int i=0;i<15;i++) sum[x][i]+=val[x][i];
}
 
void update(int x){
    for(int i=0;i<15;i++) sum[x][i]=sum[ch[x][0]][i]+sum[ch[x][1]][i]+val[x][i];
}
void pushdown(int x){
    if(!rev[x]) return;
    swap(ch[x][0],ch[x][1]);
    rev[ch[x][0]]^=1;
    rev[ch[x][1]]^=1;
    rev[x]=0;
}
int st[N],top;
void push(int x){
    top=0;
    st[++top]=x;
    while(!rt[x]) x=pa[x],st[++top]=x;
    for(int i=top;i>0;i--) pushdown(st[i]);
}
void rotate(int x,int ty){
    int fa=pa[x],gp=pa[fa],son=ch[x][ty^1];
    ch[fa][ty]=son; if(son) pa[son]=fa;
    ch[x][ty^1]=fa; pa[fa]=x;
    pa[x]=gp;
    if(rt[fa]) rt[fa]=0,rt[x]=1;
    else ch[gp][fa==ch[gp][1]]=x; /**/
    update(fa); update(x);
}
void splay(int x){
    push(x);
    while(!rt[x]){
        if(rt[pa[x]]) rotate(x,x==ch[pa[x]][1]);
        else{
            int fa=pa[x],gp=pa[fa];
            int f=fa==ch[gp][0];
            if(x==ch[fa][f]) rotate(x,f),rotate(x,!f);
            else rotate(fa,!f),rotate(x,!f);
        }
    }
}
int access(int x){
    int y=0;
    while(x){
        splay(x);
        rt[ch[x][1]]=1;
        rt[y]=0; ch[x][1]=y;
        update(x);
        y=x; x=pa[x];
    }
}
void makeroot(int x) { access(x); splay(x); rev[x]^=1; }
int find(int x){
    access(x); splay(x);
    while(ch[x][0]) x=ch[x][0];
    splay(x);
    return x;
}
void link(int x,int y) { makeroot(x); pa[x]=y; }
void cut(int x,int y){
    makeroot(x); access(y); splay(y);
    ch[y][0]=0;
    rt[x]=1; pa[x]=0;
    update(y);
}
db ans;
bool Sum(int x,int y,db c){
    if(find(x)!=find(y)) return false;
    makeroot(x); access(y); splay(y);
    db cnow=1.0; ans=0.0;
    for(int i=0;i<15;i++){
        ans+=sum[y][i]*cnow;
        cnow*=c;
    }
    return true;
}
 
int main()
{
    char ss[10];
    n=read(); m=read(); scanf("%s",ss);
    mul[0]=1;
    for(int i=1;i<15;i++) mul[i]=mul[i-1]*i;
    for(int i=1;i<=n;i++){
        ff[i]=read();
        scanf("%lf%lf",&aa[i],&bb[i]);
        cal(i); rt[i]=1;
    }
     
    int x,y;
    db c;
    while(m--){
        scanf("%s",ss);
        if(ss[0]=='a') link(read()+1,read()+1);
        else if(ss[0]=='d') cut(read()+1,read()+1);
        else if(ss[0]=='m'){
            x=read()+1;
            ff[x]=read(); scanf("%lf%lf",&aa[x],&bb[x]);
            splay(x); cal(x);
        }
        else {
            x=read()+1; y=read()+1;
            scanf("%lf",&c);
            if(Sum(x,y,c)) printf("%.8e\n",ans);
            else printf("unreachable\n");
        }
    }
     
    return 0;
}

Guess you like

Origin www.cnblogs.com/lindalee/p/12075261.html