跳到正文

剪贴板

剪贴板 k3gr80ql

原文
剪贴板信息

剪贴板信息

收录
更新

模板

常规代码

C++
#include <bits/stdc++.h>
#define MP make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair<ll,ll> pil;
typedef pair<int,int> pii;
const ll LL_INF=0x3f3f3f3f3f3f3f3f;
const int INT_INF=0x3f3f3f3f;
inline ll llmax(ll a,ll b){
	return (a>b?a:b);
}
inline int intmax(int a,int b){
	return (a>b?a:b);
}
inline ll llmin(ll a,ll b){
	return (a<b?a:b);
}
inline int intmin(int a,int b){
	return (a<b?a:b);
}
template<typename T,const ll MAX_LEN>
struct Queue{
	ll l,r,len;
	T val[MAX_LEN];
	Queue(){l=1,r=0,len=0;}
	inline void push_front(const T x){l=(l+MAX_LEN-1)%MAX_LEN;val[l]=x;++len;}
	inline void push_back(const T x){r=(r+1)%MAX_LEN;val[r]=x;++len;}
	inline void pop_front(){l=(l+1)%MAX_LEN;--len;}
	inline void pop_back(){r=(r+MAX_LEN-1)%MAX_LEN;--len;}
	inline T front(){return val[l];}
	inline T back(){return val[r];}
	inline ll size(){return len;}
	inline bool empty(){return !len;}
	inline void clear(){l=1,r=0,len=0;}
	inline T ind_front(ll x){return val[(l+x)%MAX_LEN];}
	inline T ind_back(ll x){return val[(r-x+MAX_LEN)%MAX_LEN];}
	inline T operator[](ll x){return ind_front(x);}
	inline void print_stdout(){for(ll i=0;i<len;i++)cout<<ind_front(i)<<' ';cout<<'\n';}
	inline void print_stderr(){for(ll i=0;i<len;i++)cerr<<ind_front(i)<<' ';cerr<<'\n';}
};
template<typename T,const ll MAX_LEN,const T LIMIT_DATA,bool cmp(T x,T y)>
struct Heap{
	T heap[MAX_LEN];ll cnt;
	Heap(){heap[0]=LIMIT_DATA;cnt=0;}
	inline ll lc(ll x){return x<<1;}
	inline ll rc(ll x){return x<<1|1;}
	inline ll fa(ll x){return x>>1;}
	inline void swap(ll a,ll b){T tmp=heap[a];heap[a]=heap[b];heap[b]=tmp;}
	inline void push_up(ll x){while(fa(x)>=1&&cmp(heap[x],heap[fa(x)]))swap(x,fa(x)),x=fa(x);}
	inline void insert(T x){heap[++cnt]=x;push_up(cnt);}
	inline void push_down(ll x){
		while(lc(x)<=cnt&&!cmp(heap[x],heap[lc(x)])||rc(x)<=cnt&&!cmp(heap[x],heap[rc(x)]))
			if(rc(x)>cnt||cmp(heap[lc(x)],heap[rc(x)]))swap(x,lc(x)),x=lc(x);
			else swap(x,rc(x)),x=rc(x);
	}
	inline T top(){return heap[1];}
	inline void erase(ll rt){
		if(rt==cnt)cnt--;
		else heap[rt]=heap[cnt],cnt--,push_down(rt);
	}
	inline ll size(){return cnt;}
	inline void clear(){cnt=0;}
	inline bool empty(){return !cnt;}
	inline void print_stdout(){for(ll i=1;i<=cnt;i++)cout<<heap[i]<<' ';cout<<'\n';}
	inline void print_stderr(){for(ll i=1;i<=cnt;i++)cerr<<heap[i]<<' ';cerr<<'\n';}
	void print_stdout_tree(ll rt,ll tb){
		if(rt>cnt)return;
		for(ll i=1;i<=tb;i++)cout<<'\t';
		cout<<heap[rt]<<'\n';
		print_stdout_tree(lc(rt),tb+1);
		print_stdout_tree(rc(rt),tb+1);
	}
	void print_stderr_tree(ll rt,ll tb){
		if(rt>cnt)return;
		for(ll i=1;i<=tb;i++)cerr<<'\t';
		cerr<<heap[rt]<<'\n';
		print_stdout_tree(lc(rt),tb+1);
		print_stdout_tree(rc(rt),tb+1);
	}
};
inline ll readl(){
	ll x=0;char c=getchar();bool frog=0;
	while(c<'0'||c>'9'){
		frog=(c=='-');
		c=getchar();
	}
	while(c>='0'&&c<='9'){
		x=(x<<3)+(x<<1)+(c&15);
		c=getchar();
	}
	return (frog?-x:x);
}
inline int readi(){
	int x=0;char c=getchar();bool frog=0;
	while(c<'0'||c>'9'){
		frog=(c=='-');
		c=getchar();
	}
	while(c>='0'&&c<='9'){
		x=(x<<3)+(x<<1)+(c&15);
		c=getchar();
	}
	return (frog?-x:x);
}
inline char readc(){
	char c=getchar();
	while(c==' '||c=='\n'){
		c=getchar();
	}
	return c;
}
inline string readstr(){
	string x="";char c=getchar();
	while(c!=' '&&c!='\n'){
		x+=c;
		c=getchar();
	}
	return x;
}
inline string readline(){
	string x="";char c=getchar();
	while(c!='\n'){
		x+=c;
		c=getchar();
	}
	return x;
}
void writel(ll x){
	if(x<0){
		putchar('-');
		x=-x;
	}
	if(x<10){
		putchar(x+'0');
	}else{
		writel(x/10);
		putchar(x%10+'0');
	}
}
void writei(int x){
	if(x<0){
		putchar('-');
		x=-x;
	}
	if(x<10){
		putchar(x+'0');
	}else{
		writei(x/10);
		putchar(x%10+'0');
	}
}
void writec(char x){
	putchar(x);
}
void writestr(string x){
	int len=x.size();
	for(int i=0;i<len;i++){
		putchar(x[i]);
	}
}
void writeendl(){
	putchar('\n');
}
const ll N=1e5+10,M=2e5+10,Mod=1e9+7;
ll n,m,k;

string StrQWQ="Fuckccf";//The problem's name.
int main(){
//	freopen((StrQWQ+".in").c_str(),"r",stdin);
//	freopen((StrQWQ+".out").c_str(),"w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);

//	fclose(stdin);
//	fclose(stdout);
	return 0;
}

高精

cppinline
	return a > b ? a : b;
}
inline ll lmn(ll a, ll b) {
	return a < b ? a : b;
}
template<const ll WIDTH, const ll BASE> //压位高精的位数 (BASE=10^WIDTH)
struct BigNum {
	vector<ll> s;
	BigNum& clean() {
		while (!s.back() && s.size() > 1)s.pop_back();
		return *this;
	}
	BigNum(ll num = 0) {
		*this = num;
	}
	BigNum(string s) {
		*this = s;
	}
	BigNum& operator = (long long num) {
		s.clear();
		do {
			s.push_back(num % BASE);
			num /= BASE;
		} while (num > 0);
		return *this;
	}
	BigNum& operator = (const string& str) {
		s.clear();
		ll x, len = (str.length() - 1) / WIDTH + 1;
		for (ll i = 0; i < len; i++) {
			ll end = str.length() - i * WIDTH;
			ll start = lmx(0ll, end - WIDTH);
			sscanf(str.substr(start, end - start).c_str(), "%lld", &x);
			s.push_back(x);
		}
		return (*this).clean();
	}
	BigNum operator + (const BigNum& b) const {
		BigNum c;
		c.s.clear();
		for (ll i = 0, g = 0; ; i++) {
			if (g == 0 && i >= s.size() && i >= b.s.size()) break;
			ll x = g;
			if (i < s.size()) x += s[i];
			if (i < b.s.size()) x += b.s[i];
			c.s.push_back(x % BASE);
			g = x / BASE;
		}
		return c;
	}
	BigNum operator - (const BigNum& b) const {
		BigNum c;
		c.s.clear();
		for (ll i = 0, g = 0; ; i++) {
			if (g == 0 && i >= s.size() && i >= b.s.size()) break;
			ll x = s[i] + g;
			if (i < b.s.size()) x -= b.s[i];
			if (x < 0) {
				g = -1;
				x += BASE;
			} else g = 0;
			c.s.push_back(x);
		}
		return c.clean();
	}
	BigNum operator * (const BigNum& b) const {
		ll i, j;
		ll g;
		vector<ll> v(s.size() + b.s.size(), 0);
		BigNum c;
		c.s.clear();
		for (i = 0; i < s.size(); i++) for (j = 0; j < b.s.size(); j++) v[i + j] += ll(s[i]) * b.s[j];
		for (i = 0, g = 0; ; i++) {
			if (g == 0 && i >= v.size()) break;
			ll x = v[i] + g;
			c.s.push_back(x % BASE);
			g = x / BASE;
		}
		return c.clean();
	}
	BigNum operator / (const BigNum& b) const {
		BigNum c = *this;
		BigNum m;
		for (ll i = s.size() - 1; i >= 0; i--) {
			m = m * BASE + s[i];
			c.s[i] = bsearch(b, m);
			m -= b * c.s[i];
		}
		return c.clean();
	}
	BigNum operator % (const BigNum& b) const { //要保证 *this>b
		BigNum c = *this;
		BigNum m;
		for (ll i = s.size() - 1; i >= 0; i--) {
			m = m * BASE + s[i];
			c.s[i] = bsearch(b, m);
			m -= b * c.s[i];
		}
		return m;
	}
	ll bsearch(const BigNum& b, const BigNum& m) const {
		ll L = 0, R = BASE - 1, x;
		while (1) {
			x = (L + R) >> 1;
			if (b * x <= m) {
				if (b * (x + 1) > m) return x;
				else L = x;
			} else R = x;
		}
	}
	BigNum& operator += (const BigNum& b) {
		*this = *this + b;
		return *this;
	}
	BigNum& operator -= (const BigNum& b) {
		*this = *this - b;
		return *this;
	}
	BigNum& operator *= (const BigNum& b) {
		*this = *this * b;
		return *this;
	}
	BigNum& operator /= (const BigNum& b) {
		*this = *this / b;
		return *this;
	}
	BigNum& operator %= (const BigNum& b) {
		*this = *this % b;
		return *this;
	}
	bool operator < (const BigNum& b) const {
		if (s.size() != b.s.size()) return s.size() < b.s.size();
		for (ll i = s.size() - 1; i >= 0; i--)
			if (s[i] != b.s[i]) return s[i] < b.s[i];
		return false;
	}
	bool operator >(const BigNum& b) const {
		return b < *this;
	}
	bool operator<=(const BigNum& b) const {
		return !(b < *this);
	}
	bool operator>=(const BigNum& b) const {
		return !(*this < b);
	}
	bool operator!=(const BigNum& b) const {
		return b < *this || *this < b;
	}
	bool operator==(const BigNum& b) const {
		return !(b < *this) && !(b > *this);
	}
};
template<const ll WIDTH, const ll BASE>
ostream& operator << (ostream& out, const BigNum<WIDTH, BASE>& x) {
	out << x.s.back();
	for (ll i = x.s.size() - 2; i >= 0; i--) {
		char buf[20]="";
		stringstream ss;
		ss.clear();
		ss << "%0" << WIDTH << "lld";
		sprintf(buf, ss.str().c_str(), x.s[i]);
		for (ll j = 0; j < strlen(buf); j++) out << buf[j];
	}
	return out;
}
template<const ll WIDTH, const ll BASE>
istream& operator >> (istream& in, BigNum<WIDTH, BASE>& x) {
	string s;
	if (!(in >> s)) return in;
	x = s;
	return in;
}

矩阵

C++
struct Matrix{
	ll n,m,v[110][110];
	Matrix(){n=m=0;memset(v,0,sizeof v);}
	Matrix(const ll& a,const ll& b,ll c[110][110]){
		n=a,m=b;
		for(ll i=1;i<=n;i++)
			for(ll j=1;j<=m;j++)
				v[i][j]=c[i][j];
	}
	Matrix(const Matrix& k){
		(*this)=k;
	}
	void operator=(const Matrix& b){
		n=b.n,m=b.m;
		for(ll i=1;i<=n;i++)
			for(ll j=1;j<=m;j++)
				v[i][j]=b.v[i][j];
	}
	Matrix operator+(const Matrix& b){
		Matrix ans;
		ans.n=n,ans.m=m;
		for(ll i=1;i<=n;i++)
			for(ll j=1;j<=m;j++)
				ans.v[i][j]=v[i][j]+b.v[i][j];
		return ans;
	}
	Matrix operator-(const Matrix& b){
		Matrix ans;
		ans.n=n,ans.m=m;
		for(ll i=1;i<=n;i++)
			for(ll j=1;j<=m;j++)
				ans.v[i][j]=v[i][j]-b.v[i][j];
		return ans;
	}
	Matrix operator*(const Matrix& b){
		Matrix ans;
		ans.n=n,ans.m=b.m;
		memset(ans.v,0,sizeof ans.v);
		for(ll i=1;i<=n;i++)
			for(ll j=1;j<=b.m;j++)
				for(ll k=1;k<=m;k++)
					ans.v[i][j]+=v[i][k]*b.v[k][j];
		return ans;
	}
	Matrix operator^(ll b){
		Matrix ans(*this),k(*this);
		b--;
		while(b){
			if(b&1)ans*=k;
			k*=k;
			b>>=1;
		}
		return ans;
	}
	void operator+=(const Matrix& b){
		(*this)=(*this)+b;
	}
	void operator-=(const Matrix& b){
		(*this)=(*this)-b;
	}
	void operator*=(const Matrix& b){
		(*this)=(*this)*b;
	}
	void operator^=(ll b){
		(*this)=(*this)^b;
	}
};

数论

乘法逆元
exgcd O(log⁡n\log n)
C++
void exgcd(ll a, ll b, ll &x, ll &y) {
	if (b == 0) {
		x = 1, y = 0;
		return;
	}
	exgcd(b, a % b, y, x);
	y -= a / b * x;
}
ll frog(ll k) {
	ll x, y;
	exgcd(k, Mod, x, y);
	return (x+Mod)%Mod;
}
线性求逆元 O(nn)
C++
ll inv[1000010];
void init(ll p){
	inv[1]=1;
	for(ll i=2;i<p;i++)inv[i]=(p-p/i)*inv[p%i]%p;
}
卡特兰数
单个 O(nn)
C++
ll catalan(ll n, ll mod) { //CTL(n)=C(2n,n)/(n+1)=CTL(n-1)*(4n-2)/(n+1)
	ll x = 1;
	for (ll i = 1; i <= n; i++)x *= i, x %= mod;
	x = frog(x, mod);
	for (ll i = n + 1; i <= n * 2; i++)x *= i, x %= mod;
	x *= frog(n + 1, mod);
	return x % mod;
}
无取模 O(nnn \sqrt{n})
C++
ll prim[1010];
ll catalan_no_mod(ll n) { //O(n*sqrt(n))
	ll x = 1;
	for (ll i = n + 1; i <= n * 2; i++) {
		ll t = i;
		for (ll j = 2; j * j <= t; j++) {
			while (t % j == 0) {
				prim[j]++;
				t /= j;
			}
		}
		if (t > 1)prim[t]++;
	}
	for (ll i = 1; i <= n + 1; i++) {
		ll t = i;
		for (ll j = 2; j * j <= t; j++) {
			while (t % j == 0) {
				prim[j]--;
				t /= j;
			}
		}
		if (t > 1)prim[t]--;
	}
	for (ll i = 1; i <= n * 2; i++) {
		while (prim[i]--)x *= i;
	}
	return x;
}
线性 O(mod+nmod+n)
C++
ll ctl[61];
ll ctl_init(ll n, ll mod) {
	if (ctl[n])return ctl[n];
	ctl[0] = 1;
	inv_init(mod);
	for (ll i = 1; i <= n; i++)ctl[n] = ctl[n - 1] * (4 * (n % mod) % mod + mod - 2) % mod * inv[(n + 1) % mod] % mod;
	return ctl[n];
}
排列组合
C
暴力 O(max{n,m}max\{n,m\})
C++
ll C(ll a,ll b){
	ll ans=1;
	for(ll i=1;i<=b;i++){
		ans*=inv[i];
		ans%=M;
	}
	for(ll i=a;i>=a-b+1;i--){
		ans*=i;
		ans%=M;
	}
	return ans;
}
Lucas O(plog⁡pnp\log_p n)

(p∈primep\in\text{prime})

C++
ll C(ll a,ll b){
	ll ans=1,res=1;
	for(ll i=1;i<=b;i++){
		ans*=inv[i];ans%=p;
	}
	for(ll i=a;i>=a-b+1;i--){
		ans*=i;ans%=p;
	}
	return ans%p;
}
ll Lcs(ll a,ll b){
	if(a<p&&b<p)return C(a,b);
	return C(a%p,b%p)*Lcs(a/p,b/p)%p;
}
exLucas O(plog⁡pp\log p)

(p∉primep\notin\text{prime})

C++
namespace exLUCAS{
	inline ll qp(ll a,ll b,ll mod){
		if(b==0)return 1;
		ll k=a,res=1;
		while(b){
			if(b&1)(res*=k)%=mod;
			(k*=k)%=mod;
			b>>=1;
		}
		return res;
	}
	ll exgcd(ll a,ll b,ll &x,ll &y){
		if(b==0){x=1,y=0;return a;}
		ll t=exgcd(b,a%b,y,x);
		y-=a/b*x;
		return t;
	}
	inline ll frog(ll a,ll p){
		ll x,y;
		exgcd(a,p,x,y);
		return (x%p+p)%p;
	}
	ll g(ll x,ll pi){
		if(x==0)return 0;
		return x/pi+g(x/pi,pi);
	}
	ll f(ll x,ll pi,ll mod){
		if(x==0)return 1;
		ll s=1;
		for(ll j=1;j<=mod;j++)
			if(j%pi)
				(s*=j)%=mod;
		s=qp(s,x/mod,mod);
		for(ll j=x/mod*mod;j<=x;j++)
			if(j%pi)
				(s*=j%mod)%=mod;
		(s*=f(x/pi,pi,mod))%=mod;
		return s;
	}
	ll b(ll n,ll m,ll pi,ll mod){
		return f(n,pi,mod)*frog(f(m,pi,mod),mod)%mod*frog(f(n-m,pi,mod),mod)%mod*qp(pi,g(n,pi)-g(m,pi)-g(n-m,pi),mod)%mod;
	}
	ll C(ll n,ll m,ll p){
		vector<pair<ll,ll> >ps;
		ll t=p,ans=0;
		for(ll i=2;i*i<=t;i++)
			if(t%i==0){
				ll cnts=1;
				while(t%i==0)t/=i,cnts*=i;
				ps.push_back(make_pair(i,cnts));
			}
		if(t>1)ps.push_back(make_pair(t,t));
		for(pair<ll,ll>pi:ps){
			ll bi=b(n,m,pi.first,pi.second);
			(ans+=bi*p/pi.second%p*frog(p/pi.second,pi.second)%p)%=p;
		}
		return ans;
	}
}
筛法
欧拉筛 O(nn)
C++
bitset<10000000>b;
ll pr[1000010],cnt;
int main(){
	for(ll i=2;i<=10000000;i++){
		if(!b[i])pr[++cnt]=i;
		for(ll j=1;j<=cnt&&pr[j]*i<=10000000;j++){
			b[pr[j]*i]=1;
			if(i%pr[j]==0)break;
		}
	}
}

图论

链式前向星
C++
ll head[MAXN], cnt;
struct Edge {
	ll u, v, dat, nxt;
} e[MAXN];
void add(ll x, ll y, ll w) {
	e[++cnt].dat = w;
	e[cnt].u = x;
	e[cnt].v = y;
	e[cnt].nxt = head[x];
	head[x] = cnt;
}
并查集 O(n×α(n)n\times \alpha(n))
C++
ll fa[1000010],a[1000010],n;
void init(){
	for(ll i=1;i<=n;i++)fa[i]=i;
}
ll fnd(ll x){
	if(fa[x]==x)return x;
	return fa[x]=fnd(fa[x]);
}
void Merge(ll x,ll y){
	ll fax=fnd(x),fay=fnd(y);
	if(fax!=fay){
		fa[fax]=fay;
	}
}
最短路
Floyed(多元最短路) O(n3n^3)
C++
ll dist[MAXN], n;
for(ll k=1;k<=n;k++)
	for(ll i=1;i<=n;i++)
		for(ll j=1;j<=n;j++)
			dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]);
Dijkstra(单源最短路) O(nlog⁡nn\log n)
C++
bool st[MAXN];
ll dist[MAXN], n, m;
ll head[MAXN], cnt;
struct Edge {
	ll u, v, dat, nxt;
} e[MAXN];
void add(ll x, ll y, ll w) {
	e[++cnt].dat = w;
	e[cnt].u = x;
	e[cnt].v = y;
	e[cnt].nxt = head[x];
	head[x] = cnt;
}
struct node{
	ll x,y;
	node(ll a,ll b){
		x=a,y=b;
	}
	bool operator<(node x)const{
		return y>x.y;
	}
};
void dij(ll x){
	memset(st,0,sizeof st);
	memset(dist,0x3f,sizeof dist);
	dist[x]=0;
	priority_queue<node>q;
	q.push(node(x,0));
	while(!q.empty()){
		node p=q.top();
		q.pop();
		if(st[p.x])continue;
		st[p.x]=1;
		for(ll i=head[p.x];i;i=e[i].nxt){
			ll j=e[i].v;
			if(dist[j]>dist[p.x]+e[i].dat){
				dist[j]=dist[p.x]+e[i].dat;
				q.push(node(j,dist[j]));
			}
		}
	}
}
Bellman-Ford(单源最短路,负环) O(nmnm)
C++
ll dist[MAXN], n, m, w;
ll head[MAXN], cnt;
struct Edge {
	ll u, v, dat, nxt;
} e[MAXN];
void add(ll x, ll y, ll w) {
	e[++cnt].dat = w;
	e[cnt].u = x;
	e[cnt].v = y;
	e[cnt].nxt = head[x];
	head[x] = cnt;
}
bool bellmanford(ll x) {
	memset(dist, 0x3f, sizeof dist);
	dist[x] = 0;
	bool flg;
	for (ll i = 1; i <= n; i++) {
		flg = 0;
		for (ll u = 1; u <= n; u++) {
			for (ll j = head[u]; j; j = e[j].nxt) {
				ll v = e[j].v, w = e[j].dat;
				if (dist[v] > dist[u] + w) {
					dist[v] = dist[u] + w;
					flg = 1;
				}
			}
		}
		if (!flg)break;
	}
	return flg;
}
最小生成树
Prim O(n2+mn^2+m)
C++
int mp[5010][5010],n,lowcost[5010],closest[5010];
bool used[5010];
ll prim(){
	ll ans=0;
	for(ll i=2;i<=n;i++){
		lowcost[i]=mp[1][i];
		if(lowcost[i]==0)lowcost[i]=0x3f3f3f3f;
		closest[i]=1;
	}
	lowcost[1]=0x3f3f3f3f;
	closest[1]=0;
	used[1]=1;
	for(ll i=2;i<=n;i++){
		ll ind=0,minn=0x3f3f3f3f;
		for(ll j=2;j<=n;j++){
			if(lowcost[j]<minn&&!used[j]){
				minn=lowcost[j];
				ind=j;
			}
		}
		if(ind==0){
			return -1;
		}
		used[ind]=1;
		ans+=minn;
		for(ll j=2;j<=n;j++){
			if(j==ind)continue;
			if(mp[ind][j]<lowcost[j]){
				lowcost[j]=mp[ind][j];
				closest[j]=ind;
			}
		}
		lowcost[ind]=0x3f3f3f3f;
		lowcost[closest[ind]]=0x3f3f3f3f;
		closest[ind]=0;
	}
	return ans;
}
Prim 堆优化 O(nlog⁡nn\log n)
C++
struct node{
	ll nxt,v,w;
}e[10000010];
ll cnt,head[10000010],dis[10000010];
bool vis[10000010];
priority_queue< pii,vector<pii>,greater<pii> >q;
ll n,m;
void add(ll x,ll y,ll w){
	e[++cnt].v=y;
	e[cnt].w=w;
	e[cnt].nxt=head[x];
	head[x]=cnt;
}
void prim(){
	ll tot=0,ans=0;
	memset(dis,0x3f,sizeof dis);
	dis[1]=0;
	q.push(make_pair(0ll,1ll));
	while(!q.empty()&&tot<n){
		ll d=q.top().first;ll u=q.top().second;
		q.pop();
		if(!vis[u]){
			tot++;
			ans+=d;
			vis[u]=1;
			for(ll i=head[u];i;i=e[i].nxt){
				if(e[i].w<dis[e[i].v]){
					dis[e[i].v]=e[i].w;
					q.push(make_pair(e[i].w,e[i].v));
				}
			}
		}
	}
	if(tot==n)write(ans);
	else puts("orz");
}
Kruscal O(mlog⁡m+nm\log m+n)
C++
struct Edge{
	ll u,v,w;
}e[10000010];
bool cmp(Edge x,Edge y){
	return x.w<y.w;
}
ll fa[10000010],n,m,s=0;
ll fnd(ll x){
	if(x==fa[x])return x;
	return fa[x]=fnd(fa[x]);
}
ll kruskal(){
	sort(e+1,e+m+1,cmp);
	ll cnt=0,ans=0;
	for(ll i=1;i<=m;i++){
		ll fau=fnd(e[i].u),fav=fnd(e[i].v);
		if(fau==fav)continue;
		fa[fau]=fav;
		cnt++;
		ans+=e[i].w;
		if(cnt==n-1)break;
	}
	if(cnt<n-1){
		return -1;
	}
	return ans;
}
LCA
树上倍增 O(nlog⁡nn\log n)
C++
ll n,cnt,head[500010],fa[500010][20],dep[500010],lg2[500010];
struct Edge{
	ll u,v,nxt;
}G[1000010];
void add(ll u,ll v){
	G[++cnt].u=u;
	G[cnt].v=v;
	G[cnt].nxt=head[u];
	head[u]=cnt;
}
void init(){
	for(ll i=2;i<=n;i++)lg2[i]=lg2[i>>1]+1;
}
void build_tree(ll x,ll father){
	fa[x][0]=father;
	dep[x]=dep[father]+1;
	for(ll i=1;i<=lg2[dep[x]];i++){
		fa[x][i]=fa[fa[x][i-1]][i-1];
	}
	for(ll i=head[x];i;i=G[i].nxt){
		if(G[i].v!=father)build_tree(G[i].v,x);
	}
}
ll LCA(ll x,ll y){
	if(dep[x]<dep[y])swap(x,y);
	while(dep[x]>dep[y])x=fa[x][lg2[dep[x]-dep[y]]];
	if(x==y)return x;
	for(ll k=lg2[dep[x]];k>=0;k--)
		if(fa[x][k]!=fa[y][k])
			x=fa[x][k],y=fa[y][k];
	return fa[x][0];
}
Euler序 O(nlog⁡nn \log n)
C++
#include <bits/stdc++.h>
using namespace std;
const int N=5e5+10;

struct EDGE{
	int to,w,next;
}edge[2*N];//无根树,一条边要存两次
int n,m,s,x,y,a,b,e_ct,euler[N<<1],euler_ct,head[N], dep[N],st[N<<1][25],lg[N<<1],d[N];
void LOG(){//预处理log(i)
	lg[1]=0;
	for(int i=2;i<(N<<1);i++)
		lg[i]=lg[i>>1]+1;
}
int add_edge(int a, int b){//链式前向星加边
	edge[++e_ct].to=b;
	edge[e_ct].next=head[a];
	head[a]=e_ct;
	return 0;
}
int dfs(int node, int fa){
	euler[++euler_ct]=node;
	dep[node]=dep[fa]+1;
	d[node]=euler_ct;//此处必然是第一次经过node,设置d[node];
	for(int i=head[node];i;i=edge[i].next)
		if(edge[i].to!=fa){
			dfs(edge[i].to, node);
			euler[++euler_ct]=node;//此处不是第一次经过node,不需要设置d[node]
		}
	return 0;
}
int init_st(){
	int k=2*n-1;
	for(int i=1;i<=k;i++)st[i][0]=euler[i];
	for(int j=1;j<=lg[k];j++){
		for(int i=1;i<=k-(1<<j)+1;i++){
			if(dep[st[i][j-1]]<dep[st[i+(1<<(j-1))][j-1]])
				st[i][j]=st[i][j-1];
			else st[i][j]=st[i+(1<<(j-1))][j-1];
		}
	}
	return 0;
}
int LCA(int u, int v){
	if(d[u]>d[v])swap(u,v);//欧拉序中让u在v之前出现
	int kk = lg[d[v]-d[u]+1];
	if(dep[st[d[u]][kk]]<dep[st[d[v]-(1<<kk)+1][kk]])
		return st[d[u]][kk];
	else return st[d[v]-(1<<kk)+1][kk];
}
int main(){
	ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
	//lg打表
	LOG();
	//输入无根树
	cin>>n>>m>>s;
	for(int i=1;i<n;i++){
		cin>>x>>y;
		add_edge(x,y);
		add_edge(y,x);
	}
	//深搜有根树,预处理欧拉序dep[]和结点在欧拉序中首次出现的位置映射d[]
	dfs(s,0);
	//预处理欧拉序关于深度的ST表
	init_st();
	while(m--){
		cin>>a>>b;
		cout<<LCA(a,b)<<"\n";
	}
	return 0;
}
Tarjan O(nn)
C++
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
const int MaxN = 500010, MaxM = 2e6 + 10;
int head[MaxN], ver[MaxM], Next[MaxM];
int fa[MaxN];
int vis[MaxN], ans[MaxM], d[MaxN];	// d[x] 表示点x 与根节点root 之间的距离
int n, m, root, tot;
vector<int> query[MaxN], query_id[MaxN];

void add(int x, int y)
{
	ver[++tot] = y, Next[tot] = head[x], head[x] = tot;
}

void add_query(int x, int y, int id)
{
	query[x].push_back(y), query_id[x].push_back(id);
	query[y].push_back(x), query_id[y].push_back(id);
}

int get(int x)
{
	if(fa[x] == x)
		return x;
	return fa[x] = get(fa[x]);
}

void tarjan(int x)
{
	vis[x] = 1;
	for(int i = head[x]; i; i = Next[i])
	{
		int y = ver[i];
		if(vis[y])	continue;
		d[y] = d[x] + 1;
		tarjan(y);
		fa[y] = x;
	}
	int len = query[x].size();
	for(int i = 0; i < len; ++i)
	{
		int y = query[x][i], id = query_id[x][i];
		if(vis[y] == 2)
		{
			ans[id] = get(y);
		}
	}
	vis[x] = 2;
}

int main()
{
	int x, y;
	scanf("%d%d%d", &n, &m, &root);
	for(int i = 1; i <= n; ++i)
	{
		fa[i] = i;
	}
	for(int i = 1; i < n; ++i)
	{
		scanf("%d%d", &x, &y);
		add(x, y);
		add(y, x);
	}
	for(int i = 1; i <= m; ++i)
	{
		scanf("%d%d", &x, &y);
		if(x == y)
		{
			ans[i] = x;
		}else{
			add_query(x, y, i);
		}
	}
	tarjan(root);
	for(int i = 1; i <= m; ++i)
		printf("%d\n", ans[i]);
	return 0;
}
拓扑排序 O(n+mn+m)
C++
ll in[5010],n;
queue<ll>q;
vector<ll>G[5010];
for(ll i=1;i<=n;i++)
	if(!in[i])
		q.push(i);
while(!q.empty()){
	ll t=q.front();
	q.pop();
	for(ll i=0;i<G[t].size();i++){
		in[G[t][i]]--;
		if(!in[G[t][i]])q.push(G[t][i]);
	}
}
树的重心 O(nn)
C++
ll n;
ll head[N],cnt;
struct Edge{ll u,v,nxt;}e[M];
inline void add(ll u,ll v){
	e[++cnt].u=u;
	e[cnt].v=v;
	e[cnt].nxt=head[u];
	head[u]=cnt;
}
ll dp[N],minn=LL_INF,fat[N];
void dfs(ll x,ll fa){
	dp[x]=1;fat[x]=fa;
	for(ll i=head[x];i;i=e[i].nxt){
		if(e[i].v!=fa)dfs(e[i].v,x),dp[x]+=dp[e[i].v];
	}
}
int main(){
 	n=readl();
	for(ll i=1;i<n;i++){
		ll x=readl(),y=readl();
		add(x,y);
		add(y,x);
	}
	dfs(1,0);
	for(ll i=1;i<=n;i++){
		ll ct=n-dp[i];
		for(ll j=head[i];j;j=e[j].nxt){
			if(e[j].v!=fat[i])ct=llmax(ct,dp[e[j].v]);
		}
		minn=llmin(minn,ct);
	}
}
树的直径 O(nn)
树上 DP O(nn)
C++
ll dp[N],ans;
void dfs(ll x,ll fa){
	for(ll i=head[x];i;i=e[i].nxt)
		if(e[i].v!=fa){
			dfs(e[i].v,x);
			ans=llmax(dp[x]+dp[e[i].v]+e[i].w,ans);
			dp[x]=llmax(dp[x],dp[e[i].v]+e[i].w);
		}
}
dfs O(nn)
C++
ll u,v,tree_d,mx,nd;
void dfs(ll x,ll fa,ll s){
	if(s>mx){
		mx=s;nd=x;
	}
	for(ll i=head[x];i;i=e[i].nxt)
		if(e[i].v!=fa)
			dfs1(e[i].v,x,s+e[i].w);
}
void dfs_td(){
	mx=-1,nd=0;
	dfs(1,0,0);
	u=nd,mx=-1;
	dfs(u,0,0);
	v=nd,tree_d=mx;
}
缩点
Tarjan O(n+mn+m)
C++
const ll N=5e5+10,M=2e6+10;
ll head[N], cnt,n,m;
struct Edge {
	ll u, v, nxt;
} e[M];
void add(ll x, ll y) {
	e[++cnt].u = x;
	e[cnt].v = y;
	e[cnt].nxt = head[x];
	head[x] = cnt;
}//链式前向星
ll dfn[N],low[N],timer;
/*
 ·dfn[i] 是一个时间戳,代表节点 i 被遍历到的次序,时间戳不可改变.
 ·low[i] 代表该子树中,且仍在栈中的最小时间戳,low[i] 相等的点在同一强连通分量中.
 ·timer 是当前时间戳.
 ·当无法继续搜索时,如果 dfn[x]=low[x],说明找到了一个强连通分量.
*/
ll sta[N],top;//栈.
bool insta[N];//是否在栈里.
ll wei[N],ring[N];//点权重;强连通分量缩点后的点.
inline void push(ll x){sta[++top]=x;insta[x]=1;}//入栈.
inline ll pop(){ll t=sta[top--];insta[t]=0;return t;}//出栈.
void tarjan(ll x){//当前节点.
	low[x]=dfn[x]=timer++;//赋值时间戳.
	push(x);
	for(ll i=head[x];i;i=e[i].nxt){//遍历.
		if(!dfn[e[i].v]){//未搜索过.
			tarjan(e[i].v);//递归.
			low[x]=llmin(low[x],low[e[i].v]);//更新 low[x].
		}else if(insta[e[i].v]){
			low[x]=llmin(low[x],dfn[e[i].v]);//更新 low[x].
		}
	}
	if(low[x]==dfn[x]){//找到了一个强连通分量.
		ll y;
		do{
			y=pop();//取栈顶元素.
			ring[y]=x;//x 是缩点后的点.
			if(x!=y){
				wei[x]+=wei[y];//权重转移.
			}
		}while(x!=y);
	}
}
ll n_head[N], n_cnt;
Edge n_e[M];
void n_add(ll x, ll y) {
	n_e[++n_cnt].u = x;
	n_e[n_cnt].v = y;
	n_e[n_cnt].nxt = n_head[x];
	n_head[x] = n_cnt;
}//新的链式前向星
int main(){
	for(ll i=1;i<=n;i++)
		if(!dfn[i])
			tarjan(i);
	//Tarjan
	for(ll i=1;i<=cnt;i++){
		ll x=ring[e[i].u],y=ring[e[i].v];
		if(x!=y){//不在同一个强连通分量上.
			n_add(x,y);
			in[y]++;
		}
	}
	//建新图
	return 0;
}
Kosaraju O(n+mn+m)
C++
ll head[N],head_[N],cnt,cnt_,tot,n,m,f[N],q[N];
struct Edge {
	ll u, v, nxt;
} e[M],e_[M];
void add(ll x, ll y) {
	e[++cnt].u = x;
	e[cnt].v = y;
	e[cnt].nxt = head[x];
	head[x] = cnt;
}
void add_(ll x, ll y) {
	e_[++cnt_].u = x;
	e_[cnt_].v = y;
	e_[cnt_].nxt = head_[x];
	head_[x] = cnt_;
}
bool vis[N];
void dfs1(ll x){
	vis[x]=1;
	for(ll i=head_[x];i;i=e_[i].nxt)
		if(!vis[e_[i].v])dfs1(e_[i].v);
	q[++tot]=x;
}
void dfs2(ll x,ll qwq){
	vis[x]=0;
	for(ll i=head[x];i;i=e[i].nxt)
		if(vis[e[i].v])dfs2(e[i].v,qwq);
	f[x]=qwq;
}
int main(){
	for(ll i=1;i<=m;i++){
		ll x,y;
		cin>>x>>y;
		add(x,y);
		add_(y,x);
	}
	for(ll i=1;i<=n;i++)
		if(!vis[i])dfs1(i);
	for(ll i=n;i>=1;i--)
		if(vis[q[i]])dfs2(q[i],q[i]);
}
割点
Tarjan O(n+mn+m)
C++
const ll N=100010,M=500010;
inline ll lmn(ll x,ll y){return x<y?x:y;}
ll n,m,cnt,head[N],dfn[N],low[N],timer;
struct Edge{ll u,v,nxt;}e[M*2];
inline void add(ll u,ll v){
	e[++cnt].u=u;
	e[cnt].v=v;
	e[cnt].nxt=head[u];
	head[u]=cnt;
}
void tarjan(ll x,ll fa){
	ll res=0;
	dfn[x]=low[x]=++timer;
	for(ll i=head[x];i;i=e[i].nxt){
		if(e[i].v==fa)continue;
		if(dfn[e[i].v])low[x]=lmn(low[x],dfn[e[i].v]);
		else{
			tarjan(e[i].v,x);
			low[x]=lmn(low[x],low[e[i].v]);
			if(dfn[x]<=low[e[i].v]){
				//    x 是割点
			}
		}
	}
}
欧拉回路
dfs O(nmnm)
C++
const ll N=510,M=1034;
ll m,n,in[N],qwq=1;
ll G[N][N];
vector<ll>v;
void dfs(ll x){
	for(ll i=1;i<=n;i++)
		if(G[x][i]){
			G[x][i]--;G[i][x]--;
			dfs(i);
		}
	v.push_back(x);
}
int main(){
	cin>>n>>m;
	for(ll i=1;i<=m;i++){
		cin>>x>>y;
		G[x][y]++;G[y][x]++;
		in[x]++;in[y]++;
	}
	for(ll i=1;i<=n;i++){
		if(in[i]%2){
			qwq=i;
			break;
		}
	}
	dfs(qwq);
	for(ll i=v.size()-1;i>=0;i--){
		cout<<v[i]<<'\n';
	}
}
二分图匹配
匈牙利 O(n3n^3)
C++
bool dfs(ll x){
	for(ll i=head[x];i;i=e[i].nxt){
		if(!st[e[i].v]){
			st[e[i].v]=1;
			if(!mat[e[i].v]||dfs(mat[e[i].v])){
				mat[e[i].v]=x;
				return true;
			}
		}
	}
	return false;
}
网络流 O(mnm \sqrt n)
C++
ll n,m,E;
//Graph
ll cnt=1,head[N],now[N];
struct Edge{ll u,v,w,nxt;}e[M];
inline void add_(ll u,ll v,ll w){
	e[++cnt].u=u;
	e[cnt].v=v;
	e[cnt].w=w;
	e[cnt].nxt=head[u];
	head[u]=cnt;
}
inline void add(ll u,ll v,ll w){
	add_(u,v,w);
	add_(v,u,0);
}
ll d[N];
//BFS
queue<ll>q;
bool bfs(ll s,ll t){
	while(q.size())q.pop();
	memset(d,-1,sizeof d);
	d[s]=1;
	now[s]=head[s];
	q.push(s);
	while(q.size()){
		ll tp=q.front();q.pop();
		for(ll i=head[tp];i;i=e[i].nxt){
			if(e[i].w>0&&d[e[i].v]==-1){
				d[e[i].v]=d[tp]+1;
				now[e[i].v]=head[e[i].v];
				q.push(e[i].v);
				if(e[i].v==t)return 1;
			}
		}
	}
	return 0;
}
//DFS
ll dfs(ll x,ll t,ll val){
	if(x==t)return val;
	ll w,used=val;
	for(ll i=now[x];i&&used;i=e[i].nxt){
	    now[x]=i;
		if(e[i].w&&d[e[i].v]==d[x]+1){
			w=dfs(e[i].v,t,min(used,e[i].w));
			if(!w)d[e[i].v]=-1;
			e[i].w-=w;
			e[i^1].w+=w;
			used-=w;
		}
	}
	return val-used;
}
//DINIC
ll Dinic(ll S,ll T){
	ll ans=0,tmp;
	while(bfs(S,T)){
	    while(tmp=dfs(S,T,1145141919810))ans+=tmp;
	}
	return ans;
}

void addd(ll S,ll T){
	for(ll i=1;i<=n;i++)add(S,i,1);
	for(ll i=1;i<=m;i++)add(i+n,T,1);
}
int main(){
	cin>>n>>m>>E;
	for(ll i=1;i<=E;i++){
		ll x,y;
		cin>>x>>y;
		if(x<=n&&y<=m)
			add(x,y+n,1);
	}
	addd((n+m+2),(n+m+1));
	cout<<Dinic((n+m+2),(n+m+1));
	return 0;
}
树链剖分
C++
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=1e5+10,M=1e5+10;
ll n,m,r,p,w[N],x,y,z,op;
namespace GRAPH{
	ll cnt,head[N];
	struct Edge{ll u,v,nxt;}e[M*2];
	inline void _add(ll u,ll v){e[++cnt]=(Edge){u,v,head[u]};head[u]=cnt;}
	inline void add(ll u,ll v){_add(u,v),_add(v,u);}
}
using namespace GRAPH;

namespace TREECUT{
	ll fa[N],sz[N],son[N],dep[N],dfn[N],top[N],timer;
	void dfs1(ll x,ll fat){
		fa[x]=fat;
		sz[x]=1;
		dep[x]=dep[fat]+1;
		for(ll i=head[x];i;i=e[i].nxt){
			if(e[i].v==fat)continue;
			dfs1(e[i].v,x);
			sz[x]+=sz[e[i].v];
			if(sz[e[i].v]>sz[son[x]])son[x]=e[i].v;
		}
	}
	void dfs2(ll x,ll tp){
		top[x]=tp;
		dfn[x]=++timer;
		if(!son[x])return;
		dfs2(son[x],tp);
		for(ll i=head[x];i;i=e[i].nxt){
			if(e[i].v==fa[x]||e[i].v==son[x])continue;
			dfs2(e[i].v,e[i].v);
		}
	}
}
using namespace TREECUT;

namespace SEGTREE{
	struct Node{
		ll v,lzy,L,R;
	}tr[4*N];
	inline ll lc(ll x){return x<<1;}
	inline ll rc(ll x){return (x<<1)|1;}
	inline void push_up(ll x){
		tr[x].v=(tr[lc(x)].v+tr[rc(x)].v)%p;
	}
	inline void Lzy(ll x){
		if(tr[x].lzy){
			ll mid=(tr[x].L+tr[x].R)>>1;
			tr[lc(x)].v+=(mid-tr[x].L+1)*tr[x].lzy;
			tr[lc(x)].v%=p;
			tr[rc(x)].v+=(tr[x].R-mid)*tr[x].lzy;
			tr[rc(x)].v%=p;
			tr[lc(x)].lzy+=tr[x].lzy;
			tr[lc(x)].lzy%=p;
			tr[rc(x)].lzy+=tr[x].lzy;
			tr[rc(x)].lzy%=p;
			tr[x].lzy=0;
		}
	}
	void build_tree(ll rt=1,ll L=1,ll R=n){
		tr[rt].L=L,tr[rt].R=R;
		tr[rt].v=tr[rt].lzy=0;
		if(L==R)return;
		ll mid=(L+R)>>1;
		build_tree(lc(rt),L,mid);
		build_tree(rc(rt),mid+1,R);
	}
	void updata(ll rt,ll l,ll r,ll x){
		if(l<=tr[rt].L&&tr[rt].R<=r){
			tr[rt].v+=(tr[rt].R-tr[rt].L+1)*x,tr[rt].lzy+=x;
			tr[rt].v%=p,tr[rt].lzy%=p;
			return;
		}
		if(tr[rt].R<l||r<tr[rt].L)return;
		Lzy(rt);
		ll mid=(tr[rt].L+tr[rt].R)>>1;
		if(l<=mid)updata(lc(rt),l,r,x);
		if(mid+1<=r)updata(rc(rt),l,r,x);
		push_up(rt);
	}
	ll query(ll rt,ll l,ll r){
		if(l<=tr[rt].L&&tr[rt].R<=r)return tr[rt].v%p;
		if(tr[rt].R<l||r<tr[rt].L)return 0;
		Lzy(rt);
		ll mid=(tr[rt].L+tr[rt].R)>>1,s=0;
		if(l<=mid)s=(s+query(lc(rt),l,r)%p)%p;
		if(mid+1<=r)s=(s+query(rc(rt),l,r)%p)%p;
		return s;
	}
}
using namespace SEGTREE;

namespace SOLVE{
	void f1(ll x,ll y,ll z){
		while(top[x]!=top[y]){
			if(dep[top[x]]<dep[top[y]])swap(x,y);
			updata(1,dfn[top[x]],dfn[x],z);
			x=fa[top[x]];
		}
		if(dep[x]<dep[y])swap(x,y);
		updata(1,dfn[y],dfn[x],z);
	}
	void f3(ll x,ll z){
		updata(1,dfn[x],dfn[x]+sz[x]-1,z);
	}
	ll f2(ll x,ll y){
		ll res=0;
		while(top[x]!=top[y]){
			if(dep[top[x]]<dep[top[y]])swap(x,y);
			res=(res+query(1,dfn[top[x]],dfn[x]))%p;
			x=fa[top[x]];
		}
		if(dep[x]<dep[y])swap(x,y);
		res=(res+query(1,dfn[y],dfn[x]))%p;
		return res;
	}
	ll f4(ll x){
		return query(1,dfn[x],dfn[x]+sz[x]-1);
	}
}
using namespace SOLVE;

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m>>r>>p;
	for(ll i=1;i<=n;i++)cin>>w[i];
	for(ll i=1;i<n;i++)cin>>x>>y,add(x,y);
	dfs1(r,0);dfs2(r,0);build_tree();
//	for(ll i=1;i<=n;i++)cout<<dep[i]<<' '<<fa[i]<<' '<<son[i]<<' '<<sz[i]<<' '<<dfn[i]<<'\n';
	for(ll i=1;i<=n;i++)
		if(w[i])updata(1,dfn[i],dfn[i],w[i]%p);
	while(m--){
		cin>>op;
		if(op==1){
			cin>>x>>y>>z;
			f1(x,y,z%p);
		}else if(op==2){
			cin>>x>>y;
			cout<<f2(x,y)<<'\n';
		}else if(op==3){
			cin>>x>>y;
			f3(x,y%p);
		}else{
			cin>>x;
			cout<<f4(x)<<'\n';
		}
	}
	return 0;
}

数据结构

线性数据结构
Chtholly Tree, Old Driver Tree, 颜色段均摊
  • 总:O(nlog⁡log⁡nn \log \log n)(数据随机)
  • 最坏:O(n2n^2)(构造)
  • split():O(log⁡n\log n)

空间复杂度:O(nn)

经典题目: Willem, Chtholly and Seniorious

经典做法:

  1. 区间赋值
  2. 暴力骗分
C++
struct Node{
	ll l,r;
	mutable ll x;
	Node(ll a,ll b,ll c){l=a,r=b,x=c;}
	bool operator<(const Node &b)const{
		return l<b.l;
	}
};
set<Node>CT;
set<Node>::iterator split(ll x){
	if(x>n)return CT.end();
	auto qwq=--CT.upper_bound(Node(x,0,0));
	if(qwq->l==x)return qwq;
	ll l=qwq->l,r=qwq->r,v=qwq->x;
	CT.erase(qwq);
	CT.insert(Node(l,x-1,v));
	return CT.insert(Node(x,r,v)).first;
}
void work(ll l,ll r,ll x){
	auto RP=split(r+1);
	auto LP=split(l);
	for(;LP!=RP;LP++){
		//Do Something...
	}
}
//Chtholly's so cute!
链表
  • 插入节点,Insert O(11)
  • 删除节点,Delete O(11)
  • 随机访问,指针式 O(nn),链式前向星 O(11)
  • 遍历,O(nn)

空间复杂度:O(nn)

经典题目: 次大值求和,P7912 [CSP-J 2021] 小熊的果篮

经典做法:

  1. 插入删除 O(1)

  2. 离散化 + 桶排 + 遍历值,O(1) 找到左右侧第一个大于(小于)该元素的值

C++
ll n,l[100010],r[100010],a[100010];

struct Node{ll x,i;}x[100010];
bool cmp(Node a,Node b){
	if(a.x!=b.x)return a.x<b.x;
	return a.i<b.i;
}

int main(){
	cin>>n;
	for(ll i=1;i<=n;i++)cin>>x[i].x,x[i].i=i;
	sort(x+1,x+n+1,cmp);
	for(ll i=1;i<=n;i++){
		a[x[i].i]=i;
		l[i]=i-1;
		r[i]=i+1;
	}
	r[n+1]=n+1;
	r[0]=1;
	l[n+1]=n;
	for(ll i=1;i<=n;i++){
		l[r[a[i]]]=l[a[i]];r[l[a[i]]]=r[a[i]];
      ...
		//l[a[i]] 是左侧第一个大于 i 的节点的序号
		//r[a[i]] 是右侧第一个大于 i 的节点的序号
	}
	/*
	for(ll i=n;i>=1;i--){
		l[r[a[i]]]=l[a[i]];r[l[a[i]]]=r[a[i]];
      ...
		//l[a[i]] 是左侧第一个小于 i 的节点的序号
		//r[a[i]] 是右侧第一个小于 i 的节点的序号
	}
	*/
	cout<<ans;
	return 0;
}
树形数据结构
树状数组

注意点:常数极小!

  • 单点更新,Updata O(log⁡n\log n)
  • 区间查询,Query O(log⁡n\log n)

空间复杂度:O(nn)

经典题目:P1908 逆序对,P3810 【模板】三维偏序(陌上花开)

经典做法: 1.单点更新,区间查询

C++
ll tr[500010],n;
inline ll lowbit(ll x){return (x&(-x));}
inline void updata(ll i,ll x){
	while(i<=n)tr[i]+=x,i+=lowbit(i);
}
inline ll query(ll i){
	ll ans=0;
	while(i>=1)ans+=tr[i],i-=lowbit(i);
	return ans;
}
inline ll query_range(ll l,ll r){
	return query(r)-query(l-1);
}
普通线段树,Segment Tree

注意点:常数大,码量大,数组开 4 倍!

  • 建树,Build Tree O(nlog⁡nn \log n)
  • 更新,Updata O(log⁡n\log n)
  • 查询,Query O(log⁡n\log n)

空间复杂度:O(nn)

C++
inline ll lc(ll x){return (x<<1);}
inline ll rc(ll x){return (x<<1|1);}
struct Segtree{
	ll l,r,v,lzy1,lzy2,...;
}tr[4*N];//一定要开 4*n!
ll a[N];
inline void Push_up(ll x){tr[x].v=...;}//数值上传
inline void Lzy(ll rt,ll L,ll R){//懒标记下传
	if(tr[x].lzy1){
		ll mid=(L+R)>>1;
		tr[rt].v=...;
		tr[lc(rt)].lzy1=...;
		tr[lc(rt)].lzy2=...;
		tr[rc(rt)].lzy1=...;
		tr[rc(rt)].lzy2=...;
		tr[rt].lzy1=0;
		tr[rt].lzy2=0;
		...;
	}
}
void Build_tree(ll rt,ll L,ll R){//rt: 节点序号; L~R: 此节点区间;
	tr[rt].l=L,tr[rt].r=R;
	tr[rt].lzy1=0,tr[rt].lzy2=1;
	if(L==R){tr[rt].v=a[L];return;}
	ll mid=(L+R)>>1;
	Build_tree(lc(rt),L,mid);
	Build_tree(rc(rt),mid+1,R);
	Push_up(rt);
}
void Updata(ll rt,ll L,ll R,ll l,ll r,ll x){//rt: 节点序号; L~R: 此节点区间; l~r: 更新区间; x: 更新值.
	if(l<=L&&R<=r){
		tr[rt].v=...;//更新
		return;
	}
	Lzy(rt,L,R);
	ll mid=(L+R)>>1;
	if(l<=mid)Updata(lc(rt),L,mid,l,r,x);
	if(mid+1<=r)Updata(rc(rt),mid+1,R,l,r,x);
	Push_up(rt);
}
ll Query(ll rt,ll L,ll R,ll l,ll r){//rt: 节点序号; L~R: 此节点区间; l~r: 查询区间.
	if(l<=L&&R<=r)return tr[rt].v;//查询
	Lzy(rt,L,R);
	ll mid=(L+R)>>1,ans=...;
	if(l<=mid)ans=Query(lc(rt),L,mid,l,r)...;
	if(mid+1<=r)ans=Query(rc(rt),mid+1,R,l,r)...;
	return ans;
}
字典树,Trie Tree

注意点:数组开 字符串数量×字符串长度\text{字符串数量}\times\text{字符串长度},尽量节省空间!

  • 查询,Query O(lengthlength)
  • 更新,Updata O(lengthlength)

空间复杂度:O(字符种类数×n×length\text{字符种类数}\times n\times length)

经典题目: P2580 于是他错误的点名开始了,P5149 会议座位

C++
struct Trie{
	ll tr[N][129],cnt[N],ind;
	Trie(){memset(tr,0,sizeof tr);memset(cnt,0,sizeof cnt);ind=0;}
	inline void updata(string k){
		ll rt=0;
		for(ll i=0;i<k.length();i++){
			if(!tr[rt][k[i]])tr[rt][k[i]]=++ind;
			rt=tr[rt][k[i]];
		}
		cnt[rt]++;
	}
	inline pair<ll,ll> query(string k){//first: the number of the string.  second: the id of the string.
		ll rt=0;
		for(ll i=0;i<k.length();i++){
			if(!tr[rt][k[i]])return make_pair(0,-1);
			rt=tr[rt][k[i]];
		}
		return make_pair(cnt[rt],rt);
	}
	void prt(ll rt,ll tb,char x,char l,char r){
		for(ll i=1;i<=tb;i++)cout<<"\t";
		cout<<x<<':'<<cnt[rt]<<'\n';
		for(char i=l;i<=r;i++)
			if(tr[rt][i])
				prt(tr[rt][i],tb+1,i,l,r);
	}
};