{"id":1028,"date":"2024-01-27T20:09:46","date_gmt":"2024-01-27T12:09:46","guid":{"rendered":"http:\/\/ggapa.net:81\/?p=1028"},"modified":"2024-02-04T14:17:56","modified_gmt":"2024-02-04T06:17:56","slug":"%e9%94%99%e9%a2%98%e6%9c%ac","status":"publish","type":"post","link":"http:\/\/ggapa.net:81\/2024\/01\/27\/%e9%94%99%e9%a2%98%e6%9c%ac\/","title":{"rendered":"\u9519\u9898\u672c"},"content":{"rendered":"
#include <bits\/stdc++.h>\nusing namespace std;\nconst int N = 1e5 + 5;\n\nint n, m, r, mod;\nint a[N], aa[N],dep[N], size[N], top[N], hson[N], fa[N], dfn[N];\nint cnt;\n\nstruct tree {\n int laz, sum;\n int l, r;\n}t[N * 4];\nvector<int> G[N];\n\n#define ls (x << 1)\n#define rs (x << 1 | 1)\n\nvoid pushdown(int x) { \/\/\u61d2\u6807\u8bb0\u4e0b\u4f20\u51fa\u9519\n if(!t[x].laz) return; \n t[ls].sum = (t[ls].sum + t[x].laz * (t[ls].r - t[ls].l + 1) % mod) % mod;\n t[rs].sum = (t[rs].sum + t[x].laz * (t[rs].r - t[rs].l + 1) % mod) % mod;\n t[ls].laz = (t[ls].laz + t[x].laz) % mod; t[rs].laz = (t[rs].laz + t[x].laz) % mod;\n t[x].laz = 0;\n}\nvoid pushup(int x) {\n t[x].sum = (t[ls].sum + t[rs].sum) % mod;\n}\n\nvoid bulid(int x, int l, int r) {\n t[x].l = l; t[x].r = r;\n if(l == r) {\n t[x].sum = a[l] % mod;\n return ;\n }\n int mid = (l + r) >> 1;\n bulid(ls, l, mid);\n bulid(rs, mid + 1, r);\n pushup(x);\n}\n\nvoid modify(int x, int l, int r, int ch) {\n if(l <= t[x].l && t[x].r <= r) {\n t[x].laz = (t[x].laz + ch) % mod;\n t[x].sum = (t[x].sum + ch * (t[x].r - t[x].l + 1) % mod) % mod;\n return ;\n }\n pushdown(x);\n int mid = (t[x].l + t[x].r) >> 1;\n if(l <= mid) modify(ls, l, r, ch);\n if(r > mid) modify(rs, l, r, ch);\n pushup(x);\n}\n\nint qurey(int x, int l, int r) {\n if(l <= t[x].l && t[x].r <= r) {\n return t[x].sum;\n }\n pushdown(x);\n int mid = (t[x].l + t[x].r) >> 1, tmp = 0;\n if(l <= mid) tmp = (tmp + qurey(ls, l, r)) % mod;\n if(r > mid) tmp = (tmp + qurey(rs, l, r)) % mod;\n return tmp;\n}\n\n#undef ls\n#undef rs\n\nvoid dfs1(int x, int f) {\n dep[x] = dep[f] + 1; size[x] = 1; fa[x] = f;\n for(auto to : G[x] ) {\n if(to == f) continue;\n dfs1(to, x);\n size[x] += size[to];\n if(size[hson[x]] < size[to]) hson[x] = to;\n }\n}\n\nvoid dfs2(int x, int tp) {\n dfn[x] = ++cnt;\n a[cnt] = aa[x];\n top[x] = tp;\n if(!hson[x]) return;\n dfs2(hson[x], tp);\n for(auto to : G[x] ) {\n if(to == fa[x] || to == hson[x]) continue;\n dfs2(to, to);\n }\n}\n\nvoid mrange(int x, int y, int ch) {\n while(top[x] != top[y]) {\n if(dep[top[x]] < dep[top[y]]) swap(x, y);\n modify(1, dfn[top[x]], dfn[x], ch);\n x = fa[top[x]];\n }\n if(dep[x] > dep[y]) swap(x, y);\n modify(1, dfn[x], dfn[y], ch);\n}\n\nint qrange(int x, int y) {\n int ans = 0;\n while(top[x] != top[y]) {\n if(dep[top[x]] < dep[top[y]]) swap(x, y);\n ans = (ans + qurey(1, dfn[top[x]], dfn[x])) % mod; \/\/\u53c2\u6570\u5e26\u9519\n x = fa[top[x]];\n }\n if(dep[x] > dep[y]) swap(x, y);\n ans = (ans + qurey(1, dfn[x], dfn[y])) % mod; \n return ans;\n}\n\nvoid mson(int x, int ch) {\n modify(1, dfn[x], dfn[x] + size[x] - 1, ch);\n}\n\nint qson(int x) {\n return qurey(1, dfn[x], dfn[x] + size[x] - 1) % mod;\n}\n\nint main() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n cin >> n >> m >> r >> mod;\n for(int i = 1; i <= n; i++) cin >> aa[i];\n for(int i = 1, u, v; i < n; i++) {\n cin >> u >> v;\n G[u].push_back(v);\n G[v].push_back(u);\n }\n dfs1(r, 0);\n dfs2(r, r);\n bulid(1, 1, n);\n \/\/ for(int i = 1; i <= n; i++) cout << dfn[i] << '\\n';\n while(m--) {\n int op, x, y, z;\n cin >> op;\n if(op == 1) {\n cin >> x >> y >> z;\n mrange(x, y, z);\n }\n if(op == 2) {\n cin >> x >> y;\n cout << qrange(x, y) % mod<< '\\n';\n }\n if(op == 3) {\n cin >> x >> z;\n mson(x, z);\n }\n if(op == 4) {\n cin >> x;\n\n cout << qson(x) % mod<< '\\n';\n }\n }\n return 0;\n}<\/code><\/pre>\n\u7f51\u7edc\u6700\u5927\u6d41<\/h1>\n#include <bits\/stdc++.h>\n#define rep(i, a, b) for(int i = (a); i <= (b); i++)\n#define per(i, a, b) for(int i = (a); i >= (b); i--)\nusing namespace std;\ntypedef long long ll;\nconst int N = 200 + 5, M = 5000 + 5;\n#define int long long\n\nint n, m, s, t;\nint tot = 1, head[M], cur[M];\nint dep[M];\nll ans;\n\nstruct edge {\n int v, nxt;\n ll val;\n}e[M * 2];\n\nvoid add(int u, int v, ll w) {\n e[++tot].v = v;\n e[tot].val = w;\n e[tot].nxt = head[u];\n head[u] = tot;\n\n e[++tot].v = u;\n e[tot].val = 0;\n e[tot].nxt = head[v];\n head[v] = tot;\n}\n\nint bfs() {\n queue<int> q;\n memset(dep, 0, sizeof(dep)); \/\/\u6570\u7ec4\u6ca1\u6709\u6e05\u7a7a\uff0c\u8868\u73b0\uff1a\u6b7b\u5faa\u73af\u9000\u4e0d\u51fa\u53bb\u3002\n q.push(s);\n dep[s] = 1;\n cur[s] = head[s];\n while(!q.empty()) {\n int u = q.front();\n q.pop();\n for(int i = head[u]; i; i = e[i].nxt) {\n int v = e[i].v;\n if(e[i].val > 0 && !dep[v]) {\n q.push(v);\n cur[v] = head[v];\n dep[v] = dep[u] + 1;\n if(v == t) return 1;\n }\n }\n }\n return dep[t];\n}\n\nint dfs(int u, ll sum) {\n if(u == t || !sum) return sum;\n ll k, res = 0;\n for(int i = cur[u]; i && sum; i = e[i].nxt) {\n int v = e[i].v;\n cur[u] = i; \/\/\u6ca1\u6709\u52a0\u5f27\u4f18\u5316TLE\n if(e[i].val > 0 && dep[v] == dep[u] + 1) {\n k = dfs(v, min(sum, e[i].val));\n if(k == 0) dep[v] = 0;\n e[i].val -= k;\n e[i^1].val += k; \/\/val\u9519\u5199\u6210v\uff0c\u8868\u73b0\uff1a\u53ef\u80fd\u6b7b\u5faa\u73af\uff0c\u4e5f\u53ef\u80fd\u6709\u4e00\u4e9b\u5176\u4ed6\u7684\u95ee\u9898\uff0c\u5c1a\u4e0d\u786e\u5b9a\u3002\n res += k;\n sum -= k;\n }\n }\n return res;\n}\n\nsigned main() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n cin >> n >> m >> s >> t;\n rep(i, 1, m) {\n int u, v; ll w;\n cin >> u >> v >> w;\n add(u, v, w);\n }\n while(bfs()) {\n ans += dfs(s, LLONG_MAX);\n }\n cout << ans << '\\n';\n\n return 0;\n}<\/code><\/pre>\n\u5386\u53f2\u6700\u503c\u7ebf\u6bb5\u6811<\/h1>\n#include <bits\/stdc++.h>\n#define rep(i, a, b) for(int i = (a); i <= (b); i++)\n#define per(i ,a ,b) for(int i = (a); i >= (b); i--)\n\nusing namespace std;\nconst int N = 5e5 + 5;\ntypedef long long ll;\n\nint n, m;\nint a[N];\n\nstruct tree{\n ll sum; \n int l, r, maxa, cnt, se, maxb;\n int add1, add2, add3, add4;\n}t[N << 4];\n\n#define ls x << 1\n#define rs x << 1 | 1\n\nvoid pushup(int x) {\n t[x].sum = t[ls].sum + t[rs].sum;\n t[x].maxa = max(t[ls].maxa, t[rs].maxa);\n t[x].maxb = max(t[ls].maxb, t[rs].maxb);\n if(t[ls].maxa == t[rs].maxa) {\n t[x].cnt = t[ls].cnt + t[rs].cnt;\n t[x].se = max(t[ls].se, t[rs].se);\n }\n if(t[ls].maxa > t[rs].maxa) {\n t[x].cnt = t[ls].cnt;\n t[x].se = max(t[ls].se, t[rs].maxa);\n }\n if(t[ls].maxa < t[rs].maxa) {\n t[x].cnt = t[rs].cnt;\n t[x].se = max(t[ls].maxa, t[rs].se);\n }\n}\n\nvoid apply(int x, int tag1, int tag2, int tag3, int tag4) {\n t[x].sum += 1ll * tag1 * t[x].cnt + 1ll * tag2 * (t[x].r - t[x].l + 1 - t[x].cnt);\n t[x].maxb = max(t[x].maxb, t[x].maxa + tag3); \/\/\u5728\u7ed9maxb\u8d4b\u503c\u7684\u65f6\u5019\uff0c\u5c06 = \u5199\u6210 +=\uff0c\u8868\u73b0\uff1a\u533a\u95f4\u5386\u53f2\u6700\u5927\u503c\u83ab\u540d\u5176\u5999\u5f88\u5927\u3002\n t[x].maxa += tag1;\n if(t[x].se != -2e9) t[x].se += tag2;\n t[x].add3 = max(t[x].add3, t[x].add1 + tag3);\n t[x].add4 = max(t[x].add4, t[x].add2 + tag4);\n t[x].add1 += tag1; t[x].add2 += tag2;\n} \n\nvoid pushdown(int x) {\n\n int mxn = max(t[ls].maxa, t[rs].maxa);\n if(t[ls].maxa == mxn) \n apply(ls, t[x].add1, t[x].add2, t[x].add3, t[x].add4);\n else apply(ls, t[x].add2, t[x].add2, t[x].add4, t[x].add4); \/\/else \u7684\u8d4b\u503c\u9519\u8bef\uff0c\u82e5\u4e0d\u662f\u6700\u5927\u503c\u5219\u5e94\u8be5\u8d4b\u975e\u6700\u5927\u503c\u7684\u61d2\u6807\u8bb0\uff0c\u8868\u73b0\uff1a\u6781\u5c11\u60c5\u51b5\u4e0b\uff08\u6709\u53ef\u80fd\u662f\u6570\u636e\u8303\u56f4\u592a\u5c0f\u4e86\uff09\u4f1a\u5076\u5c14\u5192\u51fa\u6765\u4e00\u4e2a\u9519\u8bef\u7684\u6c42\u548c\u64cd\u4f5c\u3002\n if(t[rs].maxa == mxn) \n apply(rs, t[x].add1, t[x].add2, t[x].add3, t[x].add4);\n else apply(rs, t[x].add2, t[x].add2, t[x].add4, t[x].add4); \/\/ else \u7684\u8d4b\u503c\u9519\u8bef\uff0c\u82e5\u4e0d\u662f\u6700\u5927\u503c\u5219\u5e94\u8be5\u8d4b\u975e\u6700\u5927\u503c\u7684\u61d2\u6807\u8bb0\uff0c\u8868\u73b0\uff1a\u6781\u5c11\u60c5\u51b5\u4e0b\uff08\u6709\u53ef\u80fd\u662f\u6570\u636e\u8303\u56f4\u592a\u5c0f\u4e86\uff09\u4f1a\u5076\u5c14\u5192\u51fa\u6765\u4e00\u4e2a\u9519\u8bef\u7684\u6c42\u548c\u64cd\u4f5c\u3002\n t[x].add1 = t[x].add2 = t[x].add3 = t[x].add4 = 0; \n}\nvoid bulid(int x, int l, int r) {\n t[x].l = l; t[x].r = r;\n t[x].add1 = t[x].add2 = t[x].add3 = t[x].add4 = 0;\n if(l == r) {\n t[x].maxa = t[x].maxb = t[x].sum = a[l];\n t[x].se = -2e9; t[x].cnt = 1; \n return ;\n }\n int mid = (l + r) >> 1;\n bulid(ls, l, mid);\n bulid(rs, mid + 1, r);\n pushup(x);\n}\n\nvoid modify_add(int x, int l, int r, int v) {\n if(t[x].r < l || t[x].l > r) return ;\n if(l <= t[x].l && t[x].r <= r) {\n apply(x, v, v, v, v);\n return ;\n }\n pushdown(x); \n modify_add(ls, l, r, v); modify_add(rs, l, r, v);\n pushup(x);\n}\n\nvoid modify_min(int x, int l, int r, int v) {\n if(t[x].r < l || t[x].l > r || v >= t[x].maxa) return ; \/\/\u9000\u51fa\u6761\u4ef6\u5c11\u5199\u4e86\uff0c\u8868\u73b0\uff1a\u628a\u6700\u5c0f\u503c\u6539\u5927\u4e86\u3002\n if(l <= t[x].l && t[x].r <= r && t[x].se < v) { \/\/\u6761\u4ef6\u5c11\u5199\u4e86\uff0c\u8868\u73b0\uff1a\u628a\u6700\u5c0f\u503c\u6539\u5927\u4e86\u3002\n int tmp = t[x].maxa - v;\n \/\/ t[x].sum -= 1ll * t[x].cnt * tmp;\n \/\/ t[x].maxa = v; t[x].add1 -= tmp;\n apply(x, -tmp, 0, -tmp, 0);\n \/\/ apply(x, v - t[x].maxa, 0, v - t[x].maxa, 0);\n return ;\n }\n pushdown(x);\n modify_min(ls, l, r, v); modify_min(rs, l, r, v);\n pushup(x);\n}\n\nll query_sum(int x, int l, int r) {\n if(t[x].r < l || t[x].l > r) return 0;\n if(l <= t[x].l && t[x].r <= r) \n return t[x].sum;\n pushdown(x);\n return query_sum(ls, l, r) + query_sum(rs, l, r);\n}\n\nint query_maxa(int x, int l, int r) {\n if(t[x].r < l || t[x].l > r) return -2e9; \/\/\u4e0d\u5c0f\u5fc3 return 0\uff0c\u800c\u4e0d\u662f\u8fd4\u56de-inf\uff0c\u8868\u73b0\uff1a\u6700\u5927\u503c\u8f93\u51fa0\n if(l <= t[x].l && t[x].r <= r) \n return t[x].maxa;\n pushdown(x);\n return max(query_maxa(ls, l, r), query_maxa(rs, l, r));\n}\n\nint query_maxb(int x, int l, int r) {\n if(t[x].r < l || t[x].l > r) return -2e9; \/\/\u4e0d\u5c0f\u5fc3 return 0\uff0c\u800c\u4e0d\u662f\u8fd4\u56de-inf\uff0c\u8868\u73b0\uff1a\u6700\u5927\u503c\u8f93\u51fa0\n if(l <= t[x].l && t[x].r <= r) \n return t[x].maxb;\n pushdown(x);\n return max(query_maxb(ls, l, r),query_maxb(rs, l, r));\n}\n\n#undef ls\n#undef rs\n\nint main() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n cin >> n >> m;\n rep(i, 1, n) cin >> a[i];\n bulid(1, 1, n);\n while(m--) {\n int op, l, r, v;\n cin >> op >> l >> r;\n if(op == 1) {\n cin >> v;\n modify_add(1, l, r, v);\n }\n if(op == 2) {\n cin >> v;\n modify_min(1, l, r, v);\n } \n if(op == 3) cout << query_sum(1, l, r) << '\\n';\n if(op == 4) cout << query_maxa(1, l, r) << '\\n';\n if(op == 5) cout << query_maxb(1, l, r) << '\\n';\n }\n\n return 0;\n}\n\n\/\/ P6242 \u3010\u6a21\u677f\u3011\u7ebf\u6bb5\u6811 3\uff08\u533a\u95f4\u6700\u503c\u64cd\u4f5c\u3001\u533a\u95f4\u5386\u53f2\u6700\u503c\uff09\n<\/code><\/pre>\n\u4e3b\u5e2d\u6811<\/h1>\n#include<bits\/stdc++.h>\nusing namespace std;\n#define int long long\nconst int maxn = 4e5 + 10, N = 2e5;\nint n, m, tot, a[maxn << 5], b[maxn << 5], rt[maxn << 5];\nint siz;\nstruct node{\n int ls, rs, sum;\n}t[maxn << 5];\nvoid init(){\n cin >> n >> m;\n for (int i = 1; i <= n; i++) cin >> a[i], b[i] = a[i];\n sort(b + 1, b + n + 1);\n siz = unique(b + 1, b + n + 1) - b - 1;\n for (int i = 1; i <= n; i++) a[i] = lower_bound(b + 1, b + siz + 1, a[i]) - b;\n}\ninline void modify(int& id, int pre, int l, int r, int x){\n id = ++tot;\n t[id] = t[pre];\n t[id].sum++;\n if (l == r) return;\n int mid = (l + r) >> 1;\n if (x <= mid) modify(t[id].ls, t[pre].ls, l, mid, x);\n else modify(t[id].rs, t[pre].rs, mid + 1, r, x);\n}\ninline int query(int id1, int id2, int l, int r, int k){\n if (l == r) return l;\n int mid = (l + r) >> 1;\n int sum1 = t[t[id1].ls].sum - t[t[id2].ls].sum;\n if (sum1 >= k) return query(t[id1].ls, t[id2].ls, l, mid, k);\n else return query(t[id1].rs, t[id2].rs, mid + 1, r, k - sum1);\n}\nvoid build_tree(){\n for (int i = 1; i <= n; i++){\n modify(rt[i], rt[i - 1], 1, siz, a[i]); \/\/ \u8b66\u949f\n }\n}\nvoid work(){\n for (int i = 1; i <= m; i++){\n int l, r, k;\n cin >> l >> r >> k;\n cout << b[query(rt[r], rt[l - 1], 1, siz, k)] << '\\n'; \n }\n}\nsigned main(){\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n init();\n build_tree();\n work();\n return 0;\n}<\/code><\/pre>\n\u6700\u77ed\u8def<\/h1>\nFloyd<\/h2>\n#include <bits\/stdc++.h>\n#define rep(i, a, b) for(int i = (a); i <= (b); i++)\n#define per(i, a, b) for(int i = (a); i >= (b); i--)\nusing namespace std;\ntypedef long long ll;\nconst int N = 500 + 5;\n#define int long long\n\nll a[N][N];\nint n, m, q;\n\nsigned main() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n cin >> n >> m >> q;\n rep(i, 1, n) rep(j, 1, n) a[i][j] = LLONG_MAX;\n for(int i = 1, u, v, w; i <= m; i++) {\n cin >> u >> v >> w;\n a[u][v] = a[v][u] = min(a[u][v], w); \/\/\u91cd\u8fb9\u5904\u7406\n }\n rep(i, 1, n) a[i][i] = 0; \/\/\u81ea\u5df1\u5230\u81ea\u5df1\n rep(k, 1, n) rep(i, 1, n) rep(j ,1 ,n)\n if(a[i][k] != LLONG_MAX && a[k][j] != LLONG_MAX && i != k && j != k) \/\/ik, jk \u4e0d\u540c \n a[i][j] = min(a[i][j], a[i][k] + a[k][j]);\n for(int i = 1, u, v; i <= q; i++) {\n cin >> u >> v;\n cout << (a[u][v] == LLONG_MAX ? -1 : a[u][v]) << '\\n';\n }\n return 0;\n}<\/code><\/pre>\nDijkstra<\/h2>\n#include <bits\/stdc++.h>\n#define rep(i, a, b) for(int i = (a), stOwxc = (b); i <= stOwxc; i++)\n#define per(i, a, b) for(int i = (a), stOwxc = (b); i >= stOwxc; i--)\nusing namespace std;\ntypedef long long ll;\n#define int long long\ntypedef pair<int, int> pii;\nconst int N = 1e5 + 5;\n\nint n, m, s = 1;\nvector<pii> G[N];\nll dis[N];\nbool vis[N];\n\nvoid Dijkstra() {\n for(int i = 1; i <= n; i++) dis[i] = 1e18;\n priority_queue<pii, vector<pii>, greater<pii>> q;\n dis[s] = 0; \n q.push({0, s});\n\n while(!q.empty()) {\n int x = q.top().second; q.pop();\n if(vis[x]) continue; \/\/vis\u6570\u7ec4\n vis[x] = true;\n for(auto to : G[x] ){\n if(dis[to.first] > dis[x] + to.second) {\n dis[to.first] = dis[x] + to.second;\n if(!vis[to.first])\n q.push({dis[to.first], to.first});\n\n }\n }\n }\n}\n\nsigned main() {\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n cin >> n >> m >> s;\n for(int i = 1, u, v, w; i <= m; i++) {\n cin >> u >> v >> w;\n G[u].push_back({v, w});\n }\n Dijkstra();\n rep(i, 1, n) cout << dis[i] << " \\n"[i == n];\n\n return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u6811\u94fe\u5256\u5206 #include <bits\/stdc++.h> using namespace std […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"_links":{"self":[{"href":"http:\/\/ggapa.net:81\/wp-json\/wp\/v2\/posts\/1028"}],"collection":[{"href":"http:\/\/ggapa.net:81\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ggapa.net:81\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ggapa.net:81\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/ggapa.net:81\/wp-json\/wp\/v2\/comments?post=1028"}],"version-history":[{"count":9,"href":"http:\/\/ggapa.net:81\/wp-json\/wp\/v2\/posts\/1028\/revisions"}],"predecessor-version":[{"id":1062,"href":"http:\/\/ggapa.net:81\/wp-json\/wp\/v2\/posts\/1028\/revisions\/1062"}],"wp:attachment":[{"href":"http:\/\/ggapa.net:81\/wp-json\/wp\/v2\/media?parent=1028"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ggapa.net:81\/wp-json\/wp\/v2\/categories?post=1028"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ggapa.net:81\/wp-json\/wp\/v2\/tags?post=1028"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}