inline int LCA(int x , int y) {
if(depth[x] < depth[y]) swap(x , y);
for(ri i = 20 ; i >= 0 ; -- i)
if(depth[f[x][i]] >= depth[y]) x = f[x][i];
if(x == y) return x;
for(ri i = 20 ; i >= 0 ; -- i)
if(f[x][i] != f[y][i]) x = f[x][i] , y = f[y][i];
return f[x][0];
}
dfs序、欧拉序
C++
树上差分
C++
// luogu-judger-enable-o2#include<bits/stdc++.h>using namespace std;
#define ll long long#define ri register int#define For(i , x , y) for(i = x ; i <= y; ++ i)
const int N = 3e5 + 5;
inline int read() {
int s = 0 , w = 1; char ch = getchar();
while(ch < 48 || ch > 57) {if(ch == '-') w = -1; ch = getchar();}
while(ch >= 48 && ch <= 57) s = (s << 1) + (s << 3) + (ch ^ 48) , ch = getchar();
return s * w;
}
int n , a[N] ;
int tot , head[N];
struct Edge{int to ,nxt;} e[N << 1];
inline void addedge(int x , int y) {e[++tot].to = y , e[tot].nxt = head[x] , head[x] = tot;}
int f[N][21] , depth[N] , cnt[N] , cnt1[N];
inline void dfs1(int u) {
depth[u] = depth[f[u][0]] + 1;
for(int i = 1 ; (1 << i) <= depth[u] ; ++ i) f[u][i] = f[f[u][i - 1]][i - 1];
for(int i = head[u] ; i ; i = e[i].nxt) {
int v = e[i].to;
if(v == f[u][0]) continue;
f[v][0] = u; dfs1(v);
}
}
inline int lca(int x , int y) {
if(depth[x] < depth[y]) swap(x , y);
for(int i = 20 ; i >= 0 ; -- i)
if(depth[f[x][i]] >= depth[y]) x = f[x][i];
if(x == y) return x;
for(int i = 20 ; i >= 0 ; -- i)
if(f[x][i] != f[y][i]) x = f[x][i] , y = f[y][i];
return f[x][0];
}
inline void dfs2(int u) {
for(int i = head[u] ; i ; i = e[i].nxt) {
int v = e[i].to;
if(v == f[u][0]) continue;
dfs2(v); cnt[u] += cnt[v];
}
}
int main() {
ri i , j;
n = read();
For(i , 1 , n) a[i] = read();
For(i , 1 , n - 1) {
int x = read() , y = read();
addedge(x , y); addedge(y , x);
}
dfs1(1); cnt1[a[1]] ++;
For(i , 1 , n - 1) {
int l = lca(a[i] , a[i + 1]);
++ cnt[a[i]] , ++ cnt[a[i + 1]] , -- cnt[l] , -- cnt[f[l][0]] , -- cnt1[a[i]];
} -- cnt1[a[n]];
dfs2(1);
For(i , 1 , n) printf("%d\n",cnt[i] + cnt1[i]);
return 0;
}
树哈希
5.欧拉回路
C++
#include<bits/stdc++.h>using namespace std;
#define ll long long#define R register int#define For(i , x , y) for(i = x ; i <= y ; ++ i)
const int N = 2049;
inline int read() {
int s = 0 , w = 1; char ch = getchar();
while(ch < 48 || ch > 57) {if(ch == '-') w = -1; ch = getchar();}
while(ch >= 48 && ch <= 57) s = (s << 1) + (s << 3) + (ch ^ 48) , ch = getchar();
return s * w;
}
int n , m , g[N][N] , du[N];
int stk[N] , top;
inline void dfs(int u) {
for(int i = 1 ; i <= n ; ++ i)
if(g[u][i]) -- g[u][i] , -- g[i][u] , dfs(i);
stk[++ top] = u;
}
int main() {
R i , j;
m = read();
For(i , 1 , m) {
int x = read() , y = read();
++ g[x][y] , ++ g[y][x]; n = max(n , max(x , y));
++ du[x] , ++ du[y];
}
int s = 1;
For(i , 1 , n)
if(du[i] & 1) {s = i; break;}
dfs(s);
while(top) printf("%d\n",stk[top --]);
return 0;
}
6.网络流
最大流dinic
C++
inline bool bfs() {
memset(d , 0 , sizeof(d));
queue <int> q; q.push(S); d[S] = 1;
while(!q.empty()) {
int u = q.front() ; q.pop();
Next(i , u) {
int v = e[i].to;
if(d[v] || !e[i].w) continue;
q.push(v) ; d[v] = d[u] + 1;
if(v == T) return 1;
}
} return 0;
}
int dinic(int u , int flow) {
if(u == T) return flow;
int rest = flow;
for(ri i = Iter[u] ; i && rest ; i = e[i].nxt) {
Iter[u] = i ; int v = e[i].to ;
if(d[v] != d[u] + 1 || !e[i].w) continue;
int k = dinic(v , min(rest , e[i].w) );
if(!k) d[v] = 0;
else {
e[i].w -= k ; e[i ^ 1].w += k ; rest -= k;
}
} return flow - rest;
}
二、动态规划
1.树形dp
2.状压dp
3.数位dp
4.dp优化
斜率优化
四边形不等式优化
单调队列优化
三、高级数据结构
-1.(单调)栈、队列、链表
0.分块、st表
C++
inline int query(int l , int r) {
int k = log2(r - l + 1);
return max(f[l][k] , f[r - (1 << k) + 1][k]);
}
for(int j = 1 ; j <= 20 ; j ++)
for(int i = 1 ; i + (1 << j) - 1 <= n ; i ++)
f[i][j] = max(f[i][j - 1] , f[i + (1 << (j - 1))][j - 1]);
1.堆heap
C++
priority_queue <int> q;
2.树状数组
C++
void modify(int x){while(x <= n) c[x] ++ , x += lowbit(x);}
int query(int x){
int res =0 ;
while(x) res += c[x] , x -= lowbit(x);
return res;
}
3.线段树
区间修改
C++
inline void pushdown(int rt , int L , int R) {
if(add[rt] == 0) return ; int mid = (L + R) >> 1;
int l = lc(rt) , r = rc(rt);
add[l] += add[rt] , add[r] += add[rt];
sum[l] += add[rt] * (mid - L + 1); sum[r] += add[rt] * (R - mid);
mx[l] += add[rt] , mx[r] += add[rt];
add[rt] = 0;
}
inline void modify(int rt , int l , int r , int L , int R , int val) {
if(l <= L && R <= r) {add[rt] += val; mx[rt] += val; sum[rt] += val * (R - L + 1); return ;}
int mid = (L + R) >> 1; pushdown(rt , L , R);
if(l <= mid) modify(lc(rt) , l , r , L , mid , val);
if(r > mid) modify(rc(rt) , l , r , mid + 1 , R , val);
pushup(rt);
}
区间合并
权值线段树
动态开点线段树
C++
inline void modify(int &rt , int p , int L , int R ) {
if(!rt) rt = ++ cnt;
if(L == R) {sum[rt] = 1; return ;}
int mid = (L + R) >> 1;
if(p <= mid) modify(lc[rt] , p , L , mid);
else modify(rc[rt] , p , mid + 1 , R);
pushup(rt);
}
线段树合并
C++
inline int merge(int u , int v , int L , int R) {
if(!u || !v) return u | v;
if(L == R) {sum[u] += sum[v] ; return u;}
int mid = (L + R) >> 1;
lc[u] = merge(lc[u] , lc[v] , L , mid);
rc[u] = merge(rc[u] , rc[v] , mid + 1 , R);
pushup(u); return u;
}
二维线段树
4.平衡树
Treap
Fhq_Treap
C++
#include<bits/stdc++.h>using namespace std;
const int N = 200009 , INF = 2147483647 ;
int read() {
int s = 0 , w = 1;
char ch = getchar();
while(ch < 48 || ch > 57) {if(ch== '-') w = -1; ch = getchar();}
while(ch >= 48 && ch <= 57) {s = (s << 1) + (s << 3) + (ch ^ 48) , ch = getchar();}
return s * w;
}
int size[N] , val[N] , key[N] , ch[N][2] , tot = 0 , root = 0;
void pushup(int rt) {size[rt] = size[ch[rt][0]] + size[ch[rt][1]] + 1;}
int New(int x) {
val[++tot] = x; size[tot] = 1;
key[tot] = rand();
ch[tot][0] = ch[tot][1] = 0;
return tot;
}
void split(int rt , int x , int &a , int &b) {
if(!rt) {a = b = 0;return;}
if(val[rt] <= x) a = rt , split(ch[rt][1] , x , ch[rt][1] , b);
else b = rt , split(ch[rt][0] , x , a , ch[rt][0]);
pushup(rt);
}
int merge(int a , int b) {
if(!a || !b) return a + b;
if(key[a] < key[b]) {
ch[a][1] = merge(ch[a][1] , b);
pushup(a); return a;
} else {
ch[b][0] = merge(a , ch[b][0]);
pushup(b); return b;
}
}
int kth(int rt , int x) {
while(1) {
if(x <= size[ch[rt][0]]) rt = ch[rt][0];
else if(x > size[ch[rt][0]] + 1) {
x = x - size[ch[rt][0]] - 1;
rt = ch[rt][1];
} else return rt;
}
}
void Insert(int v) {
int x = 0 , y = 0;
split(root , v , x, y);
root = merge(merge(x , New(v)) , y);
}
void Remove(int v) {
int x = 0 , y = 0 , z = 0;
split(root , v , x , z);
split(x , v - 1 , x , y);
y = merge(ch[y][0] , ch[y][1]);
root = merge(merge(x , y) , z);
}
int rank(int v) {
int x = 0 , y = 0;
split(root , v - 1 , x , y);
int ans = size[x] + 1;
root = merge(x , y);
return ans;
}
int prev(int v) {
int x = 0 , y = 0;
split(root , v - 1 , x , y);
int ans = kth(x , size[x]);
root = merge(x , y);
return ans;
}
int succ(int v) {
int x = 0 , y = 0;
split(root , v , x , y);
int ans = kth(y , 1);
root = merge(x , y);
return ans;
}
void write(int x)
{
if(x < 0)putchar('-'), x *= (-1);
if(x > 9)write(x / 10);
putchar(x % 10 + '0');
}
int main() {
srand(time(NULL));
int T = read();
while(T --) {
int opt = read() , x = read();
if(opt == 1) Insert(x);
else if(opt == 2) Remove(x);
else if(opt == 3) write(rank(x)) , putchar('\n');
else if(opt == 4) write(val[kth(root , x)]) , putchar('\n');
else if(opt == 5) write(val[prev(x)]) , putchar('\n');
else write(val[succ(x)]) , putchar('\n');
}
return 0;
}
Splay
C++
#include<bits/stdc++.h>using namespace std;
const int N = 500009;
int root , tot = 0 , ch[N][2] , par[N] , size[N] , val[N] , rev[N] , lazy[N] , sum[N] , maxl[N] , maxr[N] , ans[N];
int c[N] , n , m;
char str[10];
queue <int> q;
inline int read() {
int s = 0 , w = 1;
char ch = getchar();
while(ch < '0' || ch > '9'){if(ch == '-') w = -1; ch = getchar();}
while(ch >= '0' && ch <= '9'){s = (s << 1) + (s << 3) + (ch ^ 48); ch = getchar();}
return s * w;
}
inline void write(int x) {
if(x < 0)putchar('-'), x *= (-1);
if(x > 9)write(x / 10);
putchar(x % 10 + '0');
}
int chk(int rt) {return ch[par[rt]][1] == rt;}
void pushup(int rt) {
int l = ch[rt][0] , r = ch[rt][1];
size[rt] = size[l] + size[r] + 1;
sum[rt] = sum[l] + sum[r] + val[rt];
maxl[rt] = max(sum[l] + val[rt] + maxl[r] , maxl[l]);
maxr[rt] = max(sum[r] + val[rt] + maxr[l] , maxr[r]);
ans[rt] = max(maxr[l] + val[rt] + maxl[r] , max(ans[l] , ans[r]));
}
void pushdown(int rt) {
int l = ch[rt][0] , r = ch[rt][1];
if(lazy[rt]) {
lazy[rt] = rev[rt] = 0;
val[l] = val[r] = val[rt];
lazy[l] = lazy[r] = 1;
sum[l] = val[l] * size[l];
sum[r] = val[r] * size[r];
maxl[l] = maxr[l] = max(0 , sum[l]);
maxl[r] = maxr[r] = max(0 , sum[r]);
ans[l] = val[l] < 0 ? val[l] : sum[l];
ans[r] = val[r] < 0 ? val[r] : sum[r];
}
if(rev[rt]) {
swap(ch[l][0] , ch[l][1]); swap(ch[r][0] , ch[r][1]);
swap(maxl[l] , maxr[l]);
swap(maxl[r] , maxr[r]);
rev[l] ^= 1 , rev[r] ^= 1;
rev[rt] = 0;
}
}
void rotate(int rt) {
int f = par[rt] , gf = par[f] , d = chk(rt) , son = ch[rt][d ^ 1];
ch[f][d] = son; par[son] = f;
ch[gf][chk(f)] = rt; par[rt] = gf;
ch[rt][d ^ 1] = f; par[f] = rt;
pushup(f) , pushup(rt);
}
void Splay(int rt , int goal = 0) {
while(par[rt] != goal) {
int f = par[rt] , gf = par[f];
if(gf != goal) {
if(chk(rt) == chk(f)) rotate(f);
else rotate(rt);
}
rotate(rt);
}
if(!goal) root = rt;
}
void recycle(int rt) {
if(ch[rt][0]) recycle(ch[rt][0]);
q.push(rt);
if(ch[rt][1]) recycle(ch[rt][1]);
}
int kth(int x) {
int rt = root;
while(1) {
pushdown(rt);
if(x <= size[ch[rt][0]]) rt = ch[rt][0];
else if(x > size[ch[rt][0]] + 1) {
x = x - size[ch[rt][0]] - 1;
rt = ch[rt][1];
}
else return rt;
}
}
void connect(int f , int rt , int d){ch[f][d] = rt; par[rt] = f;}
int New(int x) {
int rt;
if(q.empty()) rt = ++ tot;
else rt = q.front() , q.pop();
ans[rt] = val[rt] = c[x];
lazy[rt] = rev[rt] = par[rt] = ch[rt][0] = ch[rt][1] = 0;
sum[rt] = c[x];
maxl[rt] = maxr[rt] = max(0 , c[x]);
size[rt] = 1;
return rt;
}
int Build(int l , int r) {
if(l > r) return 0;
int mid = (l + r) >> 1 , rt = New(mid);
if(l == r) return rt;
int L = Build(l , mid - 1 ) , R = Build(mid + 1 , r);
if(L) connect(rt , L , 0);
if(R) connect(rt , R , 1);
pushup(rt);
return rt;
}
void Insert(int pos , int len) {
int x = kth(pos + 1) , y = kth(pos + 2);
Splay(x) , Splay(y , x);
connect(y , Build(1 , len) , 0);
pushup(y); pushup(x) ;
}
void Remove(int pos , int len) {
int x = kth(pos) , y = kth(pos + len + 1);
Splay(x) , Splay(y , x);
recycle(ch[y][0]);
par[ch[y][0]] = 0; ch[y][0] = 0;
pushup(y); pushup(x);
}
void Update(int pos , int len , int v) {
int x = kth(pos) , y = kth(pos + len + 1);
Splay(x) , Splay(y , x);
lazy[ch[y][0]] = 1;
val[ch[y][0]] = v; sum[ch[y][0]] = size[ch[y][0]] * val[ch[y][0]];
maxl[ch[y][0]] = maxr[ch[y][0]] = max(0 , sum[ch[y][0]]);
ans[ch[y][0]] = v < 0 ? v : sum[ch[y][0]];
pushdown(ch[y][0]);
pushup(y) , pushup(x);
}
void Reverse(int pos , int len) {
int x = kth(pos) , y = kth(pos + len + 1);
Splay(x) , Splay(y , x);
if(!lazy[ch[y][0]]) {
rev[ch[y][0]] ^= 1;
swap(ch[ch[y][0]][0] , ch[ch[y][0]][1]);
swap(maxl[ch[y][0]] , maxr[ch[y][0]]);
pushdown(ch[y][0]);
pushup(y) , pushup(x);
}
}
int Getsum(int pos , int len) {
int x = kth(pos) , y = kth(pos + len + 1);
Splay(x) , Splay(y , x);
return sum[ch[y][0]];
}
int main()
{
while(!q.empty()) q.pop();
n = read() , m = read();
for(int i = 2 ; i <= n + 1; i ++ ) c[i] = read();
c[1] = c[n + 2] = -10000;
root = Build(1 , n + 2);
tot = n + 2;
while(m --){
scanf("%s",str);
if(str[0] == 'I') {
int pos = read() , tot = read();
for(int i = 1 ; i <= tot ; i ++) c[i] = read();
Insert(pos , tot);
}
if(str[0] == 'D') {
int pos = read() , len = read();
Remove(pos , len);
}
if(str[0] == 'M' && str[2] == 'K'){
int pos = read() , len = read() , x = read();
Update(pos , len , x);
}
if(str[0] == 'R'){
int pos = read() , len = read();
Reverse(pos , len);
}
if(str[0] == 'G'){
int pos = read() , len = read();
write(Getsum(pos , len)) , putchar('\n');
}
if(str[0] == 'M' && str[2] == 'X') write(ans[root]) , putchar('\n');
}
return 0;
}
/*附加一个单点插入void insert(int x) { int cur = root, p = 0; while (cur && val[cur] != x) { p = cur; cur = ch[cur][x > val[cur]]; } if (cur) { cnt[cur]++; } else { cur = ++ncnt; if (p) ch[p][x > val[p]] = cur; ch[cur][0] = ch[cur][1] = 0; val[cur] = x; par[cur] = p; cnt[cur] = size[cur] = 1; } splay(cur);}*/
动态开点splay
替罪羊树
5.LCT
6.树套树
7.离线
莫队
C++
// luogu-judger-enable-o2#include<bits/stdc++.h>using namespace std;
const int N = 50005;
struct Qus{int id , belong , l , r , upd;} q[N];
struct Upd{int P , col ;} a[N];
int n , m , T , tot1 = 0 , tot2 = 0 , c[N];
int cnt[1000005] , ans = 0 , out[N] , L = 1 , R = 0;
inline int read() {
int s = 0 , w = 1; char ch = getchar();
while(ch < 48 || ch > 57) {if(ch == '-') w = -1; ch = getchar();}
while(ch >= 48 && ch <= 57) s = (s << 1) + (s << 3) + (ch ^ 48) , ch = getchar();
return s * w;
}
bool cmp(Qus A , Qus B) {
if(A.belong != B.belong) return A.l < B.l;
else if(A.r != B.r) return A.r < B.r;
return A.upd < B.upd;
}
void update(int x) {
if(L <= R && L <= a[x].P && a[x].P <= R) {
cnt[c[a[x].P]] -- , cnt[a[x].col] ++;
if(!cnt[c[a[x].P]]) ans --;
if(cnt[a[x].col] == 1) ans ++;
}
swap(a[x].col , c[a[x].P]);
}
void add(int x) {
if(!cnt[c[x]]) ans ++;
cnt[c[x]] ++;
}
void remove(int x) {
cnt[c[x]] --;
if(!cnt[c[x]]) ans --;
}
int main() {
n = read() , T = read(); m = (int)sqrt(T);
for(int i = 1 ; i <= n ; i ++) c[i] = read();
for(int i = 1 ; i <= T ; i ++) {
char ch;
scanf("%c",&ch);
if(ch == 'Q') {
tot1 ++;
q[tot1].l = read() , q[tot1].r = read();
q[tot1].belong = (q[tot1].l - 1) / m + 1;
q[tot1].id = tot1;
q[tot1].upd = tot2;
} else {
a[++tot2].P = read(); a[tot2].col = read();
}
}
sort(q + 1 , q + tot1 + 1 , cmp);
// for(int i = 1 ; i <= tot1 ; i ++) printf("%d %d %d\n",q[i].upd,q[i].l,q[i].r);int j = 0 ;
for(int i = 1 ; i <= tot1 ; i ++) {
while(j < q[i].upd) update(++ j);
while(j > q[i].upd) update(j --);
while(L < q[i].l) remove(L ++);
while(L > q[i].l) add(-- L);
while(R < q[i].r) add(++ R);
while(R > q[i].r) remove(R --);
out[q[i].id] = ans;
}
for(int i = 1 ; i <= tot1 ; i ++) printf("%d\n" , out[i]);
putchar('\n');
return 0;
}
整体二分、cdq分治
线性基
C++
inline void insert(ll x ){
ri i ;
rep(i , 62 , 0) {
if(!(x >> (ll)i)) continue;
if(!p[i]) {p[i] = x; break;}
x ^= p[i];
}
}
四、数学
0.欧拉筛法、辗转相除法、快速幂
C++
void sieve() {
For(i , 2 , 1e6) {
if(!flag[i]) prime[++ tot] = i;
for(ri j = 1 ; j <= tot && i * prime[j] <= 1e6; ++ j){
flag[i * prime[j]] = 1;
if(i % prime[j] == 0) break;
}
}
}
inline int gcd(int A , int B) {
return !B ? A : gcd(B , A % B);
}
inline int qpow(int x , int y){
int ans = 1 , res = x;
while(y) {
if(y & 1) ans = (ans * res) % P;
y >>= 1 ; res = (res * res) % P;
} return ans;
}
1.Exgcd
C++
inline void Exgcd(int a , int b , int &d , int &x , int &y) {
if(!b) {d = a , x = 1 , y = 0 ; return ;}
Exgcd(b , a % b , d , y , x) , y -= a / b * x;
}
2.乘法逆元
C++
inline int inv(int a , int p){
int d = 0 , x = 0 , y = 0;
Exgcd(a , p , d , x , y);
return (x % p + p) % p;
}
3.快速乘
C++
inline ll ksc(ll A , ll B , ll p) {
ll ans = 0;
for(; B ; B >>= 1 , A = (A + A) % p) if(B & 1) ans = (ans + A) % p ;
return ans;
}
4.欧拉定理
C++
#include<bits/stdc++.h>using namespace std;
#define ll long long#define R register int
ll n,m;
bool flag =0 ;
ll read() {
ll s = 0 , w = 1; char ch = getchar();
while(ch < 48 || ch > 57) {if(ch == '-') w=-1; ch = getchar();}
while(ch >= 48 && ch <= 57) {
s = (s * 10 + ch - 48) ;
if(s >= n) flag = 1; s %= n;
ch = getchar();
}
return flag ? s * w + n : s * w;
}
ll phi(ll x) {
ll ans = x , y = sqrt(x);
for(ll i = 2; i <= y ; ++ i)
if(x % i == 0) {
ans = ans / i * (i - 1);
while(x % i == 0) x /= i;
}
if(x > 1) ans = ans / x * (x - 1);
return ans;
}
ll qpow(ll x , ll y) {
ll ans = 1 , res = x;
while(y) {
if(y & 1) (ans*=res)%=m;
y>>=1; (res*=res)%=m;
}
return ans % m;
}
int main() {
ll a , b;
cin >> a >> m;
n = phi(m);
b = read();
cout << qpow(a , b) << endl;
return 0;
}
5.中国剩余定理CRT , EXCRT
C++
inline ll ksc(ll A , ll B , ll p) {
ll ans = 0;
for(; B ; B >>= 1 , A = (A + A) % p) if(B & 1) ans = (ans + A) % p ;
return ans;
}
inline void Exgcd(ll A , ll B , ll &d , ll &x , ll &y) {
ll t;
if(!B) d = A , x = 1 , y = 0;
else {
Exgcd(B , A % B , d , x , y);
t = x , x = y , y = t - (A / B) * y;
}
}
inline ll Excrt() {
ll i ;
ll ans = a[1] % b[1] , lcm = b[1] , d , x , y;
For(i , 2 , n) {
ll B = ((a[i] - ans) % b[i] + b[i]) % b[i];
Exgcd(lcm , b[i] , d , x , y);
if(B % d != 0) return -1;
x = ksc(x , B / d , b[i]);
ans += x * lcm;
lcm *= b[i] / d;
ans = (ans % lcm + lcm) % lcm;
}
return ans ;
}
int main() {
ri i , j;
n = read();
For(i , 1 , n) b[i] = read() , a[i] = read();
cout << Excrt() << endl;
return 0;
}
6.组合数学
组合数
C++
inline int C(int n , int m) {
return fac[n] * inv(fac[m]) % P * inv(fac[n - m]) % P;
}