红名祭

· · 个人记录

祭献

## 刷题记录 | 难度 | 数量(题) | | ----------- | ----------- | | 暂无评定 | 0 | | 红题 | 22 | | 橙题 | 47 | | 黄体 | 34 | | 绿题 | 35 | | 蓝题 | 51 | | 紫题 | 42 | | 黑题 | 2 | ## 题解 | 推荐一下自己的题解 | 算法 | | ----------- | ---------- | | [P1139 题解](https://www.luogu.com.cn/blog/sangshang/p1139-ti-xie) | 搜索 & 剪枝 | | [P9902 『PG2』模拟最大流 题解](https://www.luogu.com.cn/blog/sangshang/p9902-pg2-mu-ni-zui-tai-liu-ti-xie) | 网络流 & 毒瘤算法优化 | | [P2046 [NOI2010] 海拔 题解](https://www.luogu.com.cn/blog/sangshang/p2046-noi2010-hai-ba-ti-xie) | 贪心 & 图论建模 | | [P2172 [国家集训队] 部落战争 题解](https://www.luogu.com.cn/blog/sangshang/p2172-guo-jia-ji-xun-dui-bu-lao-zhan-zheng-ti-xie) | 二分图 & 网络流 | | [P3756 [CQOI2017] 老C的方块 题解](https://www.luogu.com.cn/blog/sangshang/p3756-cqoi2017-lao-c-di-fang-kuai-ti-xie) | 毒瘤图论建模 & 网络流 | | [P4553 80人环游世界 题解](https://www.luogu.com.cn/blog/sangshang/p4553-80-ren-huan-you-shi-jie-ti-xie) | 图论建模 & 上下界网络流 | | [P8215 [THUPC2022 初赛] 分组作业](https://www.luogu.com.cn/blog/sangshang/p8215-thupc2022-chu-sai-fen-zu-zuo-ye) | 图论建模 & 网络流 | | [P4066 [SHOI2003] 吃豆豆 题解](https://www.luogu.com.cn/blog/sangshang/p4066-shoi2003-chi-dou-dou-ti-xie) | 优化图论建模 & 网络流 | | [P7173 【模板】有负圈的费用流 题解](https://www.luogu.com.cn/blog/sangshang/solution-p7173) | 模板 & 上下界网络流 | ## 写过最长的代码 1. $\bf{Highest\ Label\ Preflow\ Push}$ 算法实现 P3756 老 C 的方块。 ```cpp #include <bits/stdc++.h> using namespace std; namespace IO { const int IN_LEN = 1 << 22; char buf[IN_LEN], *front = buf, *back = buf; inline char FastGetchar() { if (front >= back) { front = buf; back = buf + fread(buf, 1, IN_LEN, stdin); if (front == back) { return EOF; } } return (front++)[0]; } inline int read() { int ret = 0; char ch = FastGetchar(); bool flag = false; while (ch < '0' || ch > '9') { if (ch == '-') { flag = true; } ch = FastGetchar(); } while (ch >= '0' && ch <= '9') { ret = ret * 10 + ch - '0'; ch = FastGetchar(); } return flag ? -ret : ret; } } using namespace IO; class HLPP { public: typedef int TYPE; static const int maxn = 100005, maxm = 1200005, inf = 0x7f7f7f7f; class Node { public: int X, Y; TYPE val; Node(int X, int Y, TYPE val): X(X), Y(Y), val(val) {} Node() {} bool operator<(const Node a)const { return ((this->X == a.X) ? (this->Y < a.Y) : (this->X < a.X)); } }; Node Target[maxn]; std::map<std::pair<int, int>, int>IDX; class edge { public: int to, rev; TYPE flow; edge(int to, TYPE flow, int rev): to(to), flow(flow), rev(rev) {} edge() {} }; std::vector<edge>vec[maxn]; TYPE Excess[maxn]; int n, m, s, t, WolkCnt, maxh, maxgaph; int h[maxn], gap[maxn], gapnex[maxn], gappre[maxn], Excess_list[maxn], Excess_nex[maxn], Cur[maxn]; HLPP(int n, int m, int s, int t): n(n), m(m), s(s), t(t), maxh(0), maxgaph(0), WolkCnt(0), Excess({0}), h({0}), gap({-1}), gapnex({-1}), gappre({-1}), Excess_list({-1}), Excess_nex({-1}) {} HLPP() {} inline void Add_Edge(int from, int to, TYPE flow) { vec[from].push_back(edge(to, flow, vec[to].size())); vec[to].push_back(edge(from, 0, vec[from].size() - 1)); } inline void Push_flow(int u, edge &e, TYPE flow) { int v = (e.to); if (!Excess[v] && (v != t) && (h[v] != inf)) { Excess_nex[v] = Excess_list[h[v]]; Excess_list[h[v]] = v; } Excess[u] -= flow, Excess[v] += flow, e.flow -= flow, vec[v][e.rev].flow += flow; } inline void Set_Height(int u, int newh) { if (~gappre[u]) { if (gappre[u] == u) { gap[h[u]] = gapnex[u]; gappre[gapnex[u]] = gapnex[u]; } else { gapnex[gappre[u]] = gapnex[u]; if (~gapnex[u]) { gappre[gapnex[u]] = gappre[u]; } } } if ((h[u] = newh) >= inf) { return; } maxgaph = std::max(maxgaph, newh); if (Excess[u]) { maxh = std::max(maxh, newh); Excess_nex[u] = Excess_list[newh]; Excess_list[newh] = u; } if (~(gapnex[u] = gap[newh])) { gappre[gapnex[u]] = u; } gap[newh] = gappre[u] = u; } inline void Globals_Relabel() { maxgaph = maxh = WolkCnt = 0; std::memset(h, inf, sizeof(h)); h[t] = 0; std::memset(gap, -1, sizeof(gap)); std::memset(gapnex, -1, sizeof(gapnex)); std::memset(gappre, -1, sizeof(gappre)); std::memset(Excess_list, -1, sizeof(Excess_list)); std::memset(Excess_nex, -1, sizeof(Excess_nex)); std::memset(Cur, 0, sizeof(Cur)); int q[maxn], front = 0, back = -1; std::memset(q, 0, sizeof(q)); for (q[++back] = t; front <= back;) { int u = q[front++]; for (edge &e : vec[u]) { int v = (e.to); if (vec[v][e.rev].flow && h[v] == inf && (v != s)) { q[++back] = v; Set_Height(v, h[u] + 1); } } } } inline void Scaling(int u) { int sz = vec[u].size(), nh = inf; for (int i = Cur[u]; i < sz; ++i) { edge &e = vec[u][i]; if (e.flow) { int v = (e.to); if (h[v] + 1 == h[u]) { Push_flow(u, e, std::min(e.flow, Excess[u])); if (!Excess[u]) { Cur[u] = i; return; } } else { nh = std::min(nh, h[v] + 1); } } } for (int i = 0; i < Cur[u]; ++i) { edge &e = vec[u][i]; if (e.flow) { nh = std::min(nh, h[(e.to)] + 1); } } ++WolkCnt; Cur[u] = 0; int oldh = h[u]; if (~gapnex[gap[oldh]]) { Set_Height(u, nh); } else { for (int i = oldh; i <= maxgaph; ++i) { for (int j = gap[i]; ~j; j = gapnex[j]) { h[j] = inf; } gap[i] = -1; } maxgaph = oldh - 1; } } inline TYPE GetMaxFlow() { Globals_Relabel(); for (edge &e : vec[s]) { if (e.flow && h[(e.to)] != inf) { Push_flow(s, e, e.flow); maxh = std::max(maxh, h[(e.to)]); } } for (; maxh; --maxh) { while (~Excess_list[maxh]) { int front = Excess_list[maxh]; Excess_list[maxh] = Excess_nex[front]; Scaling(front); if (WolkCnt >= (n << 2)) { Globals_Relabel(); } } } return Excess[t]; } inline int Check(int X, int Y) { int tmp1 = X & 1, tmp2 = Y & 3; if (tmp2 == 1) { if (tmp1 == 0) { return 1; } else { return 2; } } else if (tmp2 == 2) { if (tmp1 == 0) { return 4; } else { return 3; } } else if (tmp2 == 3) { if (tmp1 == 0) { return 3; } else { return 4; } } else { if (tmp1 == 0) { return 2; } else { return 1; } } } inline void Solve() { int R, C, N; C = read(), R = read(), N = read(); this[0] = HLPP(N + 1, inf, 0, N + 1); for (int i = 1; i <= N; ++i) { Target[i].Y = read(), Target[i].X = read(), Target[i].val = read(); } for (int i = 1; i <= N; ++i) { Node &tmp = Target[i]; IDX[make_pair(tmp.X, tmp.Y)] = i; } for (int i = 1; i <= N; ++i) { Node &now = Target[i]; int tmp = Check(now.X, now.Y); if (tmp == 1) { Add_Edge(s, i, now.val); std::map<std::pair<int, int>, int>::iterator iter; if ((iter = IDX.find(make_pair(now.X + 1, now.Y))) != IDX.end()) { int id = (*iter).second; Add_Edge(i, id, inf); } if ((iter = IDX.find(make_pair(now.X - 1, now.Y))) != IDX.end()) { int id = (*iter).second; Add_Edge(i, id, inf); } if ((now.Y & 3) == 1) { if ((iter = IDX.find(make_pair(now.X, now.Y - 1))) != IDX.end()) { int id = (*iter).second; Add_Edge(i, id, inf); } } else if ((now.Y & 3) == 0) { if ((iter = IDX.find(make_pair(now.X, now.Y + 1))) != IDX.end()) { int id = (*iter).second; Add_Edge(i, id, inf); } } } else if (tmp == 2) { if ((now.Y & 3) == 1) { std::map<std::pair<int, int>, int>::iterator iter; if ((iter = IDX.find(make_pair(now.X, now.Y + 1))) != IDX.end()) { int id = (*iter).second; TYPE val = Target[id].val; Add_Edge(i, id, std::min(now.val, val)); } } else if ((now.Y & 3) == 0) { std::map<std::pair<int, int>, int>::iterator iter; if ((iter = IDX.find(make_pair(now.X, now.Y - 1))) != IDX.end()) { int id = (*iter).second; TYPE val = Target[id].val; Add_Edge(i, id, std::min(now.val, val)); } } } else if (tmp == 3) { std::map<std::pair<int, int>, int>::iterator iter; if ((iter = IDX.find(make_pair(now.X + 1, now.Y))) != IDX.end()) { int id = (*iter).second; Add_Edge(i, id, inf); } if ((iter = IDX.find(make_pair(now.X - 1, now.Y))) != IDX.end()) { int id = (*iter).second; Add_Edge(i, id, inf); } if ((now.Y & 3) == 2) { if ((iter = IDX.find(make_pair(now.X, now.Y + 1))) != IDX.end()) { int id = (*iter).second; Add_Edge(i, id, inf); } } else if ((now.Y & 3) == 3) { if ((iter = IDX.find(make_pair(now.X, now.Y - 1))) != IDX.end()) { int id = (*iter).second; Add_Edge(i, id, inf); } } } else { Add_Edge(i, t, now.val); } } printf("%d\n", GetMaxFlow()); } }; HLPP Main; int main() { Main.Solve(); return 0; } ``` 2. 低常数的 $\bf{Splay}$ 平衡树。 ```cpp #include <bits/stdc++.h> using namespace std; namespace IO { const int LEN = (1 << 22); char buf[LEN], *front = buf, *back = buf; #define GetChar() ((front==back)&&(back=(front=buf)+fread(buf,1,LEN,stdin),front==back)?EOF:(front++)[0]) inline int read() { int res = 0; bool flag = false; char ch = GetChar(); while (ch < '0' || ch > '9') { if (ch == '-') { flag = true; } ch = GetChar(); } while (ch >= '0' && ch <= '9') { res = res * 10 + ch - '0'; ch = GetChar(); } return flag ? -res : res; } } using namespace IO; class SPLAY { public: static const int maxn = 1e6 + 1e5 + 5; class Node { public: int val, son[2], siz, fa, cnt; Node(int ls, int rs, int fa, int val, int siz, int cnt): son({ls, rs}), fa(fa), val(val), siz(siz), cnt(cnt) {} Node() {} }; Node V[maxn]; int root, tot; #define New(val) (V[++tot]=val,tot) #define update(u) (V[u].siz=(V[V[u].son[0]].siz+V[V[u].son[1]].siz+V[u].cnt)) #define clear(u) (V[u]=Node(0,0,0,0,0,0)) #define which(u) (V[V[u].fa].son[1]==u) inline void rotate(int u) { int fa = V[u].fa, gf = V[fa].fa; if (fa) { bool w = which(u); V[fa].son[w] = V[u].son[!w]; V[V[u].son[!w]].fa = fa; V[fa].fa = u; V[u].son[!w] = fa; V[u].fa = gf; if (gf) { V[gf].son[V[gf].son[1] == fa] = u; } update(fa), update(u); } } inline void splay(int u, int to) { for (int fa; (fa = V[u].fa) != to; rotate(u)) { if (V[fa].fa != to) { rotate((which(u) == which(fa)) ? fa : u); } } if (!to) { root = u; } } inline void insert(int val) { if (!root) { root = New(Node(0, 0, 0, val, 1, 1)); return; } int now = root, last = 0; while (true) { if (val == V[now].val) { ++V[now].cnt; update(now), update(last); splay(now, 0); return; } last = now; now = V[now].son[V[now].val < val]; if (!now) { now = New(Node(0, 0, last, val, 1, 1)); V[last].son[V[last].val < val] = now; update(last); splay(now, 0); return; } } } inline int GetRankByVal(int val) { int now = root, res = 0; while (now) { if (val < V[now].val) { now = V[now].son[0]; } else { res += V[V[now].son[0]].siz; if (val == V[now].val) { splay(now, 0); return res; } res += V[now].cnt; now = V[now].son[1]; } } return res; } inline int GetValByRank(int rank) { int now = root; while (true) { if (rank <= V[V[now].son[0]].siz) { now = V[now].son[0]; } else { int tmp = V[now].cnt + V[V[now].son[0]].siz; if (rank <= tmp) { splay(now, 0); return now; } rank -= tmp; now = V[now].son[1]; } } } inline int GetPre(int val) { int now = root, it = 0, ans = -0x7f7f7f7f; while (now) { if (val == V[now].val) { if (V[now].son[0]) { now = V[now].son[0]; while (V[now].son[1]) { now = V[now].son[1]; } it = now; } break; } if (V[now].val < val && V[now].val > ans) { it = now, ans = V[now].val; } now = V[now].son[V[now].val < val]; } return it; } inline int GetNex(int val) { int now = root, it = 0, ans = 0x7f7f7f7f; while (now) { if (val == V[now].val) { if (V[now].son[1]) { now = V[now].son[1]; while (V[now].son[0]) { now = V[now].son[0]; } it = now; } break; } if (V[now].val > val && V[now].val < ans) { it = now, ans = V[now].val; } now = V[now].son[V[now].val < val]; } return it; } inline int find(int val) { int now = root; while (now) { if (val == V[now].val) { return now; } now = V[now].son[V[now].val < val]; } printf("Runtime Error.\n"); return NULL; } inline void Delete(int it) { if (it == root) { if (V[it].cnt > 1) { --V[it].cnt; update(it); } else if (!V[it].son[0] && !V[it].son[1]) { clear(it); root = 0; } else if (!V[it].son[0]) { root = V[it].son[1]; V[V[it].son[1]].fa = 0; clear(it); } else if (!V[it].son[1]) { root = V[it].son[0]; V[V[it].son[0]].fa = 0; clear(it); } else { int Pre = GetPre(V[it].val); splay(Pre, 0); V[Pre].son[1] = V[it].son[1]; V[V[it].son[1]].fa = Pre; update(Pre); clear(it); } } else { if (V[it].cnt > 1) { --V[it].cnt; update(it); splay(it, 0); } else if (!V[it].son[0] && !V[it].son[1]) { V[V[it].fa].son[which(it)] = 0; update(V[it].fa); splay(V[it].fa, 0); clear(it); } else if (!V[it].son[0]) { V[V[it].fa].son[which(it)] = V[it].son[1]; V[V[it].son[1]].fa = V[it].fa; update(V[it].fa); splay(V[it].fa, 0); clear(it); } else if (!V[it].son[1]) { V[V[it].fa].son[which(it)] = V[it].son[0]; V[V[it].son[0]].fa = V[it].fa; update(V[it].fa); splay(V[it].fa, 0); clear(it); } else { int Pre = GetPre(V[it].val); splay(Pre, V[it].fa); V[Pre].son[1] = V[it].son[1]; V[V[it].son[1]].fa = Pre; update(Pre); splay(Pre, 0); clear(it); } } } inline void Pos_Insert(int val) { insert(val); } inline void Pos_Delete(int val) { Delete(find(val)); } inline int Pos_GetValByRank(int rank) { int tmp = GetValByRank(rank); splay(tmp, 0); return V[tmp].val; } inline int Pos_GetRankByVal(int val) { int tmp = GetRankByVal(val); return (tmp + 1); } inline int Pos_GetPre(int val) { int tmp = GetPre(val); splay(tmp, 0); return V[tmp].val; } inline int Pos_GetNex(int val) { int tmp = GetNex(val); splay(tmp, 0); return V[tmp].val; } inline void Print(int u) { if (V[u].son[0]) { Print(V[u].son[0]); } for (int i = 1; i <= V[u].cnt; ++i) { printf("%d ", V[u].val); } if (V[u].son[1]) { Print(V[u].son[1]); } } inline void Pos_Print() { Print(root); printf("\n"); } }; SPLAY Tree; int main() { int n, m; n = read(), m = read(); for (int i = 1; i <= n; ++i) { int x; x = read(); Tree.Pos_Insert(x); } //Tree.Pos_Print(); int ans = 0, last = 0; for (int i = 1; i <= m; ++i) { int pos, x; pos = read(), x = read(); x ^= last; if (pos == 1) { Tree.Pos_Insert(x); } else if (pos == 2) { Tree.Pos_Delete(x); } else if (pos == 3) { last = Tree.Pos_GetRankByVal(x); } else if (pos == 4) { last = Tree.Pos_GetValByRank(x); } else if (pos == 5) { last = Tree.Pos_GetPre(x); } else if (pos == 6) { last = Tree.Pos_GetNex(x); } if (pos >= 3) { ans ^= last; } //Tree.Pos_Print(); } printf("%d\n", ans); /*int n; scanf("%d", &n); for (int i = 1; i <= n; ++i) { int pos, x; scanf("%d%d", &pos, &x); if (pos == 1) { Tree.Pos_Insert(x); } else if (pos == 2) { Tree.Pos_Delete(x); } else if (pos == 3) { printf("%d\n", Tree.Pos_GetRankByVal(x)); } else if (pos == 4) { printf("%d\n", Tree.Pos_GetValByRank(x)); } else if (pos == 5) { printf("%d\n", Tree.Pos_GetPre(x)); } else { printf("%d\n", Tree.Pos_GetNex(x)); } //Tree.Pos_Print(); }*/ return 0; } ``` ## 目标 - 刷够 $50$ 道紫题,刷够 $5$ 道黑题。 - 学会 $\bf{Tarjan}$ 算法,再多刷点题。 - 多做点 $\bf{DP}$ 题,最好是斜率紫题。 - 学好数学,多做点题,多写点代码。 - 狂刷网络流建模紫题或黑题。 - 多写点题解。