我的缺省源

这篇博客记录了我的思考,汗水,以及一些写的十分丑陋的模板。既是为了方便我自己回顾与理解,也可能方便他人。

1.快读

Code
class Reader{
private :
	char _buf[1000002];
	char *start_buf=_buf,*end_buf=_buf;
	bool neg=0;
	char ch;
public :
	inline Reader& operator>>(char &val) noexcept {
		val = (start_buf==end_buf && 
			(end_buf = (start_buf=_buf)+fread(_buf,1,1000000,stdin)
			,start_buf == end_buf)? EOF : *start_buf++ );
		return *this;
	}
	inline Reader& operator>>(bool &val) noexcept {
		*this>>ch;
		val= ch!=0x30;
		return *this;
	}
	inline Reader& operator>>(signed &val) noexcept {
		neg=0;for(*this>>ch;!isdigit(ch);*this>>ch)
			neg^=!(ch^'-');
		for(val=0;isdigit(ch);*this>>ch)
			val = (val<<3) + (val<<1) + (ch&0xf);
		val=(neg?~val+1:val);
		return *this;
	}
	inline Reader& operator>>(long long signed &val) noexcept {
		neg=0;for(*this>>ch;!isdigit(ch);*this>>ch)
			neg^=!(ch^'-');
		for(val=0;isdigit(ch);*this>>ch)
			val = (val<<3) + (val<<1) + (ch&0xf);
		val=(neg?~val+1:val);
		return *this;
	}
	inline Reader& operator>>(unsigned &val) noexcept {
		for(*this>>ch;!isdigit(ch);*this>>ch);
		for(val=0;isdigit(ch);*this>>ch)
			val = (val<<3) + (val<<1) + (ch&0xf);
		return *this;
	}
	inline Reader& operator>>(long long unsigned &val) noexcept {
		for(*this>>ch;!isdigit(ch);*this>>ch);
		for(val=0;isdigit(ch);*this>>ch)
			val = (val<<3) + (val<<1) + (ch&0xf);
		return *this;
	}
	inline Reader& operator>>(char* val) noexcept {
		char *_val(val);
		for(*this>>ch;ch==32||(6<ch&&ch<14);*this>>ch) printf("12");
		for(;~ch&&ch!=32&&(ch<7||ch>13);*this>>ch)
			*_val++ =ch;
		*_val=0;
		return *this;
	}
	inline Reader& operator>>(std::string &val) noexcept {
		val.clear();
		for(*this>>ch;(ch==32||(6<ch&&ch<14))&&ch!=EOF;*this>>ch);
		for(;~ch&&ch!=32&&(ch<7||ch>13);*this>>ch)
			val.push_back(ch);
		return *this;
	}
} read;

2.并查集

Code
template<const int Len>
class DSU {
private:
	struct DSU_node{
		int parents,size;
	} node[Len];
public:
	DSU() noexcept {
		for(int i=0;i<Len;i++)
			node[i].parents=i,
			node[i].size=1;
	}
	int Find(const int &t) {
		if(node[t].parents!=t)
			return node[t].parents = Find(node[t].parents);
		else return t;
	}
	inline void Union(int lt,int rt) {
		lt=Find(lt),rt=Find(rt);
		if(lt!=rt) {
			if(node[lt].size<node[rt].size)
				node[lt].parents=rt;
			else node[rt].parents=lt;
		}
	}
   inline bool Same(int lt,int rt) {
      lt=Find(lt),rt=Find(rt);
      return lt==rt;
   }
};

树状数组

Code
template<typename Value,const int LEN>
class BIT {
/*
 * Please operator + of T first
*/
public:
	void build(const std::vector<Value>&val) {
		int j=1;
		for(int i=0;i<val.size();++i,++j) {
			c[j]=c[j]+val[i];
			c[j+lowbit(j)]=c[j+lowbit(j)]+c[j];
		}
	}
	void build(const Value *start,const Value *end) {
		int j=1;
		for(auto it=start;it!=end;++it,++j) {
			c[j]=c[j]+*it;
			c[j+lowbit(j)]=c[j+lowbit(j)]+c[j];
		}
	}
	BIT() = default;
	BIT(const std::vector<Value>&val) { build(val); }
	BIT(const Value *start,const Value *end) { build(start,end); }
	void resize(const int &_ML) { MaxLen=_ML; }
	void reset() { ++NowTag; }
	void update(int x,const Value &k = 1) {
		for(;x<=MaxLen;x+=lowbit(x)) {
			if(tag[x]!=NowTag) c[x]=Value();
			c[x]=c[x]+k,tag[x]=NowTag;
		}
	}
	Value query(int x){
		Value res=Value();
		for(;x>0;x-=lowbit(x)) {
			if(tag[x]==NowTag)
				res=res+c[x];
      }
		return res;
	}
private:
	inline int lowbit(const int &x) { return x&-x; }
	int NowTag,tag[LEN+1];
	int MaxLen = LEN;
	Value c[LEN+1];
};
posted @ 2023-04-28 15:30  ASnown  阅读(23)  评论(0)    收藏  举报