namespace hdk

没有高精类,因为这玩意太占内存了,正在优化

demap
Rander
StringAddition_InFix
string
ordered_vector

#include<bits/stdc++.h>
using namespace std;
namespace hdk{
	const size_t size=100000;
	template<typename T>
	class vector{
		private:T a[size];T* begind=a;T* endd=a;
		public:
			inline void push(T x){if(endd>a+size) exit(274);*endd=x;endd++;}
			inline bool empty(){if(endd==begind) return true;return false;}
			inline void pop(){if(endd==begind) exit(275);endd--;}
			inline T back(){if(endd==begind) exit(275);return *(endd-1);}
			inline T* begin(){return begind;}
			inline T* end(){return endd+1;}
			inline T get(int* x){if(x>endd) exit(276);return *x;}
			inline int remain_size(){return size-(endd-begind);}
			T* operator [](int x){if(begind+x>endd) endd=begind+x;return begind+x;}
			inline void clear(){memset(a,0,sizeof a);endd=begind;}
	};
	template<typename T>
	class stack{
		private:T a[size];T* begind=a;T* endd=a;
		public:
			inline void push(T x){if(endd>a+size) exit(274);*endd=x;endd++;}
			inline bool empty(){if(endd==begind) return true;return false;}
			inline void pop(){if(endd==begind) exit(275);endd--;}
			inline T back(){if(endd==begind) exit(275);return *(endd-1);}
			inline T* begin(){return begind;}
			inline T* end(){return endd+1;}
			inline T get(int* x){if(x>endd) exit(276);return *x;}
			inline int remain_size(){return size-(endd-begind);}
			T* operator [](int x){return begind+x;}
			inline void clear(){endd=begind;}
			inline void push(hdk::vector<T> x){for(T* i=x.begin();i!=x.end();++i){push(*i);}}
	};
	class rander{
		private:
			std::vector<long long> list;
			int it,ssize;
		public:
			inline int reset(int size){if(size<=0)return -1;int seed=std::rand();mt19937 Rand(seed);ssize=size;it=0;list.clear();for(int i=1;i<=size;++i){list.push_back(Rand());}return seed;}
			inline int reset(){struct _timeb T;_ftime(&T);srand(T.millitm);return rander::reset(10000);}
			inline std::vector<long long> randlist(int size){int seed=ssize;reset(size);std::vector<long long> ret=list;reset(seed);return ret;}
			inline int rand(){if(ssize<=0)reset();int ret=list[it];it++;if(it>=ssize)reset(ssize);return ret;}
			inline int rand(int mod){int ret=rander::rand()%mod;return ret;}
			inline long long randabs(int mod){long long ret=abs(rander::rand());if(mod)ret%=mod;return ret;}
			inline long long rand(int l,int r){if(l<0)return rander::rand(0,r);long ret=rander::randabs(r-l+1)+l;return ret;}
			template<typename _T>inline _T from(vector<_T>p){long long ret=rander::randabs(p.size());return p[ret];}
			template<typename _T>inline void randsort(vector<_T>&p){for(int i=1;i<=p.size();++i){int x=rander::rand(0,p.size()-1),y=rander::rand(0,p.size()-1);swap(p[x],p[y]);}}
			template<typename _T>inline void randsort(_T &p,int from,int to){for(int i=1;i<=to-from+1;++i){int x=rander::rand(from,to),y=rander::rand(from,to);swap(p[x],p[y]);}}
	};
	template<typename F,typename S>
	class demap{
		private:
			std::map<F,S> f;
			std::map<S,F> s;
		public:
			inline void add(F Key,S Text){f[Key]=Text;s[Text]=Key;}
			S find_text(F Key){return f[Key];}
			F find_key(S Text){return s[Text];}
			inline bool count_key(F Key){return f.count(Key);}
			inline bool count_text(S Text){return s.count(Text);}
			inline void clear(){s.clear();f.clear();}
			inline bool empty(){return s.empty();}
	};
	class BIT{
	private:
		long long d[100001],di[100001],s[100001];
		inline int lowbit(int x){return x&-x;}
		void change(long long *c,int x,int y){while(x<=n){c[x]+=y;x+=lowbit(x);}}
		long long sum(long long *c,int x){long long ans=0;while(x>0){ans+=c[x];x-=lowbit(x);}return ans;}
	public:
		int n;
		void clear(){memset(d,0,sizeof(d));memset(di,0,sizeof(di));memset(s,0,sizeof(s));}
		long long sum(int l,int r){return sum(r)-sum(l-1);}
		void change(int x,int y,int changevalue){change(d,x,changevalue);change(d,y+1,-changevalue);change(di,x,changevalue*x);change(di,y+1,-changevalue*(y+1));}
		void make_sum(int id,int x){s[id]=s[id-1]+x;}
		long long sum(int id){return sum(id,id);}
		void change(int id,int changevalue){change(id,id,changevalue);}
	};
	template<typename T>
	class matrix{
		private:int n,m,mod;bool ifmod;T mat[size+1][size+1];
		public:
		T get(int x,int y){return mat[x][y];}
		void clear(){for(int i=1;i<=n;++i){for(int j=1;j<=m;++j){mat[i][j]=0;}}n=0;m=0;ifmod=false;mod=0;}
		void setmod(int x){if(x==0){ifmod=false;}else{ifmod=true;}mod=x;}
		void resize(int nsize,int msize){n=nsize;m=msize;}
		void fillmain(int x){for(int i=1;i<=n;++i){mat[i][i]=x;}}
		void fillsec(int x){for(int i=1;i<=n;++i){mat[i][n-i+1]=x;}}
		void fill(int x){for(int i=1;i<=n;++i){for(int j=1;j<=m;++j){mat[i][j]=x;}}}
		void fill(int x,int startn,int endn,int startm,int endm){for(int i=startn;i<=endn;++i){for(int j=startm;j<=endm;++j){mat[i][j]=x;}}}
		void opposite(){for(int i=1;i<=n;++i){for(int j=1;j<=m;++j){mat[i][j]*=-1;}}}
		void packed_clear(int nsize,int msize,int filln,int mod){clear();resize(nsize,msize);setmod(mod);fill(filln);}
		void input(){cin>>n>>m;for(int i=1;i<=n;++i){for(int j=1;j<=m;++j){cin>>mat[i][j];}}}
		void inputn(int nsize){n=nsize;cin>>m;for(int i=1;i<=n;++i){for(int j=1;j<=m;++j){cin>>mat[i][j];}}}
		void inputm(int msize){m=msize;cin>>n;for(int i=1;i<=n;++i){for(int j=1;j<=m;++j){cin>>mat[i][j];}}}
		void input(int nsize,int msize){n=nsize;m=msize;for(int i=1;i<=n;++i){for(int j=1;j<=m;++j){cin>>mat[i][j];}}}
		void print(){for(int i=1;i<=n;++i){for(int j=1;j<=m;++j){cout<<mat[i][j]<<" ";}cout<<endl;}}
		matrix<T> operator *(const matrix &A)const{matrix<T> p;p.packed_clear(n,A.m,0,mod);for(int i=1;i<=n;++i){for(int j=1;j<=A.m;++j){for(int k=1;k<=m;++k){if(ifmod){p.mat[i][j]+=(mat[i][k]*A.mat[k][j])%mod;p.mat[i][j]%=mod;}else{p.mat[i][j]+=mat[i][k]*A.mat[k][j];}}}}return p;}
		matrix<T> operator +(const matrix &A)const{matrix<T> p;p.packed_clear(n,m,0,mod);for(int i=1;i<=n;++i){for(int j=1;j<=m;++j){if(ifmod){p.mat[i][j]=(mat[i][j]+A.mat[i][j])%mod;}else{p.mat[i][j]=mat[i][j]+A.mat[i][j];}}}return p;}
		matrix<T> operator -(const matrix &A)const{matrix<T> p;p.packed_clear(n,m,0,mod);for(int i=1;i<=n;++i){for(int j=1;j<=m;++j){if(ifmod){p.mat[i][j]=(mat[i][j]-A.mat[i][j])%mod;}else{p.mat[i][j]=mat[i][j]-A.mat[i][j];}}}return p;}
		matrix<T> operator ^(const long long times)const{matrix<T> p;p.packed_clear(n,m,1,mod);for(int i=1;i<=times;++i){p=p*(*this);}return p;}
		matrix<T> operator |(long long times)const{matrix<T> base,p;p.packed_clear(n,m,0,mod);base.packed_clear(n,m,0,mod);base=(*this);p.fillmain(1);if(times<=0){return p;}while(times){if(times&1){p=p*base;}base=base*base;times>>=1;}return p;}
		matrix<T> operator *(const int x)const{matrix<T> p;p.packed_clear(n,m,0,mod);for(int i=1;i<=n;++i){for(int j=1;j<=m;++j){if(ifmod){p.mat[i][j]=(mat[i][j]*x)%mod;}else{p.mat[i][j]=mat[i][j]*x;}}}return p;}
	};
	const long double eps=1e-7;
	namespace SIEN_INT{
		struct Eint{
			int pow;
			long double frac;
			void clear(){pow=0;frac=0;}
			void reco(){if(frac>0){while(frac<1){if(frac-1.0<eps) break;frac*=10;pow--;}while(frac>=10){if(frac-10.0<eps) break;frac/=10;pow++;}}if(frac<0){while(frac>-1){if(fabs(frac+1.0<eps)) break;frac*=10.0;pow--;}while(frac<=-10){if(fabs(frac+10.0<eps)) break;frac/=10.0;pow++;}}}
			void print(){printf("%Lfe%d",frac,pow);}
			void input(){cin>>frac>>pow;reco();}
			void redpow(int to){while(pow!=to){pow--;frac*=10;}}
			void operator =(const pair<long double,int> x){frac=x.first;pow=x.second;reco();}
			void operator =(const int x){frac=x;pow=0;reco();}
			bool operator >(const Eint &A)const{if(pow==A.pow) return frac>A.frac;return pow>A.pow;}
			bool operator ==(const Eint &A)const{if(pow==A.pow&&fabs(frac-A.frac)<eps) return true;return false;}
			bool operator <(const Eint &A)const{return !((*this>A)||(*this==A));}
			bool operator >=(const Eint &A)const{return !(*this<A);}
			bool operator <=(const Eint &A)const{return !(*this>A);}
			Eint operator +(Eint &A){Eint ans;if(pow>A.pow) redpow(A.pow);else A.redpow(pow);ans.frac=frac+A.frac;ans.pow=pow;ans.reco();reco();A.reco();return ans;}
			void operator +=(Eint &A){*this=*this+A;}
			Eint operator -(Eint &A){Eint ans;if(pow>A.pow) redpow(A.pow);else A.redpow(pow);ans.frac=frac-A.frac;ans.pow=pow;ans.reco();reco();A.reco();return ans;}
			void operator -=(Eint &A){*this=*this-A;}
			Eint operator *(const Eint &A)const{Eint ans;ans.frac=frac*A.frac;ans.pow=pow+A.pow;ans.reco();return ans;}
			void operator *=(const Eint &A){*this=*this*A;}
			Eint operator /(const Eint &A)const{Eint ans;ans.frac=frac/A.frac;ans.pow=pow-A.pow;ans.reco();return ans;}
			void operator /=(const Eint &A){*this=(*this/A);}
			Eint operator ^(const int x){Eint ans;ans.frac=std::pow(frac,x);ans.pow=pow*x;ans.reco();return ans;}
			void operator ^=(const int x){pow*=x;}
		};
		Eint e(int x){Eint ans;ans=x;return ans;}
		pair<long double,int> E(long double _f,int _p){return make_pair(_f,_p);}
		pair<long double,int> E(int _p,long double _f){return make_pair(_f,_p);}
	}
	namespace Iter{
		void cout(std::vector<int> &_v,int _from,int _to,char _devide){std::vector<int>::iterator iter;std::vector<int>::reverse_iterator riter;
		if(_from<_to){for(iter=_v.begin()+_from;iter!=_v.begin()+_to;++iter){std::cout<<*iter<<_devide;}}
		else{for(riter=_v.rbegin()+_to;riter!=_v.rbegin()+_from;++riter){std::cout<<*riter<<_devide;}}}
	}
	template<typename T>
	void memset(T a[],int _val,int _size){if(_val==0){for(T* i=a;i<=a+_size-1;++i) *i&=0;return;}for(T* i=a;i<=a+_size-1;++i)*i=_val;}
	namespace fastio{
		void rule(bool setting){std::ios::sync_with_stdio(setting);}
		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^48);ch=getchar();}return x*f;}
	    inline int read(int &A){A=read();return A;}
		inline void write(int A){if(A<0){putchar('-');A=-A;}if(A>9){write(A/10);}putchar(A%10+'0');}
		inline void write(char A){putchar(A);}
		inline void space(){putchar(' ');}
		inline void endl(){putchar('\n');}
		#define read read()
		#define w(a) write(a)
		#define e endl()
		#define s space()
		#define we(a) write(a);endl()
		#define ws(a) write(a);space()
	}
	class StringAddition_InFix{
		private:
			string x="00";
			inline void fixed(int size){if(size<=x.length())return;string res;
				for(int i=1;i<=size-x.length();++i){res.push_back('0');}
				for(int i=0;i<=x.length()-1;++i){res.push_back(x[i]);}x=res;
			}
			inline void refixed(){
				string res;bool is0=false;res.push_back('0');
				for(int i=0;i<=x.length()-1;++i){if(x[i]!='0'||is0){is0=true;res.push_back(x[i]);}}x=res;
			}
		public:
			void operator =(string inx){x.push_back('0');for(int i=0;i<=inx.length()-1;++i){x.push_back(inx[i]);}}
			void operator =(long long inx){
				x.clear();string ans;int r=inx;while(r){ans.push_back(r%10+'0');r/=10;}x.push_back('0');
				for(int i=ans.size()-1;i>=0;--i){x.push_back(ans[i]);}}
			string it(){string res;for(int i=1;i<=x.length()-1;++i){res.push_back(x[i]);}if(res.empty()) res="0";return res;}
			StringAddition_InFix operator +(StringAddition_InFix a){
				StringAddition_InFix ans;ans=a;
				if(x.length()>ans.x.length()){ans.fixed(x.length());}else fixed(ans.x.length());
				for(int i=max(x.length(),ans.x.length())-1;i>=1;--i){int np=(ans.x[i]-'0'+x[i]-'0');if(np>=10) ans.x[i-1]+=np/10;ans.x[i]=np%10+'0';}
				ans.refixed();refixed();return ans;}
			StringAddition_InFix operator +(long long a){StringAddition_InFix r;r=a;return *this+r;}
			void operator +=(StringAddition_InFix a){*this=*this+a;}
			void operator +=(long long a){*this=*this+a;}
			friend ostream& operator<<(ostream& output,StringAddition_InFix& inx){output<<inx.it()<<endl;return output;}
			friend istream& operator>>(istream& input,StringAddition_InFix& inx){input>>inx.x;inx.fixed(inx.x.length()+1);return input;}
	};
	typedef StringAddition_InFix Int;
	Int max(Int &la,Int &lb){if(la.it()>lb.it()) return la;else return lb;}
	Int min(Int &la,Int &lb){if(la.it()<lb.it()) return lb;else return la;}
	namespace splay{
		const int N=1000001;int w[N];const int inf=114514191;
		struct splaytree{
			int root,tot;std::vector<int> search_answer;
			struct tree{int w;int cnt,size;int fa,son[2];int tag;}t[N];
			inline void clear(int x){t[x]={0,0,0,0,{0,0}};}
			inline bool judgeson(int x){return t[t[x].fa].son[1]==x;}
			inline void update(int x){if(x){t[x].size=t[x].cnt;
				if(t[x].son[0]) t[x].size+=t[t[x].son[0]].size;
				if(t[x].son[1]) t[x].size+=t[t[x].son[1]].size;}}
			inline int build(int l,int r,int last){
				if(l>r) return 0;int mid=(l+r)/2;int now=++tot;
				t[now]={w[mid],1,1,last,{0,0},0};
				t[now].son[0]=build(l,mid-1,now);
				t[now].son[1]=build(mid+1,r,now);update(now);return now;}
			inline void pushdown(int x){if(x and t[x].tag){
				t[t[x].son[1]].tag^=1;t[t[x].son[0]].tag^=1;
				swap(t[x].son[1],t[x].son[0]);t[x].tag=0;}
			}
			inline void rotate(int x){
				int f=t[x].fa,gf=t[f].fa;pushdown(x),pushdown(f);
				int k=judgeson(x);t[f].son[k]=t[x].son[k^1];t[t[f].son[k]].fa=f;
				t[x].son[k^1]=f;t[f].fa=x;t[x].fa=gf;
				if(gf){t[gf].son[t[gf].son[1]==f]=x;}update(f);}
			inline void splay(int x){for(int f;(f=t[x].fa);rotate(x)){if(t[f].fa){
				if(judgeson(x)==judgeson(f)){rotate(f);}else rotate(x);}}root=x;}
			inline void splay(int x,int goal){
				for(int f;(f=t[x].fa)!=goal;rotate(x)){if(t[f].fa!=goal){
					if(judgeson(x)==judgeson(f)){rotate(f);}else{rotate(x);}}}
					if(goal==0){root=x;}}
			inline void insert(int x){
				if(!root){tot++;t[tot]={x,1,1,0,{0,0}};root=tot;return;}
				int now=root,fa=0;
				while(1){if(x==t[now].w){t[now].cnt++;update(now);update(fa);splay(now);break;}
					fa=now;now=t[now].son[t[now].w<x];if(!now){
						tot++;t[tot]={x,1,1,fa,{0,0}};t[fa].son[t[fa].w<x]=tot;
						update(fa);splay(tot);break;}}}
			inline int find(int x){
				int now=root;while(now){pushdown(now);
					if(x<=t[t[now].son[0]].size){now=t[now].son[0];}
					else{x-=t[t[now].son[0]].size+1;if(!x) return now;now=t[now].son[1];}}return 0;}
			inline void reverse(int L,int R){
				int l=L-1,r=R+1;l=find(l),r=find(r);splay(l,0);splay(r,l);int p=t[root].son[1];p=t[p].son[0];t[p].tag^=1;}
			inline int findnum(int rank){
				int now=root;while(now){if(t[now].son[0] and rank<=t[t[now].son[0]].size){
					now=t[now].son[0];}else{
					int temp=t[now].cnt;if(t[now].son[0]){temp+=t[t[now].son[0]].size;}
						if(rank<=temp) return t[now].w;rank-=temp;now=t[now].son[1];}}
				return t[now].w;}
			inline int findrank(int val){
				int now=root,ans=0;while(now){if(val<t[now].w){now=t[now].son[0];}
					else{if(t[now].son[0]){ans+=t[t[now].son[0]].size;}
						if(val==t[now].w){splay(now);return ans;}
						ans+=t[now].cnt;now=t[now].son[1];}}return ans;}
			inline int findpre(){int now=t[root].son[0];while(t[now].son[1]) now=t[now].son[1];return now;}
			inline int findnext(){int now=t[root].son[1];while(t[now].son[0]) now=t[now].son[0];return now;}
			inline int findpre(int val){insert(val);int ans=findpre();free(val);return ans;}
			inline int findnext(int val){insert(val);int ans=findnext();free(val); return ans;}
			inline int got(int id){return t[id].w;}
			inline void free(int x){
				findrank(x);if(t[root].cnt>1){t[root].cnt--;update(root);return;}
				if(!t[root].son[0] and !t[root].son[1]){clear(root);root=0;return;}
				if(!t[root].son[0]){int oroot=root;root=t[root].son[1];t[root].fa=0;clear(oroot);return;}
				else if(!t[root].son[1]){int oroot=root;root=t[root].son[0];t[root].fa=0;clear(oroot);return;}
				int left=findpre(),oroot=root;splay(left);
				t[root].son[1]=t[oroot].son[1];t[t[oroot].son[1]].fa=root;clear(oroot);update(root);}
			inline void search(int now){
				pushdown(now);if(t[now].son[0]) search(t[now].son[0]);
				if(t[now].w!=inf and t[now].w!=-inf){search_answer.push_back(t[now].w);}if(t[now].son[1]) search(t[now].son[1]);}
			inline void printanswer(char devide,char ending){
				for(int i:search_answer){cout<<i<<devide;}cout<<ending;}
		};
	}
}
posted @ 2024-06-08 19:31  HaneDaniko  阅读(32)  评论(0编辑  收藏  举报