跳到正文

个人记录

代码库

Kayisama约 7 分钟
原文
文章信息与版本

文章信息

收录
2026年5月3日
更新
2026年5月3日
篇幅
约 7 分钟

版本

  1. v2当前
  2. v1

如此强力,令人震撼

复数:

C++
struct cmp{
	using ld=long double;
	int r,i;//实部 虚部
	cmp(int aa=0,int bb=0){r=aa,i=bb;}
	friend cmp operator+(cmp x,cmp y){
		int a=x.r,b=x.i,c=y.r,d=y.i;
		return cmp(a+c,b+d	);
	}
	friend cmp operator-(cmp x,cmp y){
		int a=x.r,b=x.i,c=y.r,d=y.i;
		return cmp(a-c,b-d);
	}
	friend cmp operator*(cmp x,cmp y){
		int a=x.r,b=x.i,c=y.r,d=y.i;
		return cmp(a*c-b*d,a*d+b*c);
	}
	friend cmp operator/(cmp x,int y){
		ld a=x.r,b=x.i,Y=y;
		return cmp(round(a/Y),round(b/Y));
	}
	friend cmp operator/(cmp x,cmp y){
		int c=y.r,d=y.i;
		return x*(y.bar())/(c*c+d*d);
	}
	friend cmp operator%(cmp x,cmp y){
		cmp tmp=(x/y)*y;
		return x-tmp;
	}
	friend bool operator==(cmp x,cmp y){
		int a=x.r,b=x.i,c=y.r,d=y.i;
		return (a==c) && (b==d);
	}
	int len(){return r*r+i*i;}//长度
	cmp bar(){return cmp(r,-i);}//共轭
};

凸包

C++
struct node{//存储点
	double x,y;
	friend bool operator<(node a,node b){
		if (a.x==b.x) return a.y<b.y;
		return a.x<b.x;
	}
	friend bool operator==(node a,node b){
		return a.x==b.x && a.y==b.y;
	}
};
struct vec{//向量
	double x,y;
	vec(double a,double b){
		x=a,y=b;
	}
	bool check(vec t){
		return (x*t.y-y*t.x)>0;//是否为左转
	}
};
double dist(node a,node b){//两点间距离
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
bool duel(node a,node b,node p){//以a点为线段的一个端点,AB和AP是否顺时针转过了一个角度
	vec ab(b.x-a.x,b.y-a.y);
	vec ap(p.x-a.x,p.y-a.y);
	return ab.check(ap);
}
node p[maxn];
node sta[maxn];
int top;
void Andrew(){//Andrew算法求凸包,凸包存储在sta[1]到sta[top]之间(因为闭合,所以sta[1]=sta[top])
	sort(p+1,p+1+n);
	sta[1]=p[1];
	sta[2]=p[2];
	top=2;
	for (int i=3;i<=n;i++){//升序下凸壳
		while (top>1 && !duel(sta[top-1],sta[top],p[i])) top--;
		sta[++top]=p[i];
	}
	int ttop=top;
	sta[++top]=p[n-1];
	for (int i=n-2;i;i--){//降序下凸壳
		while (top>ttop && !duel(sta[top-1],sta[top],p[i])) top--;
		sta[++top]=p[i];
	}
	return;
}

向量

C++
struct vec{
	double x,y;
	friend vec operator+(vec a,vec b){
		return {a.x+b.x,a.y+b.y};
	}
	friend vec operator-(vec a,vec b){
		return {a.x-b.x,a.y-b.y};
	}
	friend vec operator*(vec a,vec b){//点乘
		return a.x*a.x-a.y*a.y;
	}
	friend bool operator==(vec a,vec b){
		return a.x==b.x && a.y==b.y;
	}
};

LCT

C++
struct LCT{
	#define ls(x) c[x][0]
	#define rs(x) c[x][1]
	int f[maxn],c[maxn][3];
	stack<int> sta;
	bool r[maxn];
	bool nroot(int x){
		return !(ls(f[x])!=x && rs(f[x])!=x);
	}
	void pushup(int x){
		//write something
		return;
	}
	void pushr(int x){
		swap(ls(x),rs(x));
		r[x]^=1;
		return;
	}
	void pushdown(int x){
		if (r[x]){
			if (ls(x)) pushr(ls(x));
			if (rs(x)) pushr(rs(x));
			r[x]=0;
		}
		return;
	}

	void rot(int x){
		int y=f[x],z=f[y],k=rs(y)==x,w=c[x][!k];
		if (nroot(y)) c[z][rs(z)==y]=x;
		c[x][!k]=y;
		c[y][k]=w;
		if (w) f[w]=y;
		f[y]=x;f[x]=z;
		pushup(y);pushup(x);
		return;
	}
	void splay(int x){
		int y=x;
		for (;!sta.empty();sta.pop());
		sta.push(x);
		for (;nroot(y);sta.push(y=f[y]));
		for (;!sta.empty();pushdown(sta.top()),sta.pop());
		for (;nroot(x);){
			int y=f[x],z=f[y];
			if (nroot(y)) rot(((x==rs(y))^(y==rs(z)))?x:y);
			rot(x);
		}
		pushup(x);
		return;
	}
	void access(int x){
		for (int y=0;x;x=f[y=x]){
			splay(x);
			rs(x)=y;
			pushup(x);
		}
		return;
	}
	void makeroot(int x){
		access(x);splay(x);
		pushr(x);
		return;
	}
	int findroot(int x){
		access(x);splay(x);
		for (;ls(x);pushdown(x),x=ls(x));
		splay(x);
		return x;
	}
	void split(int x,int y){
		makeroot(x);access(y);
		splay(y);
		return;
	}
	void link(int x,int y){
		makeroot(x);
		if (findroot(y)!=x) f[x]=y;
		return;
	}
	void cut(int x,int y){
		makeroot(x);
		if (findroot(y)==x && f[y]==x && !ls(y)){
			f[y]=rs(x)=0;
			pushup(x);
		}
		return;
	}
  #undef ls
  #undef rs
};
原文