版本比较
板子
正文
增加 380 行删除 9 行
1123456567891021 行未改动32333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161179 行未改动1271281291305313113213313413525 行未改动161162163164165166167168169170921711721731741751761771781791801021811821831841855 行未改动1911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673686 行未改动375376377378379132380381382383384385386387140388389390391144392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521原谅我的码风 /jk板子按照主观实用性从大到小排序。
## 快读快写```cpp//Linux 评测机是xxx_unlocked//Windows 要去掉_unlocked//#define getchar_unlocked getchar//#define putchar_unlocked putcharinline void read(int& x){ x=0; char ch=getchar_unlocked();21 行未改动 putchar_unlocked(x%10+'0');}```
## 线段树```cppclass SGT{ 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{9 行未改动 { t.resize(sz+4); n=sz; }
BIT(){} void add(int id,int k) //加入一个数字 { while(id<=n) {25 行未改动 { 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) //增加数 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 query(int x,int y) { int ans=0; for(int i=x;i>=1;i-=lowbit(i)) {5 行未改动 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{6 行未改动 { f[i]=i; } } DSU(){} int get_f(int x) //寻找x的代表点 int get_f(int x) { if(f[x]==x) { return x; } return f[x]=get_f(f[x]); } void merge(int a,int b) //合并 void merge(int a,int b) { f[get_f(a)]=get_f(b); } bool is_same_f(int a,int b) // a和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;}```