{"id":2620,"date":"2023-04-26T02:09:15","date_gmt":"2023-04-25T18:09:15","guid":{"rendered":"https:\/\/www.shuizilong.com\/house\/?p=2620"},"modified":"2023-05-01T00:07:33","modified_gmt":"2023-04-30T16:07:33","slug":"atcoder-beginner-contest-298","status":"publish","type":"post","link":"https:\/\/www.shuizilong.com\/house\/archives\/atcoder-beginner-contest-298\/","title":{"rendered":"AtCoder Beginner Contest 298"},"content":{"rendered":"<h1>Problem F. Rook Score<\/h1>\n<p>\u6211\u4eec\u8bb0\u5f55\u6bcf\u4e00\u884c\u548c\u6bcf\u4e00\u5217\u7684\u548c R[], C[]\uff0c\u90a3\u4e48\u7b54\u6848\u662f\u67d0\u4e2a R[] + C[] &#8211; X[][]<br \/>\n\u6ce8\u610f\u8fd9\u91cc X[][] \u53ef\u80fd\u5728\u8f93\u5165\u91cc\uff0c\u4e5f\u53ef\u80fd\u4e0d\u5728\uff0c\u524d\u8005\u53ea\u6709 O(n) \u79cd\uff0c<br \/>\n\u540e\u8005\u5bf9\u6bcf\u4e00\u4e2a R\uff0c\u6211\u4eec\u53ea\u8981\u627e\u5176\u4e2d\u6700\u5927\u7684 C\uff0c\u6240\u4ee5\u5176\u5b9e\u4e5f\u53ea\u6709 O(n) \u79cd\u3002<\/p>\n<p>\u6392\u5e8f\u540e\u4e8c\u91cd\u5faa\u73af\u52a0\u4e2a\u526a\u679d\u5373\u53ef\u3002<\/p>\n<pre class=\"brush: cpp; light: false; title: ; toolbar: true; notranslate\" title=\"\">\r\n#include &lt;lastweapon\/io&gt;\r\nusing namespace lastweapon;\r\nconst int N = int(2e5) + 9;\r\nset&lt;PII&gt; P; map&lt;int, LL&gt; R, C; vector&lt;pair&lt;LL, int&gt;&gt; RR, CC;\r\nint r&#x5B;N], c&#x5B;N], x&#x5B;N];\r\nint n;\r\n\r\nint main() {\r\n\r\n#ifndef ONLINE_JUDGE\r\n    freopen(&quot;in.txt&quot;, &quot;r&quot;, stdin);\r\n    \/\/freopen(&quot;\/Users\/minakokojima\/Documents\/GitHub\/ACM-Training\/Workspace\/out.txt&quot;, &quot;w&quot;, stdout);\r\n#endif\r\n\r\n    RD(n); REP(i, n) {\r\n        RD(r&#x5B;i], c&#x5B;i], x&#x5B;i]);\r\n        R&#x5B;r&#x5B;i]] += x&#x5B;i];\r\n        C&#x5B;c&#x5B;i]] += x&#x5B;i];\r\n        P.insert({r&#x5B;i], c&#x5B;i]});\r\n    }\r\n\r\n    for (auto r: R) RR.PB({r.se, r.fi});\r\n    for (auto c: C) CC.PB({c.se, c.fi});\r\n    SRT(RR); SRT(CC); RVS(RR); RVS(CC);\r\n\r\n    LL z = 0; REP(i, n) checkMax(z, R&#x5B;r&#x5B;i]] + C&#x5B;c&#x5B;i]] - x&#x5B;i]);\r\n\r\n    REP(i, SZ(RR)) REP(j, SZ(CC)) {\r\n        LL zz = RR&#x5B;i].fi + CC&#x5B;j].fi; if (zz &lt;= z) break;\r\n        if (!CTN(P, MP(RR&#x5B;i].se, CC&#x5B;j].se))) z = zz;\r\n    }\r\n\r\n    cout &lt;&lt; z &lt;&lt; endl;\r\n}\r\n<\/pre>\n<h1>Problem Ex. Sum of Min of Length<\/h1>\n<p>\u6838\u5fc3\u8fd8\u662f\u500d\u589e\u7956\u5148\u6c42\u51fa x, y \u4e24\u70b9\u4e4b\u95f4\u7684\u4e2d\u70b9\uff0c\u7136\u540e\u8fd8\u5f97\u6839\u636e lca \u7684\u60c5\u51b5\u5404\u79cd\u5206\u7c7b\u8ba8\u8bba\u3002\u867d\u7136\u4e4b\u524d\u6211\u4eec\u5b66\u4f1a\u4e86 <a href=\"https:\/\/www.shuizilong.com\/house\/archives\/dfs-%e5%ba%8f%e6%b1%82-lca\/\">dfs \u5e8f\u6c42 lca<\/a> \u8fd9\u6837\u7684\u9ad8\u7ea7\u77e5\u8bc6\uff0c\u4f46\u770b\u8d77\u6765\u8fd9\u4e2a\u9898\u770b\u8d77\u6765\u907f\u514d\u4e0d\u4e86\u500d\u589e\u7956\u5148\uff0c\u6240\u4ee5\u8fd8\u662f\u7528\u500d\u589e\u7956\u5148\u6c42 lca \u5427\uff01<\/p>\n<p>\u9996\u5148\u8fd8\u662f dfs \u5404\u79cd\u9884\u5904\u7406\uff0c\u8fd9\u91cc\u8fd8\u8981\u9700\u8981\u4e24\u6b21 dfs\uff0c\u56e0\u4e3a\u9700\u8981\u8dd1\u6811\u5f62 dp \u6c42\u51fa dn[x] \u548c up[x]\uff0c\u5206\u522b\u8868\u793a\u5411\u4e0b\u3001\u5411\u4e0a\u7684\u8ddd\u79bb\u548c\u3002<\/p>\n<p>\u6839\u636e\u8def\u5f84\u7684\u5947\u5076\u6027\uff0c\u4e2d\u70b9\u53ef\u80fd\u5728\u4e24\u70b9\u4e4b\u95f4\u4e5f\u53ef\u80fd\u6070\u597d\u843d\u5728\u67d0\u4e2a\u70b9\u4e0a\u3002<br \/>\n\u5bf9\u4e8e\u90a3\u6837\u7684\u60c5\u51b5\uff0c\u5b83\u4efb\u610f\u8fde\u5411 x \u6216\u8005 y \u90fd\u53ef\uff0c\u4f46\u662f\u8fd9\u6837\u8ba8\u8bba\u7684\u8bdd\u611f\u89c9\u60c5\u51b5\u6709\u70b9\u591a\u538b\u529b\u6709\u70b9\u5927&#8230;<\/p>\n<p>\u4e8e\u662f\u6211\u4eec\u9700\u8981\u8bbe\u8ba1\u4e00\u4e2a\u5b50\u51fd\u6570 ff(x, y)\uff0c\u8868\u793a\u6c42\u51fa\u6240\u6709\u5230 x \u7684\u4f46\u4e0d\u7ecf\u8fc7 y \u7684\u8def\u5f84\u548c\uff01<br \/>\n\u8fd9\u6837\u7684\u8bdd\u65e0\u8bba\u5982\u4f55\u6211\u4eec\u90fd\u53ef\u4ee5\u6c42\u4e24\u4e2a\u5206\u5272\u70b9\uff0c\u907f\u514d\u8ba8\u8bba\u8def\u5f84\u7684\u5947\u5076\u6027\uff0c\u800c\u662f\u53ea\u8981\u5904\u7406\u4e00\u4e0b\u4e24\u8fb9\u6df1\u5ea6\u4e00\u6837\u7684\u60c5\u51b5\uff0c\u538b\u529b\u5927\u5e45\u51cf\u8f7b\uff01<\/p>\n<p>\u6700\u540e\u53ea\u8981\u8003\u8651\u600e\u4e48\u6c42 ff(x, y)\uff0c\u6709\u4e09\u79cd\u60c5\u51b5\uff0c\u65e0\u8bba\u54ea\u4e00\u79cd\u6211\u4eec\u90fd\u5148\u52a0\u4e0a dn[x]+up[x]\u3002<br \/>\n\u7b2c\u4e00\u79cd\u662f x \u662f y \u7684\u7956\u5148\uff0c\u90a3\u4e48\u4e0d\u4ec5\u8981\u51cf\u53bb dn[y]\uff0c\u5e76\u4e14 y \u5b50\u6811\u7684\u6240\u6709\u70b9\u8fd8\u8981\u7ee7\u7eed\u51cf\u53bb\u5c11\u7b97\u7684\u8ddd\u79bb\uff0c\u4e5f\u5c31\u662f sz[y] * (dep[y]-dep[x])\u3002<br \/>\n\u7b2c\u4e8c\u79cd\u662f y \u662f x \u7684\u7956\u5148\uff0c\u6b64\u65f6\u8981\u5148\u6c42\u51fa y \u4e00\u5708\u4f46\u4e0d\u5305\u542b x \u6240\u5728\u5206\u652f\u7684\u548c\uff0c\u6211\u4eec\u9700\u8981\u5148\u6c42\u51fa y \u7684\u5b69\u5b50\u4e2d\u662f x \u7956\u5148\u7684\u8282\u70b9 w\uff0c\u90a3\u4e48\u5c31\u662f\u51cf\u53bb dn[y]-dn[w]-sz[w]\uff0c\u7136\u540e\u518d\u7ee7\u7eed\u51cf\u53bb\u5c11\u7b97\u7684\uff0c\u4e5f\u5c31\u662f (LL)(n-sz[w]) * (dep[x]-dep[y])\u3002<br \/>\n\u6700\u540e\u4e00\u79cd\u60c5\u51b5\u662f\u6709\u53e6\u4e00\u4e2a lca\uff0c\u8fd9\u79cd\u60c5\u51b5\u53ef\u4ee5\u5408\u5e76\u5230\u60c5\u51b5\u4e00\uff0c\u53ea\u8981\u628a\u8ddd\u79bb\u51fd\u6570\u6539\u6210 (dep[x]+dep[y]-2*dep[l]) \u5373\u53ef\u3002<\/p>\n<pre class=\"brush: cpp; light: false; title: ; toolbar: true; notranslate\" title=\"\">\r\n#include &lt;lastweapon\/io&gt;\r\nusing namespace lastweapon;\r\nconst int N = int(2e5) + 9, LV = 20;\r\nint fa&#x5B;LV]&#x5B;N], dep&#x5B;N], sz&#x5B;N]; LL dn&#x5B;N], up&#x5B;N];\r\nVI adj&#x5B;N]; int n;\r\n\r\nint move_up(int x, int d) {\r\n    for (int lv=0;d;++lv,d&gt;&gt;=1)\r\n        if (d&amp;1) x = fa&#x5B;lv]&#x5B;x];\r\n    return x;\r\n}\r\ninline int lca(int x, int y) {\r\n    if (dep&#x5B;y] &lt; dep&#x5B;x]) swap(x, y); y = move_up(y, dep&#x5B;y] - dep&#x5B;x]); if (x == y) return x;\r\n    DWN(lv, LV, 0) if (fa&#x5B;lv]&#x5B;x] != fa&#x5B;lv]&#x5B;y]) x = fa&#x5B;lv]&#x5B;x], y = fa&#x5B;lv]&#x5B;y];\r\n    return fa&#x5B;0]&#x5B;x];\r\n}\r\nvoid dfs1(int u = 1, int p = 0) {\r\n    sz&#x5B;u] = 1;\r\n    for (auto v: adj&#x5B;u]) if (v != p) {\r\n        dep&#x5B;v] = dep&#x5B;u] + 1;\r\n        fa&#x5B;0]&#x5B;v] = u; for (int lv=0;fa&#x5B;lv+1]&#x5B;v]=fa&#x5B;lv]&#x5B;fa&#x5B;lv]&#x5B;v]];++lv);\r\n        dfs1(v, u);\r\n        sz&#x5B;u] += sz&#x5B;v];\r\n        dn&#x5B;u] += dn&#x5B;v] + sz&#x5B;v];\r\n    }\r\n}\r\nvoid dfs2(int u = 1, int p = 0) {\r\n    for (auto v: adj&#x5B;u]) if (v != p) {\r\n        up&#x5B;v] = up&#x5B;u] + dn&#x5B;u] + n - dn&#x5B;v] - 2*sz&#x5B;v];\r\n        dfs2(v, u);\r\n    }\r\n}\r\nint sub(int u, int v) {\r\n    return move_up(v, dep&#x5B;v]-dep&#x5B;u]-1);\r\n}\r\n\r\nLL ff(int x, int y) {\r\n    \/\/ y\r\n    \/\/ x\r\n    \/\/ z    x\r\n    \/\/x y   y\r\n    LL z = dn&#x5B;x] + up&#x5B;x]; int l = lca(x, y);\r\n    if (l == y) {\r\n        int w = sub(y, x);\r\n        z -= up&#x5B;y] + (dn&#x5B;y] - dn&#x5B;w] - sz&#x5B;w]) + (LL)(n-sz&#x5B;w])*(dep&#x5B;x]-dep&#x5B;y]);\r\n    } else {\r\n        z -= dn&#x5B;y] + (LL)sz&#x5B;y]*(dep&#x5B;x]+dep&#x5B;y]-2*dep&#x5B;l]);\r\n    }\r\n    return z;\r\n}\r\n\r\n\/\/    1\r\n\/\/  6 4 8\r\n\/\/ 75 2\r\n\/\/ 3\r\n\r\nLL f(int a, int b) {\r\n\r\n    if (a == b) return dn&#x5B;a] + up&#x5B;a];\r\n    if (dep&#x5B;a] &lt; dep&#x5B;b]) swap(a, b);\r\n    int l = lca(a, b), d = dep&#x5B;a] + dep&#x5B;b] - 2*dep&#x5B;l];\r\n    int ma = move_up(a, d\/2), mb = ma == l ? move_up(b, d\/2-1) : fa&#x5B;0]&#x5B;ma];\r\n    LL z = ff(a, mb) + ff(b, ma);\r\n    return z;\r\n}\r\n\r\nint main() {\r\n\r\n#ifndef ONLINE_JUDGE\r\n    freopen(&quot;in.txt&quot;, &quot;r&quot;, stdin);\r\n    \/\/freopen(&quot;\/Users\/minakokojima\/Documents\/GitHub\/ACM-Training\/Workspace\/out.txt&quot;, &quot;w&quot;, stdout);\r\n#endif\r\n\r\n    RD(n); DO(n-1) {\r\n        int x, y; RD(x, y);\r\n        adj&#x5B;x].PB(y);\r\n        adj&#x5B;y].PB(x);\r\n    }\r\n\r\n    dfs1(); dfs2();\r\n\r\n    Rush {\r\n        int a, b; RD(a, b);\r\n        printf(&quot;%lld\\n&quot;, f(a, b));\r\n    }\r\n}\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Problem F. Rook Score \u6211\u4eec\u8bb0\u5f55\u6bcf\u4e00\u884c\u548c\u6bcf\u4e00\u5217\u7684\u548c R[], C[]\uff0c\u90a3\u4e48\u7b54\u6848\u662f\u67d0\u4e2a R[] + C[] &#8211; X[][] \u6ce8\u610f\u8fd9\u91cc X[][] \u53ef\u80fd\u5728\u8f93\u5165\u91cc\uff0c\u4e5f\u53ef\u80fd\u4e0d\u5728\uff0c\u524d\u8005\u53ea\u6709 O(n) \u79cd\uff0c \u540e\u8005\u5bf9\u6bcf\u4e00\u4e2a R\uff0c\u6211\u4eec\u53ea\u8981\u627e\u5176\u4e2d\u6700\u5927\u7684 C\uff0c\u6240\u4ee5\u5176\u5b9e\u4e5f\u53ea\u6709 O(n) \u79cd\u3002 \u6392\u5e8f\u540e\u4e8c\u91cd\u5faa\u73af\u52a0\u4e2a\u526a\u679d\u5373\u53ef\u3002 #include &lt;lastweapon\/io&gt; using namespace lastweapon; const int N = int(2e5) + 9; set&lt;PII&gt; P; map&lt;int, LL&gt; R, C; vector&lt;pair&lt;LL, int&gt;&gt; RR, CC; int r&#x5B;N], c&#x5B;N], x&#x5B;N]; int n; int main() { #ifndef [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[1],"tags":[],"class_list":["post-2620","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2tdP7-Gg","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/2620","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/comments?post=2620"}],"version-history":[{"count":18,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/2620\/revisions"}],"predecessor-version":[{"id":2700,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/2620\/revisions\/2700"}],"wp:attachment":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/media?parent=2620"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/categories?post=2620"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/tags?post=2620"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}