{"id":1761,"date":"2021-09-09T07:58:10","date_gmt":"2021-09-08T23:58:10","guid":{"rendered":"http:\/\/www.shuizilong.com\/house\/?p=1761"},"modified":"2021-09-15T10:07:44","modified_gmt":"2021-09-15T02:07:44","slug":"spoj-cot6","status":"publish","type":"post","link":"https:\/\/www.shuizilong.com\/house\/archives\/spoj-cot6\/","title":{"rendered":"SPOJ COT6. Cut on a tree"},"content":{"rendered":"<ul>\n<li><a href=\"https:\/\/www.spoj.com\/problems\/COT6\/\">https:\/\/www.spoj.com\/problems\/COT6\/<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/lychees\/ACM-Training\/tree\/master\/Archive\/Spoj\/COT%20%E7%B3%BB%E5%88%97\/COT6\">https:\/\/github.com\/lychees\/ACM-Training\/tree\/master\/Archive\/Spoj\/COT%20%E7%B3%BB%E5%88%97\/COT6<\/a><\/li>\n<\/ul>\n<p>\u524d\u6392\u819c\u62dc\u4e3b\u5e2d\u3002\u3002\u3002<\/p>\n<p><a href=\"https:\/\/t.me\/algorithm_daily_of_minako\/2039\">\u505a\u4e86\u597d\u51e0\u5929<\/a>\u3002\u3002<a href=\"https:\/\/t.me\/algorithm_daily_of_minako\/2133\">\u603b\u7b97\u641e\u5b9a\u4e86<\/a>\u3002\u3002<\/p>\n<h2>\u7b97\u6cd5\u4e00\uff1a\u674e\u8d85\u7ebf\u6bb5\u6811 + \u7ebf\u6bb5\u6811\u5408\u5e76<\/h2>\n<p>DP \u7ed3\u6784\u548c COT3 \u4e00\u6837\uff0c\u65b9\u7a0b\u548c Cash \u4e00\u6837\uff0c\u90fd\u662f\u52a8\u6001\u7ef4\u62a4\u51f8\u58f3\uff0c\u8be2\u95ee\u4e0e\u67d0\u4e2a\u5411\u91cf\u7684\u70b9\u79ef\u7684\u6700\u5c0f\u503c\u3002\u3002\u3002<br \/>\n\u540e\u6765\u6211\u7cfb\u7edf\u7684\u5b66\u4e60\u4e86\u4e00\u4e0b <a href=\"https:\/\/www.shuizilong.com\/house\/archives\/%e7%ba%bf%e6%ae%b5%e6%a0%91%e5%90%88%e5%b9%b6\/\">\u7ebf\u6bb5\u6811\u5408\u5e76<\/a>\uff0c<br \/>\n\u7136\u540e\u53d1\u73b0\u6709\u4e00\u4e2a\u9898\u7684\u505a\u6cd5\u662f\u674e\u8d85\u7ebf\u6bb5\u6811 + \u7ebf\u6bb5\u6811\u5408\u5e76\uff0c\u6765\u641e\u6811\u4e0a dp\u3002\u3002\u3002<\/p>\n<p>\u8fd9\u4e2a\u9898\u7684\u65b9\u7a0b\u548c\u90a3\u4e2a\u9898\u5dee\u4e0d\u591a\u3002\u3002\u3002\u8be2\u95ee\u5411\u91cf\u90fd\u662f (x, 1)\uff0c\u6240\u4ee5\u7528 kx+b \u6765\u641e\u66f4\u65b9\u4fbf\u3002\u3002\u3002<br \/>\n\u53ef\u4ee5\u9884\u5904\u7406\u51fa\u6240\u6709\u7684 sw\uff0c\u6765\u505a\u79bb\u6563\u5316\uff0c\u7136\u540e\u6253\u6807\u8bb0\u53ea\u8981\u6574\u4f53\u5e73\u79fb b\uff0c\u4e0d\u4f1a\u5f71\u54cd\u7ebf\u6bb5\u6811\u7684\u7ed3\u6784\u3002\u3002\u3002<br \/>\n\u7136\u540e\u5c31\u6ca1\u6709\u7136\u540e\u4e86\u3002\u3002\u3002\u590d\u6742\u5ea6 O(nlog2n)\u3002\u3002\u3002<\/p>\n<p>\u7ecf\u8fc7\u8fd9\u4e2a\u9898\u6211\u4eec\u53d1\u73b0\u3002\u3002\u7ebf\u6027\u5206\u6bb5\u51fd\u6570\u6781\u503c \u548c \u52a8\u6001\u51f8\u5305 \u95ee\u9898\u662f\u7b49\u4ef7\u7684\u3002\u3002<br \/>\n\u6839\u636e\u60c5\u51b5\u53ef\u4ee5\u4e92\u76f8\u8f6c\u6362\u3002\u3002\u770b\u600e\u4e48\u5199\u597d\u5199\u3002\u3002\u3002<\/p>\n<p>\u4f3c\u4e4e\u8fd8\u6709\u4e00\u79cd O(nlogn) \u7684\u505a\u6cd5\u3002\u3002\u3002dfs \u5e8f + cdq \u5206\u6cbb\u3002\u3002\u3002\uff08\u5c1d\u8bd5\u4e86\u4e00\u4e0b\u5931\u8d25\u4e86\uff0c\u539f\u56e0\u662f cdq \u5206\u6cbb\u4e24\u8fb9\u7684\u72b6\u6001\u4f1a\u4e92\u76f8\u4f9d\u8d56\u3002\u3002\u3002\uff09<br \/>\n\u665a\u4e0a\u518d\u8bd5\u8bd5\u3002\u3002\u3002<\/p>\n<p>\u6211\u53d1\u73b0\u4e4b\u524d\u7684\u6a21\u677f\u91cc\u6709\u4e00\u4e2a bug \u7684\u5730\u65b9\uff08\u6211\u9760\u5c45\u7136\u8fde COT6 \u7684\u6837\u4f8b\u90fd\u8fc7\u4e0d\u53bb\u3002\u3002\uff09<\/p>\n<pre class=\"brush: cpp; light: false; title: ; toolbar: true; notranslate\" title=\"\">\r\nif (T&#x5B;x].y(ml) &gt; t.y(ml) || T&#x5B;x].y(mr) &gt; t.y(mr)) swap(T&#x5B;x], t); \/\/!\r\n<\/pre>\n<p>\u5c31\u662f\u4e0a\u9762\u8fd9\u884c\uff0c\u5fc5\u987b\u8981\u4e24\u8fb9\u90fd\u68c0\u67e5\u4e00\u6b21\uff0c\u5426\u5219\u6709\u53ef\u80fd\u6070\u597d\u5728\u8fb9\u754c\uff0c\u7136\u540e\u53c8\u76f8\u7b49\u7684\u8bdd\uff0c\u6211\u8fd9\u79cd\u5199\u6cd5\u5c31\u70b8\u4e86\u3002\u3002\u3002\u3002<\/p>\n<h2>\u7b97\u6cd5\u4e8c\uff1a\u5e73\u8861\u6811\u7ef4\u62a4\u51f8\u58f3 + \u542f\u53d1\u5f0f\u5408\u5e76<\/h2>\n<p><a href=\"https:\/\/t.me\/TwilightParliament\/102671\">https:\/\/t.me\/TwilightParliament\/102671<\/a><\/p>\n<p>\u5e73\u8861\u6811\u6709\u5f88\u591a\u5199\u6cd5\u3002\u3002\u3002\u5982\u679c\u9009\u62e9 Splay \u7684\u8bdd\uff0c\u590d\u6742\u5ea6\u53ef\u4ee5\u53d8\u6210 O(nlogn) \u3002\u3002\u3002<a href=\"https:\/\/en.wikipedia.org\/wiki\/Splay_tree#Performance_theorems\">Splay \u6709\u7740\u8bb8\u591a\u975e\u5e38\u795e\u5947\u7684\u6027\u8d28<\/a>\u3002\u3002<br \/>\n\u6bd4\u5982 Dynamic Finger Property \u3002\u3002\u3002<br \/>\n\u5bf9\u4e8e n>=m\uff0c\u628a m \u4e2a\u6709\u5e8f\u7684\u5143\u7d20\u6309\u987a\u5e8f\u63d2\u5230\u4e00\u4e2a\u5df2\u7ecf\u6709 n \u4e2a\u5143\u7d20\u7684 Splay \u91cc\u3002\u3002<br \/>\n\u65f6\u95f4\u590d\u6742\u5ea6\u662f O(mlog(n\/m))\u3002\u3002\u3002<br \/>\n\u4e00\u901a\u8ba1\u7b97\uff0c\u53ef\u4ee5\u8bc1\u660e\u542f\u53d1\u5f0f\u5408\u5e76 n \u4e2a\u4e1c\u897f from scratch \u7684\u603b\u5f00\u9500\u662f O(nlogn)\u3002\u3002<br \/>\n\uff08\u5728 n \u4e2a\u5143\u7d20\u91cc\u987a\u5e8f\u8bbf\u95ee m \u4e2a\u7684\u603b\u5f00\u9500\u5dee\u4e0d\u591a\u5c31\u662f\u628a n \u5206\u6210 m \u4e2a\u6570\u7684\u548c\uff0clog \u4ee5\u540e\u52a0\u8d77\u6765\u3002<br \/>\n\u6839\u636elog\u7684\u51f9\u6027\uff0c\u6700\u5dee\u7684\u60c5\u51b5\u662f m \u4e2a\u90fd\u662f n\/m\u3002\u3002\u3002\uff09<\/p>\n<p>\u4e0d\u8fc7\u6211\u8bd5\u56fe\u627e\u4ee5\u524d\u81ea\u5df1\u7684 Splay \u7ef4\u62a4\u51f8\u58f3\u7684\u6a21\u677f\u3002\u3002\u3002\u4ea4\u4ee5\u524d\u7684\u9898\u5c45\u7136 WA \u4e86\u3002\u3002\u3002\u4e8e\u662f\u53bb\u641c\u4e86\u4e00\u4e0b\u770b\u770b\u5927\u5bb6\u73b0\u5728\u662f\u600e\u4e48\u505a\u7684\u3002\u3002\u3002<br \/>\n\u4e8e\u662f <a href=\"https:\/\/t.me\/TwilightParliament\/102671\">\u627e\u5230\u4e86\u4e00\u4efd\u975e\u5e38\u77ed\u7684 Multiset \u7ef4\u62a4\u51f8\u58f3\u7684\u4ee3\u7801<\/a>\u3002\u3002\u3002\u3002\u3002<\/p>\n<p>\u8003\u636e\u4e86\u4e00\u4e0b\u3002\u3002\u6765\u81ea CF \u7684\u67d0\u4e2a\u745e\u5178\u7684 IGM \u9009\u624b <a href=\"https:\/\/codeforces.com\/profile\/simonlindholm\">simonlindholm<\/a>\u3002\u3002<br \/>\n\u6a21\u677f\u4ed3\u5e93\u5728 <a href=\"https:\/\/github.com\/kth-competitive-programming\/kactl\/blob\/main\/content\/data-structures\/LineContainer.h\">\u8fd9\u91cc<\/a>\u3002\u3002\u3002<\/p>\n<p>\u4e2a\u4eba\u611f\u89c9\u8fd9\u4efd\u4ee3\u7801\u5b9e\u73b0\u7684\u975e\u5e38\u4f18\u79c0\u3002\u3002\u3002\u7279\u522b\u662f\u63d2\u5165\u8fc7\u7a0b\u3002\u3002\u3002\u6bd4 <a href=\"https:\/\/github.com\/lychees\/ACM-Training\/blob\/master\/Archive\/Spoj\/COT%20%E7%B3%BB%E5%88%97\/COT6\/%E5%B9%B3%E8%A1%A1%E6%A0%91%EF%BC%88clj%EF%BC%89%20%2B%20%E5%90%AF%E5%8F%91%E5%BC%8F%E5%90%88%E5%B9%B6.cpp\">CLJ \u7684\u7248\u672c<\/a> \u90fd\u8981\u77ed\u597d\u591a\u3002\u3002\u3002<\/p>\n<p>\u8fd9\u4e2a\u662f\u4e00\u5806\u7ebf\u6027\u51fd\u6570\u6c42\u6781\u503c\u7684\u3002\u3002\u7136\u540e p \u7ef4\u62a4\u7684\u662f\u76f8\u90bb\u76f4\u7ebf\u7684\u4ea4\u70b9\u3002\u3002\u3002<br \/>\n\u67e5\u8be2\u7684\u65f6\u5019\u76f4\u63a5\u7528 p \u53bb\u4e8c\u5206\u67e5\u627e\u5c31\u53ef\u4ee5\u4e86\u3002\u3002\u3002<\/p>\n<p>\u53ea\u8981\u5bf9\u6bcf\u4e2a\u51f8\u58f3\u6574\u4f53\u6253\u6807\u8bb0\uff0c\u7136\u540e\u628a\u6a21\u677f\u4ece\u6c42\u6700\u5927\u6539\u6210\u6c42\u6700\u5c0f\u5c31\u53ef\u4ee5\u901a\u8fc7\u4e86\u3002\u3002<\/p>\n<pre class=\"brush: cpp; light: false; title: ; toolbar: true; notranslate\" title=\"\">\r\nconst int N = int(1.2e6) + 9;\r\n\r\n#define ll LL\r\nstruct Line {\r\n\tmutable ll k, m, p;\r\n\tll eval (ll x) { return k*x+m; }\r\n\tbool operator&lt;(const Line&amp; o) const { return k &gt; o.k; }\r\n\tbool operator&lt;(ll x) const { return p &lt; x; }\r\n};\r\n\r\nstruct LC : multiset&lt;Line,less&lt;&gt; &gt; {\r\n\tconst ll inf = LLONG_MAX;\r\n\tLL up;\r\n\tll div(ll a, ll b) { return a\/b - ((a^b) &lt; 0 &amp;&amp; a%b); }\r\n\tll bet(const Line&amp; x, const Line&amp; y) {\r\n\t\tif (x.k == y.k) return x.m &lt;= y.m ? inf : -inf;\r\n\t\treturn div(y.m-x.m, x.k-y.k);\r\n\t}\r\n\tbool isect(iterator x, iterator y) {\r\n\t\tif (y == end()) { x-&gt;p = inf; return 0; }\r\n\t\tx-&gt;p = bet(*x,*y); return x-&gt;p &gt;= y-&gt;p;\r\n\t}\r\n\tvoid add(ll k, ll m) {\r\n\t    m -= up;\r\n\t\tauto z = insert({k,m,0}), y = z++, x = y;\r\n\t\twhile (isect(y,z)) z = erase(z);\r\n\t\tif (x != begin() &amp;&amp; isect(--x, y)) isect(x, y=erase(y));\r\n\t\twhile ((y=x) != begin() &amp;&amp; (--x)-&gt;p &gt;= y-&gt;p) isect(x, erase(y));\r\n\t}\r\n\tll query(ll x) {\r\n\t\tassert(!empty());\r\n\t\tauto l = *lower_bound(x);\r\n\t\treturn l.k*x+l.m + up;\r\n\t}\r\n} H&#x5B;N]; int HH&#x5B;N];\r\n\r\nconst int NN = N * 20;\r\nint n;\r\n\r\nVI adj&#x5B;N]; LL sw&#x5B;N], dp&#x5B;N]; int sz&#x5B;N];\r\n\r\n\r\n#define hu H&#x5B;HH&#x5B;u]]\r\n#define hv H&#x5B;HH&#x5B;v]]\r\n\r\nvoid dfs(int u,int p) {\r\n    sw&#x5B;u] += sw&#x5B;p];\r\n    sz&#x5B;u] = 1;\r\n    LL s = 0; int max_sz = 0, vv = 0;\r\n    for (auto v: adj&#x5B;u]) if (v != p) {\r\n        dfs(v, u);\r\n        s += dp&#x5B;v]; sz&#x5B;u] += sz&#x5B;v];\r\n        if (checkMax(max_sz, sz&#x5B;v])) vv = v;\r\n    }\r\n\r\n    if (max_sz) {\r\n        HH&#x5B;u] = HH&#x5B;vv];\r\n        hu.up += s - dp&#x5B;vv];\r\n        for (auto v: adj&#x5B;u]) if (v != p &amp;&amp; v != vv) {\r\n            hv.up += s - dp&#x5B;v];\r\n            for (auto p: hv) {\r\n                hu.add(p.k, p.m + hv.up);\r\n            }\r\n        }\r\n    } else {\r\n        HH&#x5B;u] = u;\r\n    }\r\n\r\n    hu.add(sw&#x5B;u], s + sqr(sw&#x5B;u]));\r\n    dp&#x5B;u] = sqr(sw&#x5B;p]) + hu.query(-2*sw&#x5B;p]);\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#endif\r\n\r\n    RD(n); REP_1(i, n) RDD(sw&#x5B;i]);\r\n    DO(n-1) {\r\n        int u, v; RD(u, v);\r\n        adj&#x5B;u].PB(v);\r\n        adj&#x5B;v].PB(u);\r\n    }\r\n    dfs(1,0);\r\n    OT(dp&#x5B;1]);\r\n}\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/www.spoj.com\/problems\/COT6\/ https:\/\/github.com\/lychees\/ACM-Training\/tree\/master\/Archive\/Spoj\/COT%20%E7%B3%BB%E5%88%97\/COT6 \u524d\u6392\u819c\u62dc\u4e3b\u5e2d\u3002\u3002\u3002 \u505a\u4e86\u597d\u51e0\u5929\u3002\u3002\u603b\u7b97\u641e\u5b9a\u4e86\u3002\u3002 \u7b97\u6cd5\u4e00\uff1a\u674e\u8d85\u7ebf\u6bb5\u6811 + \u7ebf\u6bb5\u6811\u5408\u5e76 DP \u7ed3\u6784\u548c COT3 \u4e00\u6837\uff0c\u65b9\u7a0b\u548c Cash \u4e00\u6837\uff0c\u90fd\u662f\u52a8\u6001\u7ef4\u62a4\u51f8\u58f3\uff0c\u8be2\u95ee\u4e0e\u67d0\u4e2a\u5411\u91cf\u7684\u70b9\u79ef\u7684\u6700\u5c0f\u503c\u3002\u3002\u3002 \u540e\u6765\u6211\u7cfb\u7edf\u7684\u5b66\u4e60\u4e86\u4e00\u4e0b \u7ebf\u6bb5\u6811\u5408\u5e76\uff0c \u7136\u540e\u53d1\u73b0\u6709\u4e00\u4e2a\u9898\u7684\u505a\u6cd5\u662f\u674e\u8d85\u7ebf\u6bb5\u6811 + \u7ebf\u6bb5\u6811\u5408\u5e76\uff0c\u6765\u641e\u6811\u4e0a dp\u3002\u3002\u3002 \u8fd9\u4e2a\u9898\u7684\u65b9\u7a0b\u548c\u90a3\u4e2a\u9898\u5dee\u4e0d\u591a\u3002\u3002\u3002\u8be2\u95ee\u5411\u91cf\u90fd\u662f (x, 1)\uff0c\u6240\u4ee5\u7528 kx+b \u6765\u641e\u66f4\u65b9\u4fbf\u3002\u3002\u3002 \u53ef\u4ee5\u9884\u5904\u7406\u51fa\u6240\u6709\u7684 sw\uff0c\u6765\u505a\u79bb\u6563\u5316\uff0c\u7136\u540e\u6253\u6807\u8bb0\u53ea\u8981\u6574\u4f53\u5e73\u79fb b\uff0c\u4e0d\u4f1a\u5f71\u54cd\u7ebf\u6bb5\u6811\u7684\u7ed3\u6784\u3002\u3002\u3002 \u7136\u540e\u5c31\u6ca1\u6709\u7136\u540e\u4e86\u3002\u3002\u3002\u590d\u6742\u5ea6 O(nlog2n)\u3002\u3002\u3002 \u7ecf\u8fc7\u8fd9\u4e2a\u9898\u6211\u4eec\u53d1\u73b0\u3002\u3002\u7ebf\u6027\u5206\u6bb5\u51fd\u6570\u6781\u503c \u548c \u52a8\u6001\u51f8\u5305 \u95ee\u9898\u662f\u7b49\u4ef7\u7684\u3002\u3002 \u6839\u636e\u60c5\u51b5\u53ef\u4ee5\u4e92\u76f8\u8f6c\u6362\u3002\u3002\u770b\u600e\u4e48\u5199\u597d\u5199\u3002\u3002\u3002 \u4f3c\u4e4e\u8fd8\u6709\u4e00\u79cd O(nlogn) \u7684\u505a\u6cd5\u3002\u3002\u3002dfs \u5e8f + cdq \u5206\u6cbb\u3002\u3002\u3002\uff08\u5c1d\u8bd5\u4e86\u4e00\u4e0b\u5931\u8d25\u4e86\uff0c\u539f\u56e0\u662f cdq \u5206\u6cbb\u4e24\u8fb9\u7684\u72b6\u6001\u4f1a\u4e92\u76f8\u4f9d\u8d56\u3002\u3002\u3002\uff09 \u665a\u4e0a\u518d\u8bd5\u8bd5\u3002\u3002\u3002 \u6211\u53d1\u73b0\u4e4b\u524d\u7684\u6a21\u677f\u91cc\u6709\u4e00\u4e2a bug \u7684\u5730\u65b9\uff08\u6211\u9760\u5c45\u7136\u8fde COT6 \u7684\u6837\u4f8b\u90fd\u8fc7\u4e0d\u53bb\u3002\u3002\uff09 if (T&#x5B;x].y(ml) &gt; t.y(ml) || T&#x5B;x].y(mr) [&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-1761","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2tdP7-sp","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/1761","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=1761"}],"version-history":[{"count":1,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/1761\/revisions"}],"predecessor-version":[{"id":1767,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/1761\/revisions\/1767"}],"wp:attachment":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/media?parent=1761"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/categories?post=1761"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/tags?post=1761"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}