版本比较
板子
正文
增加 508 行删除 15 行
1123345656788910111213141516171819202122231024251226271415282930313233183435363738392021404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120232412112212312412512612712812913013113213313413513613713813914014114214314414514614726148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521这些是我足够熟练的板子板子按照主观实用性从大到小排序。
## 树状数组## 快读快写```cppint n,t[500005];int lowbit(int x)//#define getchar_unlocked getchar//#define putchar_unlocked putcharinline void read(int& x){ return (x&-x); x=0; char ch=getchar_unlocked(); bool flag=false; while(ch<'0' || ch>'9') { if(ch=='-') flag=1; ch=getchar_unlocked(); } while(ch>='0' && ch<='9') { x=x*10+ch-'0'; ch=getchar_unlocked(); } if(flag) x=-x;}void add(int a,int k)inline void write(int x){ while(a<=n) if(x<0) { t[a]+=k; a+=lowbit(a); putchar_unlocked('-'); x=-x; } if(x>9) write(x/10); putchar_unlocked(x%10+'0');}int query(int a)```
## 线段树```cppclass SGT{ int ans=0; while(a>=1) private: void up(int p) { } void addtag(int p,int x) { } void pd(int p) { if(!ls[p]) ls[p]=++cnt; if(!rs[p]) rs[p]=++cnt; addtag(ls[p],tag[p]); addtag(rs[p],tag[p]); tag[p]=0; } public: int cnt=0; vector<int> t; vector<int> ls; vector<int> rs; vector<int> tag; SGT(int sz,int x=4) { t.resize(sz*x+4); ls.resize(sz*x+4); rs.resize(sz*x+4); tag.resize(sz*x+4); if(x==4) build(1,1,sz); } SGT(){} void build(int p,int l,int r) { if(l==r) { return; } ls[p]=p*2; rs[p]=p*2+1; int mid=l+r>>1; build(ls[p],l,mid); build(rs[p],mid+1,r); } void update(int &p,int l,int r,int x,int y,int k) { if(!p) p=++cnt; if(x<=l && r<=y) { addtag(p,k); return; } pd(p); int mid=l+r>>1; if(x<=mid) update(ls[p],l,mid,x,y,k); if(mid<y) update(rs[p],mid+1,r,x,y,k); up(p); } int query(int p,int l,int r,int x,int y) { if(!p) return 0; if(x<=l && r<=y) { return t[p]; } pd(p); int mid=l+r>>1,ans=0; if(x<=mid) ans+=query(ls[p],l,mid,x,y); if(mid<y) ans+=query(rs[p],mid+1,r,x,y); return ans; } };```## 一维树状数组```cppclass BIT{private: int lowbit(int x) { ans+=t[a]; a-=lowbit(a); return (x&-x); }public: int n; vector<int> t; BIT(int sz) { t.resize(sz+4); n=sz; } BIT(){} void add(int id,int k) //加入一个数字 { while(id<=n) { t[id]+=k; id+=lowbit(id); } } int query(int id) //查询前缀和 { int ans=0; while(id>=1) { ans+=t[id]; id-=lowbit(id); } return ans; return ans; }};```
## 二维树状数组```cppclass BIT2D{ private: int n,m; vector<vector<int>> tree; int lowbit(int x) { return x&-x; } public: BIT2D(){} BIT2D(int _n,int _m) { tree.resize(_n+4,vector<int>(_m+4)); n=_n,m=_m; } void add(int x,int y,int k) { for(int i=x;i<=n;i+=lowbit(i)) { for(int j=y;j<=m;j+=lowbit(j)) { tree[i][j]+=k; } } } int query(int x,int y) { int ans=0; for(int i=x;i>=1;i-=lowbit(i)) { for(int j=y;j>=1;j-=lowbit(j)) { ans+=tree[i][j]; } } return ans; }};```## 高精度```cppstruct BigInt{ vector<short> num; BigInt(){} BigInt(int sz){num.resize(sz+5);} void add0(int x) { while(num.size()<x) num.push_back(0); } void del0() { while(num.size()>1 &&num.back()==0) num.pop_back(); } BigInt(string x) { for(int i=x.size()-1;i>=0;i--) { num.push_back(x[i]-'0'); } del0(); }
bool operator==(const BigInt& other) const { if(other.num.size()!=num.size()) return false; for(int i=0;i<this->num.size();i++) { if(num[i]!=other.num[i]) return false; } return true; } bool operator<(const BigInt& other) const { if(num.size()<other.num.size()) return true; if(num.size()>other.num.size()) return false; for(int i=num.size()-1;i>=0;i--) { if(num[i]<other.num[i]) return true; if(num[i]>other.num[i]) return false; } return false; } bool operator<=(const BigInt& other) const { return *this<other || *this==other; } bool operator>(const BigInt& other) const { return !(*this<=other); } bool operator>=(const BigInt& other) const { return !(*this<other); } BigInt operator+(BigInt other) { int len=max(num.size(),other.num.size())+1; BigInt ans(len);other.add0(len+1);add0(len+1); for(int i=0;i<=len;i++) { int delta=num[i]+other.num[i]+ans.num[i]; ans.num[i+1]=delta/10; ans.num[i]=delta%10; //cout<<"this delta="<<delta<<" "<<num[i]<<"\n"; } ans.del0();del0();other.del0(); return ans; } BigInt operator-(BigInt other) { int len=max(num.size(),other.num.size())+1; BigInt ans(len);other.add0(len+2);add0(len+1); for(int i=0;i<=len;i++) { int delta=num[i]-other.num[i]+ans.num[i]; if(delta<0) ans.num[i+1]--,delta+=10; ans.num[i]=delta%10; //cout<<"this delta="<<delta<<" "<<num[i]<<"\n"; } ans.del0();del0();other.del0(); return ans; } BigInt operator*(BigInt other) { int len=num.size()+other.num.size()+1; BigInt ans(len); for(int i=0;i<num.size();i++) { for(int j=0;j<other.num.size();j++) { ans.num[i+j]+=num[i]*other.num[j]; } } for(int i=0;i<=len;i++) { ans.num[i+1]+=ans.num[i]/10; ans.num[i]%=10; } ans.del0(); return ans; }};
```## FHQ treap```cppclass fhqtreap{ public: int head=0,cnt=0; vector<int> pri; vector<int> key; vector<int> ls; vector<int> rs; vector<int> sz; mt19937 rd; fhqtreap(int MAXN) { pri.resize(MAXN+5); key.resize(MAXN+5); ls.resize(MAXN+5); rs.resize(MAXN+5); sz.resize(MAXN+5); rd.seed(time(0)); } void up(int i) { sz[i]=sz[ls[i]]+sz[rs[i]]+1; } int merge(int l,int r) { if(!l || !r) return (l^r); if(pri[l]>pri[r]) { rs[l]=merge(rs[l],r); up(l); return l; } else { ls[r]=merge(l,ls[r]); up(r); return r; } } void split(int i,int l,int r,int k) { if(!i) { rs[l]=ls[r]=0; return; } if(key[i]<=k) //这里可能按排名排序 { rs[l]=i; split(rs[i],i,r,k); } else { ls[r]=i; split(ls[i],l,i,k); } up(i); }}; ```## 并查集```cppclass DSU{public: vector<int> f; DSU(int sz) { f.resize(sz+4); for(int i=0;i<=sz;i++) { f[i]=i; } } DSU(){} int get_f(int x) { if(f[x]==x) { return x; } return f[x]=get_f(f[x]); } void merge(int a,int b) { f[get_f(a)]=get_f(b); } bool check(int a,int b) { return get_f(a)==get_f(b); } };```## 分治```cppint a[500005];int c[500005];void msort(int l,int r){ if(l==r) return; int mid=(l+r)>>1; int i=l,j=mid+1,k=l; msort(l,mid);msort(mid+1,r); for(;i<=mid && j<=r;k++) { if(a[i]<=a[j]) c[k]=a[i++]; else c[k]=a[j++]; } for(;i<=mid;k++,i++) c[k]=a[i]; for(;j<=r;k++,j++) c[k]=a[j]; for(i=l;i<=r;i++) a[i]=c[i];}```## 快速幂```cppint fpow(int a,int b,int p) //计算a^b mod p{ int res=1; while(b) { if(b%2==1) res=res*a%p; b/=2; a*=a; a%=p; } return res%p;}```
## 不带修莫队```cppint arr[200005];int ans[200005];int bi[200005];int most=0,lena;struct query{ int l,r,id; query(int _l,int _r,int _id) { l=_l,r=_r,id=_id; } query(void){} friend bool operator<(query a,query b) { if(bi[a.l]==bi[b.l]) return a.r<b.r; return bi[a.l]<bi[b.l]; }}q[200005];void add(int x){
}void del(int x){
}signed main(){ int n,m; cin>>n>>m; lena=sqrt(n); for(int i=1;i<=n;i++) { bi[i]=(i-1)/lena+1; } for(int i=1;i<=n;i++) { cin>>arr[i]; } for(int i=1;i<=m;i++) { cin>>q[i].l>>q[i].r; q[i].id=i; } sort(q+1,q+1+m); int l=1,r=0; for(int i=1;i<=m;i++) { while(l>q[i].l) { add(--l); } while(l<q[i].l) { del(l++); } while(r<q[i].r) { add(++r); } while(r>q[i].r) { del(r--); } ans[q[i].id]=most; } for(int i=1;i<=m;i++) { cout<<ans[i]<<"\n"; } return 0;}```## gcd/lcm```cppint gcd(int a,int b){ if(b==0) return a; return gcd(b,a%b);} int lcm(int a,int b){ return a/gcd(a,b)*b;}```