记录快速读写模板,一些类模板,基于 C++ 底层变量类型实现的栈、队列、开散列法哈希表、带模整型和指针双向链表。
后续可能继续更新。
namespace mw { // MWTL
const float eps=1e-6;
const double deps=1e-9;
const long double ldeps=1e-18;
typedef unsigned long long ull;
template<typename _Tp> inline void read(_Tp &x) {
int sign=1; char ch=getchar(); x=0;
while(!isdigit(ch)) ch=='-'&&(sign*=-1),ch=getchar();
while(isdigit(ch)) x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
x*=sign;
}
inline void read(float &x) {
int sign=1; float decimal_places=0.1; char ch=getchar(); x=0;
while(!isdigit(ch)) {
if(ch=='-') sign*=-1;
if(ch=='.') break;
ch=getchar();
}
if(ch!='.') while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();
if(ch=='.') {
ch=getchar();
for(int epsc=1;epsc<7&&isdigit(ch);++epsc) x=x+decimal_places*(ch-'0'),decimal_places*=0.1,ch=getchar();
}
x*=sign;
}
inline void read(double &x) {
int sign=1; double decimal_places=0.1; char ch=getchar(); x=0;
while(!isdigit(ch)) {
if(ch=='-') sign*=-1;
if(ch=='.') break;
ch=getchar();
}
if(ch!='.') while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();
if(ch=='.') {
ch=getchar();
for(int epsc=1;epsc<10&&isdigit(ch);++epsc) x=x+decimal_places*(ch-'0'),decimal_places*=0.1,ch=getchar();
}
x*=sign;
}
inline void read(long double &x) {
int sign=1; long double decimal_places=0.1; char ch=getchar(); x=0;
while(!isdigit(ch)) {
if(ch=='-') sign*=-1;
if(ch=='.') break;
ch=getchar();
}
if(ch!='.') while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();
if(ch=='.') {
ch=getchar();
for(int epsc=1;epsc<19&&isdigit(ch);++epsc) x=x+decimal_places*(ch-'0'),decimal_places*=0.1,ch=getchar();
}
x*=sign;
}
inline void read(char &ch) { ch=getchar(); while(!isgraph(ch)) ch=getchar(); }
inline void read(std::string &x) {
char ch=getchar(); x="";
while(!isgraph(ch)) ch=getchar();
while(isgraph(ch)) x+=ch,ch=getchar();
}
inline void read(char *x) {
char ch=getchar();
while(!isgraph(ch)) ch=getchar();
while(isgraph(ch)) *x=ch,ch=getchar(),++x;
*x=0;
}
template<class _Tp,class... Args> void read(_Tp &x,Args&... args) { read(x),read(args...); } // read everything.
template<typename _Tp> void write(_Tp x) {
x<0&&(putchar('-'),x*=-1);
x>9&&(write(x/10),0);
putchar(x%10+'0');
}
void write(float x) {
x<0&&(putchar('-'),x*=-1);
write((ull)x);
x-(ull)x>eps&&putchar('.');
for(int epsc=1;epsc<7&&x-(ull)x>eps;++epsc) write((ull)((x-(ull)x)*10)),x=(x-(ull)x)*10;
}
void write(double x) {
x<0&&(putchar('-'),x*=-1);
write((ull)x);
x-(ull)x>deps&&putchar('.');
for(int epsc=1;epsc<10&&x-(ull)x>deps;++epsc) write((ull)((x-(ull)x)*10)),x=(x-(ull)x)*10;
}
void write(long double x) {
x<0&&(putchar('-'),x*=-1);
write((ull)x);
x-(ull)x>ldeps&&putchar('.');
for(int epsc=1;epsc<19&&x-(ull)x>ldeps;++epsc) write((ull)((x-(ull)x)*10)),x=(x-(ull)x)*10;
}
void write(const char &x) { putchar(x); }
void write(const std::string &x) { for(auto ch:x) putchar(ch); }
void write(const char *x) { while(x[0]) putchar(x[0]),++x; }
template<class _Tp,class... Args> void write(_Tp x,Args... args) { write(x),write(args...); } // write everything.
template<class E> class stack {
private:
E s[N]; int pos;
public:
stack() { pos=0; }
inline void push(const E& x) { s[++pos]=x; }
inline void pop() { pos--; }
inline E& top() { return s[pos]; }
inline int size() { return pos; }
inline void clear() { pos=0; }
inline bool empty() { return !pos; }
};
template<class E> class queue {
private:
E q[N]; int head,tail;
public:
queue() { head=1,tail=0; }
inline void push(const E& x) { q[++tail]=x; }
inline void pop() { head++; }
inline E& front() { return q[head]; }
inline int size() { return tail-head+1; }
inline void clear() { head=1,tail=0; }
inline bool empty() { return head>tail; }
};
template<class E> class deque {
private:
E q[N]; int head,tail;
public:
deque() { head=1,tail=0; }
inline void push_front(const E& x) { q[--head]=x; }
inline void push_back(const E& x) { q[++tail]=x; }
inline void pop_front() { ++head; }
inline void pop_back() { --tail; }
inline E& front() { return q[head]; }
inline E& back() { return q[tail]; }
inline int size() { return tail-head+1; }
inline void clear() { head=1,tail=0; }
inline bool empty() { return head>tail; }
};
template<typename K,typename V> class hash_map { // hash map using open hashing.
private:
struct data { K key; V value; int next; } E[400000];
int cnt,head[200000];
const int mod=114511;
int get(K x) { return (x%mod+mod)%mod; }
public:
hash_map() { memset(head,0,sizeof head); cnt=0; }
inline void clear() { memset(head,0,sizeof head); cnt=0; }
inline V& operator[](K x) {
int u=get(x);
for(int p=head[u];p;p=E[p].next) if(E[p].key==x) return E[p].value;
return E[++cnt]=data{x,0,head[u]},head[u]=cnt,E[cnt].value;
}
};
template<int mod> class modint {
private:
int value;
public:
modint(int x=0) { while(x>mod) x-=mod; value=x; }
modint(const modint &x)=default;
modint operator+ (const modint &x) { modint ans=value+x.value; ans.value>=mod&&(ans.value-=mod); return ans; }
modint operator- (const modint &x) { modint ans=value-x.value; ans.value<0&&(ans.value+=mod); return ans; }
modint operator* (const modint &x) { modint ans=1ll*value*x.value%mod; return ans; }
modint operator& (const modint &x) { modint ans=value&x.value; return ans; }
modint operator& (const int &x) { modint ans=value&x; return ans; }
modint operator^ (const modint &x) { modint ans=value^x.value; return ans; }
modint operator^ (const int &x) { modint ans=value^x; return ans; }
modint operator>> (const modint &x) { modint ans=value>>x.value; return ans; }
modint operator<< (modint x) {
modint ans=1,base=2;
while(x) {
x&1&&(ans*=base);
base*=base,x>>=1;
}
return *this*ans;
}
modint& operator= (int x) { while(x>mod) x-=mod; value=x; return *this; }
modint& operator+= (const modint &x) { return *this=*this+x; }
modint& operator*= (const modint &x) { return *this=*this*x; }
modint& operator>>= (const modint &x) { return *this=*this>>x; }
modint& operator<<= (const modint &x) { return *this=*this<<x; }
modint operator++ () { ++value>=mod&&(value-=mod); return *this; }
operator int() const { return value; }
};
template<class E> class linked_list {
private:
struct node {
node *last,*next;
E val;
node(node *last,node *next,E x) { this->last=last,this->next=next,val=x; }
};
class iterator: std::iterator<std::bidirectional_iterator_tag,node> {
private:
node* _ptr;
public:
iterator(node *p=nullptr) { _ptr=p; }
iterator& operator= (const iterator &it) { _ptr=it._ptr; }
iterator& operator++() { _ptr=_ptr->next; return *this; }
iterator& operator--() { _ptr=_ptr->last; return *this; }
iterator& operator++(int) { auto ret=*this; return _ptr=_ptr->next,ret; }
iterator& operator--(int) { auto ret=*this; return _ptr=_ptr->last,ret; }
bool operator== (const iterator &it) { return _ptr==it._ptr; }
bool operator!= (const iterator &it) { return _ptr!=it._ptr; }
node* operator->() { return _ptr; }
node& operator*() { return *_ptr; }
} front_it,back_it;
public:
linked_list() { front_it=nullptr,back_it=nullptr; }
inline void push_back(E x) {
node *now=new node(back_it,nullptr,x);
back_it&&(back_it->next=now),!front_it&&(front_it=now);
back_it=now;
}
inline iterator remove(node *x) {
x->last&&(x->last->next=x->next),x->next&&(x->next->last=x->last);
x==front_it&&(front_it=x->next),x==back_it&&(back_it=x->last);
auto ret=x->next; delete x;
return ret;
}
inline iterator begin() { return front_it; }
inline iterator end() { return back_it==nullptr?nullptr:back_it->next; }
inline E& front() { return front_it->val; }
inline E& back() { return back_it->val; }
};
}