Javascript 设计模式系统讲解与应用

110 阅读2分钟

download:Javascript 设计模式系统讲解与应用

从“写好代码”到“设计代码”的过程,不仅是技术的提升,更是编程思维的提升,而这其中最关键的就是设计模式,是否理解并掌握设计模式,也是衡量程序员能力的标准之一。

适合人群
工作1-3年的前端工程师,或者基础较好的应届毕业生

技术储备要求
了解面向对象思想,熟练使用jQuery或类似工具库,
有 ES6 语法基础,用过 Node.js 和 npm ,
了解 React 和 Vue 的用法(看过文档、做过 demo)
IL void access(int x) {//打通從根節點到x的途径,x最深
for(R y=0; x; x=fa(y=x)) {
splay(x),rs(x)=y,pushup(x);
}
}
Make_root(x):將x變成根節點

IL void makeroot(int x) {//把x變爲原樹的根
access(x),splay(x),Rev(x);
}
Find_root(x):找到x所在的根節點

IL int findroot(int x) {//找到x的原樹的根
access(x),splay(x);
while(ls(x)) pushdown(x),x=ls(x);
return x;
}
Spilt(x,y):將x到y的途径變成實邊途径

IL void split(int x,int y) {//y維護x-y途径上的信息
makeroot(x),access(y),splay(y);
}
Link(x,y):若x,y不連通,則參加(x,y)這條邊

IL void link(int x,int y) {
makeroot(x);if(findroot(y)!=x) fa(x)=y;
}
Cut(x,y):若x,y之間有邊,則删掉該邊

IL void cut(int x,int y) {
split(x,y);
if(fa(x)==y&&rs(x)==0) fa(x)=ls(y)=0,pushup(y);
}
Isroot(x):判別x能否是所在splay的根節點

IL int nroot(int x) //返回1阐明x不是根,返回0阐明x是根
{return ls(fa(x))==x||rs(fa(x))==x;}
P3690 【模板】Link Cut Tree (動態樹)
萌新寫代码~码風跟喻隊學的

![]()![]()```
1 #include<bits/stdc++.h>
2 #define IL inline
3 #define R register int
4 #define ls(x) a[x].ch[0]
5 #define rs(x) a[x].ch[1]
6 #define fa(x) a[x].fa
7
8 using namespace std;
9 const int N=1e5+5,inf=0x3f3f3f3f;
10
11 IL int read() {
12 int f=1;
13 char ch;
14 while((ch=getchar())<'0'||ch>'9') if(ch=='-') f=-1;
15 int res=ch-'0';
16 while((ch=getchar())>='0'&&ch<='9') res=res10+ch-'0';
17 return resf;
18 }
19
20 int n,m;
21 struct hh {
22 int ch[2],fa,val,rev,sum;
23 } a[N];
24
25 IL int chk(int x) {return x==rs(fa(x));}
26 IL void Rev(int x) {swap(ls(x),rs(x));a[x].rev^=1;}
27 IL void pushup(int x) {a[x].sum=a[ls(x)].sum^a[rs(x)].sum^a[x].val;}
28 IL int nroot(int x) //返回1阐明x不是根,返回0阐明x是根
29 {return ls(fa(x))==x||rs(fa(x))==x;}
30
31 IL void pushdown(int x) {
32 if(a[x].rev) {
33 a[x].rev=0;
34 if(ls(x)) Rev(ls(x));
35 if(rs(x)) Rev(rs(x));
36 }
37 }
38
39 IL void pushall(int x) {