跳到正文

个人记录

一些缺省源

cnlihuan约 26 分钟
原文

这是 2026年10月28日 存档的第 7 版,不是当前版本。查看当前版本

文章信息与版本
C++
inline int read()
{
	int x=0,f=1;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		if(ch=='-')f=-1;
		ch=getchar();
	}
	while(ch>='0'&&ch<='9'){
		x=(x<<1)+(x<<3)+(ch-'0');
		ch=getchar();
	}
	return x*f;
}
inline void write(int x)
{
	if(x<0)x*=-1,putchar('-');
	if(x>9)write(x/10);
	putchar(x%10+'0');
	return;
}

//DataStruct

template<int r,int c>
struct Martix{
	int p[r][c];
	Martix(){memset(p,0,sizeof(p));}
	Martix(std::initializer_list<std::initializer_list<int>> t){
		auto it=t.begin();
		for(int i=0;i<r;i++){
			auto jt=(*it).begin();
			for(int j=0;j<c;j++){
				p[i][j]=*jt;
				jt++;
			}
			it++;
		}
	}
	void PutOut(){
		cout<<"Martix:\n";
		for(int i=0;i<r;i++){
			cout<<"{";
			for(int j=0;j<c;j++){
				if(j!=0)cout<<",";
				cout<<p[i][j];
			}
			cout<<"}\n";
		}
	}
	template<int tc>
	Martix<r,tc> operator *(Martix<c,tc> &t){
		Martix<r,tc> res;
		for(int i=0;i<r;i++){
			for(int j=0;j<c;j++){
				for(int k=0;k<tc;k++){
					res.p[i][k]+=p[i][j]*t.p[j][k];
				}
			}
		}
		return res;
	}
}; 

#define lid (id<<1)
#define rid ((id<<1)+1)
template<int _size>
class SGT_OnePointAdd_IntervalQuery{
	struct Node{
		int l,r;
		int x;
	};
	Node p[_size];
	void pu(int id){
		p[id].x=p[lid].x+p[rid].x;
	}
public:
	void build(int id,int l,int r){
		p[id].l=l;p[id].r=r;
		if(l<r){
			build(lid,l,(l+r)/2);
			build(rid,(l+r)/2+1,r);
		}
	} 
	void add(int id,int x,int v){
		if(p[id].l==p[id].r){
			p[id].x+=v;
			return ;
		}
		if(x<=(p[id].l+p[id].r)/2){
			add(lid,x,v);
		}else{
			add(rid,x,v);
		}
		pu(id);
	}
	int query(int id,int l,int r){
		if(l<=p[id].l&&p[id].r<=r){
			return p[id].x;
		}
		int res=0;
		if(l<=(p[id].l+p[id].r)/2){
			res+=query(lid,l,r);
		}
		if(r>(p[id].l+p[id].r)/2){
			res+=query(rid,l,r);
		}
		return res;
	}
};
#undef lid
#undef rid

#define lid (id<<1)
#define rid ((id<<1)+1)
#define INF 0x3f3f3f3f3f3f3f3f
template <int _size>
class KTT{
	struct Line{
		int x;int k;
		Line operator +(Line t){
			Line r;
			r.x=x+t.x;r.k=k+t.k;
			return r;
		}
	}; 
	struct Str{
		Line x;
		int tag;
	}; 
	struct Node{
		int l,r;
		Str x;
		int laz;
	};
	Node p[_size];
	pair<Line,int> GetTag(Line l1,Line l2){
		if(l1.x<l2.x||(l1.x==l2.x&&l1.k<l2.k)){
			swap(l1,l2);
		}
		int x=l1.x-l2.x;
		int k=l2.k-l1.k;
		if(k<=0)return {l1,INF};
		return {l1,(x+k-1)/k};
	}
	Str merge(Str u,Str t){
		Str r;
		auto p=GetTag(u.x,t.x);
		r.x=p.first;
		r.tag=min(p.second,min(u.tag,t.tag));
		return r;
	}
	void pu(int id){
		p[id].x=merge(p[lid].x,p[rid].x);
	}
	void dp(int id,int laz){
		p[id].laz+=laz; p[id].x.tag-=laz;
		p[id].x.x.x+=p[id].x.x.k*laz;
		if(p[id].x.tag<=0){
			pd(id);
			pu(id);
		}
	}
	void pd(int id){
		dp(lid,p[id].laz);
		dp(rid,p[id].laz);
		p[id].laz=0;
	}
public:
	void build(int id,int l,int r,int *x,int *k){
		p[id].l=l;p[id].r=r;p[id].laz=0;
		if(l==r){
			p[id].x.x.x=x[l];
			p[id].x.x.k=k[l];
			p[id].x.tag=INF;
			return;
		}
		build(lid,l,(l+r)/2,x,k);
		build(rid,(l+r)/2+1,r,x,k);
		pu(id);
	}
	void add(int id,int l,int r,int v){
		if(p[id].l!=p[id].r)pd(id);
		if(l<=p[id].l&&p[id].r<=r){
			dp(id,v);
			return;
		}
		if(l<=(p[id].l+p[id].r)/2){
			add(lid,l,r,v);
		}
		if(r>(p[id].l+p[id].r)/2){
			add(rid,l,r,v);
		}
		pu(id);
	}
	Str query(int id,int l,int r){
		if(p[id].l!=p[id].r)pd(id);
		if(l<=p[id].l&&p[id].r<=r){
			return p[id].x;
		}
		if(r<=(p[id].l+p[id].r)/2){
			return query(lid,l,r);
		}
		if(l>(p[id].l+p[id].r)/2){
			return query(rid,l,r);
		}
		return merge(query(lid,l,r),query(rid,l,r));
	}
};
#undef lid
#undef rid 

#define ERROR 9
template <typename _tp,typename _val>
struct Node{
	Node*l,*r,*ls,*f;
	_tp x; _val v;
	bool mark; int du;
	Node(Node* _l,Node* _r,Node* _ls,Node* _f,_tp _x,_val _v){
		l=_l,r=_r,ls=_ls,f=_f,x=_x;v=_v;mark=0;du=0;
	}
};
template <typename _tp,typename _val>
 class FibonacciHeap {
private:
	int _size;
	 Node<_tp,_val>*_left; Node<_tp,_val>*_right;
	void _cut(Node<_tp,_val>* x){
		if(x->r!=NULL)x->r->l=x->l;
		if(x->l!=NULL)x->l->r=x->r;
		if(x->f!=NULL){
			x->f->du--;
			if(x->f->ls==x){
				x->f->ls=x->r;	
			}
		}
		x->r=NULL;x->l=NULL;x->f=NULL ; 
	}
	void _split(Node<_tp,_val>* x){
		if(_top==NULL){
			_top=x; _left=x; _right=x;
		}
		if(x==NULL||x==_top)return;
		
		if(x->f!=NULL){
			if(x->f->mark&&x->f->f!=NULL){
				_split(x->f);	
			}else{
				x->f->mark=1;
			}
		}
		
		_cut(x);
		
		x->l=_top->l;x->r=_top;
		if(x->l!=NULL)x->l->r=x;
		_top->l=x;
		x->f=NULL;x->mark=0;
		
		if(x->l==NULL)_left=x;
		if(x->r==NULL)_right=x;
		
	}
	void _eraseRoot(Node<_tp,_val>* x){
		_cut(x);
		if(_top==x)_top=NULL;
		delete x;
	}
	vector<Node<_tp,_val>*> _tmp;
	void _merge(Node<_tp,_val>* x,Node<_tp,_val>* y){
		if(x->x>y->x
		||(x->x==y->x&&x->v>y->v)){
			swap(x,y);
		}
		_cut(y);
		y->f=x;
		x->du++;
		y->r=x->ls;
		if(x->ls!=NULL)x->ls->l=y;
		x->ls=y;
		_tmp[x->du]=x;
	}
public:
	Node<_tp,_val>*_top;
	
	
	FibonacciHeap(){
		_size=0;_top=NULL;
	}
	
	Node<_tp,_val>* insert(_tp x,_val v){
		_size++;
		Node<_tp,_val>* w=
			new Node<_tp,_val>(NULL,NULL,NULL,NULL,x,v);
		_split(w);
		if(x<_top->x||(x==_top->x&&v<_top->v)){
			_top=w;
		}
		return w;
	}
	
	pair<_tp,_val> top(){
		return make_pair(_top->x,_top->v);
	}
	
	int size(){
		return _size;
	}
	
	void pop(){
		
		_size--;
		_tmp.resize(_size+4,NULL);
		
		Node<_tp,_val>* w=_top;
		while(w->ls!=NULL)_split(w->ls);
		vector<Node<_tp,_val>*> v;
		Node<_tp,_val>* cur=w;
		while(cur->l!=NULL){
			v.push_back(cur->l);
			cur=cur->l;
		}cur=w;
		while(cur->r!=NULL){
			v.push_back(cur->r);
			cur=cur->r;
		}
		
		_eraseRoot(w);
		for(unsigned int i=0;i<v.size();i++){
			if(_tmp[v[i]->du]!=NULL){
				Node<_tp,_val>* tmp=_tmp[v[i]->du];
				_tmp[v[i]->du]=NULL;
				_merge(v[i],tmp);
			}else{
				_tmp[v[i]->du]=v[i];
			}
		}
		for(unsigned int i=0;i<v.size();i++){
			_tmp[v[i]->du]=NULL;
			if(v[i]->f!=NULL)continue;
			if(_top==NULL||v[i]->x < _top->x
			||(v[i]->x==_top->x&&v[i]->v<_top->v)){
				_top=v[i];
			}
			if(v[i]->l==NULL)_left=v[i];
			if(v[i]->r==NULL)_right=v[i];
		}
		
		_tmp.clear(); _tmp.shrink_to_fit();
	}
	
	void decrease(_tp k,Node<_tp,_val>* w){
		if(w==NULL){
			cerr<<"FUCK!!!"; exit(ERROR);
		}
		if(!(k<w->x||k==w->x)){
			cerr<<"FUCK!!!"; exit(ERROR);
		}
		w->x=k;
		_split(w);
		if(k<_top->x){
			_top=w;
		}
	}
	
	void clear(){
		_size=0;
	}
	
	void merge(FibonacciHeap<_tp,_val> &t){
		if(t._size==0)return ;
		if(_size==0){
			_right=t._right;
			_left=t._left;
			_top=t._top;
		}else{
			_right->r=t._left; t._left->l=_right;
			_right=t._right;
			if(t._top->x<_top->x
			||(t._top->x==_top->x&&t._top->v<_top->v)){
				_top=t._top;
			}
		}
		_size+=t._size;
		t.clear();
	}
};
#undef ERROR

//Tree

template<int _size>
class Tree{
public:
	vector<int> p[_size];
	void add(int x,int y){
		p[x].push_back(y);
	}
	int dfn[_size];int cnt;
	int out[_size];
	void dfs(int x){
		cnt++; dfn[x]=cnt;
		for(int i=0;i<p[x].size();i++){
			dfs(p[x][i]);
		}
		out[x]=cnt;
	}
};

template<int _size>
class HeavyChainCleavage{
public:
	int siz[_size],fa[_size],son[_size],dep[_size];
	int dfn[_size],nfd[_size],out[_size],top[_size],cnt;
	template<int _treeSize>
	void dfs1(int x,Tree<_treeSize> &t){
		siz[x]=1;if(!dep[x])dep[x]=1;
		for(int i=0;i<t.p[x].size();i++){
			if(fa[x]==t.p[x][i])continue;
			fa[t.p[x][i]]=x; dep[t.p[x][i]]=dep[x]+1;
			dfs1(t.p[x][i],t);
			siz[x]+=siz[t.p[x][i]];
			if(siz[t.p[x][i]]>siz[son[x]])son[x]=t.p[x][i];
		}
	}
	template<int _treeSize>
	void dfs2(int x,Tree<_treeSize> &t){
		if(!top[x])top[x]=x;
		cnt++; dfn[x]=cnt; nfd[cnt]=x;
		if(son[x]){
			top[son[x]]=top[x];
			dfs2(son[x],t);
		}
		for(int i=0;i<t.p[x].size();i++){
			if(fa[x]==t.p[x][i]||son[x]==t.p[x][i])continue;
			dfs2(t.p[x][i],t);
		}
		out[x]=cnt;
	}
	template<int _treeSize>
	void build(Tree<_treeSize> &t){
		dfs1(1,t);
		dfs2(1,t);
	}
	int lca(int x,int y){
		if(top[x]!=top[y]){
			if(dep[top[x]]>dep[top[y]]){
				swap(x,y);
			}
		}else{
			if(dep[x]>dep[y]){
				return y;
			}else{
				return x;
			}
		}
		return lca(x,fa[top[y]]);
	}
};

//String

template<int _size>
class SAM{
	int cnt;
	int len[_size*2],link[_size*2],p[_size*2][30];
	
	int _last;
public:
	
	SAM(){cnt=0; memset(p[0],0,sizeof(p[0])); _last=0; }
	
	void Clear(){cnt=0; memset(p[0],0,sizeof(p[0])); _last=0; }
	
	void InsertChar(char C){
		int c=C-'a';
		cnt++; int cur=cnt;
		memset(p[cnt],0,sizeof(p[cnt]));
		len[cur]=len[_last]+1; link[cur]=0; 
		while(!p[_last][c]){
			p[_last][c]=cur; _last=link[_last];
		}
		if(p[_last][c]!=cur){
			if(len[_last]+1==len[p[_last][c]]) link[cur]=p[_last][c];
			else{
				int to=p[_last][c];
				cnt++; len[cnt]=len[_last]+1; link[cnt]=link[to];
				link[to]=cnt; link[cur]=cnt; 
				memcpy(p[cnt],p[to],sizeof(p[cnt]));
				while(p[_last][c]==to){
					p[_last][c]=cnt; _last=link[_last];
				}
			}
		}
		_last=cur;
	}
	
	void Insert(string &s){
		for(int i=0;i<s.size();i++){
			InsertChar(s[i]);
		}
	}
	
	template<int _treeSize>
	void GetLinkTree(Tree<_treeSize> &t){
		for(int i=1;i<=cnt;i++){
			t.add(link[i]+1,i+1);
		}
	}
};

template<int _size>
class ACAM{
	int p[_size][30]; int fa[_size];int cnt; 
	int link[_size];
	int tag[_size]; int pos[_size]; 
	int last;
public:
	void InsertChar(char &c){
		if(p[last][c-'a']==0){
			cnt++; p[last][c-'a']=cnt;
			fa[cnt]=last;
		}
		last=p[last][c-'a'];
	} 
	void Insert(string &s,int id){
		last=0;
		for(int i=0;i<s.size();i++){
			InsertChar(s[i]);
		}
		tag[last]=id;pos[id]=last;
	}
	void Build(){
		queue <pair<int,int> > q;
		for(int i=0;i<26;i++){
			if(p[0][i]){
				q.push({p[0][i],i});
			}
		}
		while(!q.empty()){
			int x=q.front().first; int c=q.front().second; q.pop();
			int t=link[fa[x]];
			while(t!=0&&p[t][c]==0){
				t=link[t];
			}
			if(p[t][c]!=0&&p[t][c]!=x){
				link[x]=p[t][c];
			}
			for(int i=0;i<26;i++){
				if(p[x][i]){
					q.push({p[x][i],i});
				}
			}
		}
	}
	
	template<int _treeSize>
	void GetFailTree(Tree<_treeSize> &t){
		for(int i=1;i<=cnt;i++){
			t.add(link[i]+1,i+1);
		}
	}
};

void KMP(int &n,int *s,int *Border){
	for(int i=1;i<=n;i++){
		Border[i]=0;
		int tag=Border[i-1];
		while(tag!=0){
			if(s[tag+1]==s[i]){
				Border[i]=tag+1;
				break;
			}
			tag=Border[tag];
		}
		if(tag==0&&s[1]==s[i]&&i!=1)Border[i]=1;
	}
} 

void Manacher(int n,string &s,int *r1,int *r0){
	int mid=0;
	for(int i=1;i<=n;i++){
		r1[i]=1;
		if(mid+r1[mid]-1>i){
			r1[i]=min(r1[2*mid-i],mid+r1[mid]-i);
		}
		while(i-r1[i]>0&&s[i-r1[i]-1]==s[i+r1[i]-1]){
			r1[i]++;
		}
		if(mid+r1[mid]<i+r1[i]){
			mid=i;
		}
	}
	mid=0;
	for(int i=1;i<n;i++){
		r0[i]=0;
		if(mid+r0[mid]>i){
			r0[i]=min(r0[2*mid-i],mid+r0[mid]-i);
		}
		while(i-r0[i]>0&&s[i+r0[i]]==s[i-r0[i]-1]){
			r0[i]++;
		}
		if(mid+r0[mid]<i+r0[i]){
			mid=i;
		}
	}
}

//Geometry

template<typename _tp>
class Vector2{
public:
	_tp x,y;
	
	Vector2(){
		x=0; y=0;
	}
	
	Vector2(initializer_list<_tp> t){
		auto it=t.begin();
		x=*it; it++;
		y=*it;
	}
	
	int Quadrant(){
		if(x==0&&y==0) return 0;
		else if(x>0&&y>=0) return 1;
		else if(x<=0&&y>0) return 2;
		else if(x<0&&y<=0) return 3;
		else return 4;
	}
	
	_tp Len2(){
		return x*x+y*y;
	}
	
	template<typename _tp2>
	_tp operator *(Vector2<_tp2> t){
		return x*t.y-y*t.x;
	}
	
	template<typename _tp2>
	Vector2<_tp> operator -(Vector2<_tp2> t){
		return {x-t.x,y-t.y};
	}
	
	template<typename _tp2>
	Vector2<_tp> operator +(Vector2<_tp2> t){
		return {x+t.x,y+t.y};
	}
	
	template<typename _tp2>
	bool operator <(Vector2<_tp2> t){
		if(Quadrant()!=t.Quadrant()){
			return Quadrant()<t.Quadrant();
		}else if(y*t.x!=t.y*x){ 
			return y*t.x<t.y*x;
		}else{
			return Len2()<t.Len2();
		}
	}
};

template<typename _tp>
vector<Vector2<_tp> > Graham(vector<Vector2<_tp> > a){
	if(a.size()<3)return a;
	Vector2<_tp> bas=a[0];
	for(int i=0;i<a.size();i++){
		if(a[i].y<bas.y){
			bas=a[i];
		}
	}
	for(int i=0;i<a.size();i++){
		a[i]=a[i]-bas;
	}
	sort(a.begin(),a.end());
	for(int i=0;i<a.size();i++){
		
		a[i]=a[i]+bas;
	}
	Vector2<_tp> s[a.size()+10]; int r=0;
	for(int i=0;i<a.size();i++){
		if(i>0&&a[i].x==a[i-1].x&&a[i].y==a[i-1].y)continue;
		while(r>1){
			if((s[r]-s[r-1])*(a[i]-s[r])<0){
				r--;
			}else{
				break;
			}
		}
		r++; s[r]=a[i];
	}
	vector<Vector2<_tp> > res;
	for(int i=1;i<=r;i++){
		res.push_back(s[i]);
	}
	return res;
}

template<typename _tp>
vector<Vector2<_tp> > Minkowski(vector<vector<Vector2<_tp> > > &a){
	vector<Vector2<_tp>> p;
	Vector2<_tp> bas;
	for(int i=0;i<a.size();i++){
		auto cas=a[i][0];
		for(int j=1;j<a[i].size();j++){
			p.push_back(a[i][j]-a[i][j-1]);
			if(a[i][j].y<cas.y||(a[i][j].y<=cas.y&&a[i][j].x<cas.x)){
				cas=a[i][j];
			}
		}
		p.push_back(a[i][0]-a[i][a[i].size()-1]);
		bas=bas+cas;
	}
	sort(p.begin(),p.end());
	p[0]=p[0]+bas;
	for(int i=1;i<p.size();i++){
		p[i]=p[i]+p[i-1];
	}
	return p;
}

//Math

int FastPow(int x,int y){
	int res=1;
	while(y){
		if(y&1){
			res=res*x%mod;
		}
		x=x*x%mod;
		y=y>>1;
	}
	return res;
}

string CutString(string &s,int l,int r){
	string res="";
	for(int i=l;i<r;i++){
		res+=s[i];
	}
	return res;
}

string CutString(string &s,char c){
	string res="";
	for(int i=0;i<s.size();i++){
		if(s[i]==c)break;
		res+=s[i];
	}
	return res;
}