洛谷 P4546 & bzoj 5020 在美妙的数学王国中畅游 —— LCT+泰勒展开
题目:https://www.luogu.org/problemnew/show/P4546
先写了个55分的部分分,直接用LCT维护即可,在洛谷上拿了60分;
注意各处 pushup,而且 splay 维护的是一条链但其形态不一定是一条链!
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int rd()
{
int ret=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=; ch=getchar();}
while(ch>=''&&ch<='')ret=ret*+ch-'',ch=getchar();
return f?ret:-ret;
}
typedef double db;
int const xn=1e5+;
int n,m,hd[xn],f[xn],fa[xn],c[xn][],rev[xn];
db A[xn],B[xn],sum[xn];
char tp[],op[];
bool isroot(int x){return c[fa[x]][]!=x&&c[fa[x]][]!=x;}
db cal(int x,db v){db y=A[x]*v+B[x]; if(f[x]==)return sin(y); if(f[x]==)return exp(y); return y;}
void pushup(int x){sum[x]=cal(x,)+sum[c[x][]]+sum[c[x][]];}
void rotate(int x)
{
int y=fa[x],z=fa[y],d=(c[y][]==x);
if(!isroot(y))c[z][c[z][]==y]=x;
fa[x]=z; fa[y]=x; fa[c[x][!d]]=y;
c[y][d]=c[x][!d]; c[x][!d]=y;
pushup(y); pushup(x);
}
int sta[xn],top;
void rever(int x)
{
if(!rev[x])return;
rev[c[x][]]^=; rev[c[x][]]^=;
swap(c[x][],c[x][]); rev[x]=;
}
void splay(int x)
{
sta[top=]=x;
for(int i=x;!isroot(i);i=fa[i])sta[++top]=fa[i];//i!
for(int i=top;i;i--)rever(sta[i]);
while(!isroot(x))
{
int y=fa[x],z=fa[y];
if(!isroot(y))
((c[y][]==x)^(c[z][]==y))?rotate(x):rotate(y);
rotate(x);
}
}
void access(int x)
{
for(int t=;x;c[x][]=t,pushup(x),t=x,x=fa[x])splay(x);//pushup
}
void makeroot(int x)
{
access(x); splay(x); rev[x]^=;
}
int find(int x)
{
access(x); splay(x); while(c[x][])x=c[x][]; return x;
}
void link(int x,int y)
{
makeroot(x); fa[x]=y;
}
void cut(int x,int y)
{
makeroot(x); access(y); splay(y);
fa[x]=; c[y][]=; pushup(y);
}
db dfs(int x,db v)
{
db ret=cal(x,v);
if(c[x][])ret+=dfs(c[x][],v);
if(c[x][])ret+=dfs(c[x][],v);
return ret;
}
void query1(int x,int y,db v)
{
if(find(x)!=find(y)){puts("unreachable"); return;}
makeroot(x); access(y); splay(y); db ret=;
//for(int t=x;t!=fa[y];t=fa[t])ret+=cal(t,v);
//printf("%.8e\n",ret);
printf("%.8e\n",dfs(y,v));
}
void query2(int x,int y)
{
if(find(x)!=find(y)){puts("unreachable"); return;}
makeroot(x); access(y); splay(y);
printf("%.8e\n",sum[y]);
}
int main()
{
n=rd(); m=rd(); scanf("%s",tp);
for(int i=;i<=n;i++)f[i]=rd(),scanf("%lf%lf",&A[i],&B[i]);
for(int i=,u,v;i<=m;i++)
{
scanf("%s",op);
if(op[]=='a'){u=rd()+; v=rd()+; link(u,v);}
else if(op[]=='d'){u=rd()+; v=rd()+; cut(u,v);}
else if(op[]=='m'){int x=rd()+; access(x); splay(x); f[x]=rd(); scanf("%lf%lf",&A[x],&B[x]); pushup(x);}//a,s,p
else
{
u=rd()+; v=rd()+; db x; scanf("%lf",&x);
if(tp[]=='')query2(u,v);
else query1(u,v,x);
}
}
}
55(60)分
参考了博客:https://www.cnblogs.com/zhoushuyu/p/8148732.html
A[x] 写成 A[i] 看了一个小时...
代码如下:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef double db;
int rd()
{
int ret=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=; ch=getchar();}
while(ch>=''&&ch<='')ret=ret*+ch-'',ch=getchar();
return f?ret:-ret;
}
int const xn=1e5+;
int n,m,f[xn],fa[xn],c[xn][],jc[],rev[xn];
db A[xn],B[xn],g[xn][];
char tp[],op[];
bool isroot(int x){return c[fa[x]][]!=x&&c[fa[x]][]!=x;}
db t[];
void pushup(int x)
{
db p=;
if(f[x]==)
{
t[]=sin(B[x]); t[]=cos(B[x]); t[]=-t[]; t[]=-t[];
for(int i=;i<=;i++,p=p*A[x])
g[x][i]=p*t[i%]+g[c[x][]][i]+g[c[x][]][i];
}
if(f[x]==)
{
db tmp=exp(B[x]);
for(int i=;i<=;i++,p=p*A[x])
g[x][i]=p*tmp+g[c[x][]][i]+g[c[x][]][i];
}
if(f[x]==)
{
g[x][]=B[x]+g[c[x][]][]+g[c[x][]][];
g[x][]=A[x]+g[c[x][]][]+g[c[x][]][];
for(int i=;i<=;i++)g[x][i]=g[c[x][]][i]+g[c[x][]][i];
}
}
void rotate(int x)
{
int y=fa[x],z=fa[y],d=(c[y][]==x);
if(!isroot(y))c[z][c[z][]==y]=x;
fa[x]=z; fa[y]=x; fa[c[x][!d]]=y;
c[y][d]=c[x][!d]; c[x][!d]=y;
pushup(y); pushup(x);
}
int sta[xn],top;
void rever(int x)
{if(!rev[x])return; rev[c[x][]]^=; rev[c[x][]]^=; swap(c[x][],c[x][]); rev[x]=;}
void splay(int x)
{
sta[top=]=x;
for(int i=x;!isroot(i);i=fa[i])sta[++top]=fa[i];
for(int i=top;i;i--)rever(sta[i]);
while(!isroot(x))
{
int y=fa[x],z=fa[y];
if(!isroot(y))
((c[y][]==x)^(c[z][]==y))?rotate(x):rotate(y);
rotate(x);
}
}
void access(int x)
{
for(int t=;x;c[x][]=t,pushup(x),t=x,x=fa[x])splay(x);
}
void makeroot(int x)
{
access(x); splay(x); rev[x]^=;
}
int find(int x)
{
access(x); splay(x); while(c[x][])x=c[x][]; return x;
}
void link(int x,int y)
{
makeroot(x); fa[x]=y;
}
void cut(int x,int y)
{
makeroot(x); access(y); splay(y); fa[x]=c[y][]=; pushup(y);
}
void query(int x,int y,db v)
{
if(find(x)!=find(y)){puts("unreachable"); return;}
makeroot(x); access(y); splay(y);
db ret=,p=; for(int i=;i<=;i++,p=p*v)ret+=g[y][i]*p/jc[i];
printf("%.8e\n",ret);
}
int main()
{
n=rd(); m=rd(); scanf("%s",tp);
for(int i=;i<=n;i++)f[i]=rd(),scanf("%lf%lf",&A[i],&B[i]);
jc[]=; for(int i=;i<=;i++)jc[i]=jc[i-]*i;
for(int i=,u,v;i<=m;i++)
{
scanf("%s",op);
if(op[]=='a'){u=rd()+; v=rd()+; link(u,v);}
if(op[]=='d'){u=rd()+; v=rd()+; cut(u,v);}
if(op[]=='m'){int x=rd()+; splay(x); f[x]=rd(); scanf("%lf%lf",&A[x],&B[x]); pushup(x);}//A[x] not A[i]!
if(op[]=='t'){u=rd()+; v=rd()+; db x; scanf("%lf",&x); query(u,v,x);}
}
return ;
}
洛谷 P4546 & bzoj 5020 在美妙的数学王国中畅游 —— LCT+泰勒展开的更多相关文章
- bzoj 5020(洛谷4546) [THUWC 2017]在美妙的数学王国中畅游——LCT+泰勒展开
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=5020 https://www.luogu.org/problemnew/show/P4546 ...
- 题解 洛谷 P4546 【[THUWC2017]在美妙的数学王国中畅游】
首先发现有连边和删边的操作,所以我们肯定要用\(LCT\)来进行维护. 接下来考虑如何进行\(LCT\)上的信息合并. \(f=1\),则函数为\(f(x)=sin(ax+b)\) \(f=2\),则 ...
- 【BZOJ5020】[LOJ2289]【THUWC2017】在美妙的数学王国中畅游 - LCT+泰勒展开
咕咕咕?咕咕咕! 题意: Description 数字和数学规律主宰着这个世界. 机器的运转, 生命的消长, 宇宙的进程, 这些神秘而又美妙的过程无不可以用数学的语言展现出来. 这印证了一句古老的名言 ...
- BZOJ5020: [THUWC 2017]在美妙的数学王国中畅游(LCT,泰勒展开,二项式定理)
Description 数字和数学规律主宰着这个世界. 机器的运转, 生命的消长, 宇宙的进程, 这些神秘而又美妙的过程无不可以用数学的语言展现出来. 这印证了一句古老的名言: ...
- bzoj5020 & loj2289 [THUWC 2017]在美妙的数学王国中畅游 LCT + 泰勒展开
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=5020 https://loj.ac/problem/2289 题解 这个 appear 和 d ...
- 「LOJ 2289」「THUWC 2017」在美妙的数学王国中畅游——LCT&泰勒展开
题目大意: 传送门 给一个动态树,每个节点上维护一个函数为$f(x)=sin(ax+b)$.$f(x)=e^{ax+b}$.$f(x)=ax+b$中的一个. 支持删边连边,修改节点上函数的操作. 每次 ...
- 【BZOJ5020】【THUWC2017】在美妙的数学王国中畅游 LCT 泰勒展开
题目大意 给你一棵树,每个点有一个函数\(f(x)\) 正弦函数 \(\sin(ax+b) (a\in[0,1],b\in[0,\pi],a+b\in[0,\pi])\) 指数函数 \(e^{ax+b ...
- [THUWC2017][bzoj5020] 在美妙的数学王国中畅游 [LCT+泰勒展开]
题面 LOJ传送门 思路 这里很重要 它提示我们,把给定的三个函数泰勒展开,并用LCT维护每一项泰勒展开式的值,维护十几项就满足了题目的精度要求 我们考虑一个函数在0位置的泰勒展开 $f(x)=\su ...
- [THUWC2017]在美妙的数学王国中畅游 LCT+泰勒展开+求导
p.s. 复合函数求导时千万不能先带值,再求导. 一定要先将符合函数按照求导的规则展开,再带值. 设 $f(x)=g(h(x))$,则对 $f(x)$ 求导: $f'(x)=h'(x)g'(h(x)) ...
随机推荐
- 查看Android.mk文件中的变量的值
当某个Android.mk中包含如下: LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_C_INCLUDES += \ $(LOCAL ...
- Java中线程和线程池
Java中开启多线程的三种方式 1.通过继承Thread实现 public class ThreadDemo extends Thread{ public void run(){ System.out ...
- Alamofire 小试牛刀
1.定义请求头 let headers: HTTPHeaders = [ "token": "W4SIjb3gHgJqgG8LRWj0jM==", " ...
- Java基础 - 常量与变量
A:常量 内存中的一小块区域,在程序执行过程中,其值不可以发生改变的量称为常量 常量的几种表现形式: a:字符串常量 "HelloWorld" b:整数常量 12 c:小数常量 1 ...
- [APIO2008]免费道路
[APIO2008]免费道路 BZOJ luogu 先把必须连的鹅卵石路连上,大于k条no solution 什么样的鹅卵石路(u,v)必须连?所有水泥路都连上仍然不能使u,v连通的必须连 补全到k条 ...
- 【android】在Service的onStartCommand()中调用stopself()应该注意的问题
在Service的onStartCommand()中调用stopself()后并不会立马destroy掉service,而是等onStartCommand()运行完才destroy. public c ...
- Bootstrap学习5--bootstrap中的模态框(modal,弹出层)
bootstrap中的模态框(modal),不同于Tooltips,模态框以弹出对话框的形式出现,具有最小和最实用的功能集. 务必将模态框的 HTML 代码放在文档的最高层级内(也就是说,尽量作为 b ...
- vim中使用sed去除网上copy的源代码行号和空格
有些时候,在网上搜索到的代码都包含有行号,高亮显示控件不支持直接提取,如: test.sh 01 #!/bin/bash 02 echo “aaa” 简单的去掉行号和前面的空格: 方案一: 1.vim ...
- python基础22------python基础之基于tcp和udp的套接字
一.TCP套接字 1.low版tcp套接字 服务器端 客户端 2.改进版tcp套接字 服务端 客户端 二.UDP的套接字 服务器 客户端 注:udp的套接字可以支持多个客户端同时访问,但tcp套接字就 ...
- 如何让Jackson JSON生成的数据包含的中文以unicode方式编码
我们都知道,Jackson JSON以高速.方便和灵活著称.之前的文章中介绍过使用注解的形式来规定如何将一个对象序列化成JSON的方法,以及如何将一个JSON数据反序列化到一个对象上.但是美中不足的一 ...