{"id":827,"date":"2013-09-14T22:15:43","date_gmt":"2013-09-14T14:15:43","guid":{"rendered":"http:\/\/www.shuizilong.com\/house\/?p=827"},"modified":"2013-09-17T05:04:36","modified_gmt":"2013-09-16T21:04:36","slug":"hdu-4729-an-easy-problem-for-elfness","status":"publish","type":"post","link":"https:\/\/www.shuizilong.com\/house\/archives\/hdu-4729-an-easy-problem-for-elfness\/","title":{"rendered":"HDU 4729. An Easy Problem for Elfness"},"content":{"rendered":"<h3>Brief description: <\/h3>\n<p>\u3002\u3002\u3002 n \u4e2a\u7ed3\u70b9\u7684\u5e26\u5bb9\u91cf\u65e0\u5411\u6811\uff0cm \u4e2a\u8be2\u95ee\u3002<br \/>\n\u6bcf\u4e2a\u8be2\u95ee\u5f62\u5982 (s, t, k, a, b)\u3002\u3002\u8868\u793a\u3002\u3002<br \/>\n\u3002\u3002\u5141\u8bb8\u5df2 a \u7684\u4ee3\u4ef7\u4fee\u5efa\u4e00\u6761\u5355\u4f4d\u5bb9\u91cf\u7684\u65b0\u8fb9\uff0cb \u7684\u4ee3\u4ef7\u5c06\u4e00\u6761\u65e7\u8fb9\u6216\u65b0\u8fb9\u589e\u52a0\u5355\u4f4d\u6d41\u91cf\u3002\u3002<br \/>\n\u3002\u3002\u9884\u7b97\u4e3a k \u65f6 s->t \u7684\u6700\u5927\u6d41\u3002\u3002<br \/>\n<!--more--><\/p>\n<h3>Analysis: <\/h3>\n<p>\u3002\u3002\u5148\u8003\u8651\u52a0\u8fb9\u7684\u60c5\u51b5\u3002\u3002\u5982\u679c\u8981\u52a0\u8fb9\u7684\u8bdd\u3002\u3002\u53ea\u4f1a\u52a0\u5728 <code>s->t<\/code> \u4e0a\u3002\u3002\u3002<br \/>\n\u3002\u3002\u5982\u679c<code>a <= b<\/code> \u3002\u3002\u90a3\u4e48\u72c2\u52a0\u8fb9\u5c31\u884c\u4e86\u3002\u3002\u3002\u5426\u5219\u7684\u8bdd\u3002\u3002\u53ea\u4f1a\u6dfb\u52a0\u4e00\u6761\u8fb9\u3002\u3002\u4e14\u6269\u5bb9\u64cd\u4f5c\u5168\u90e8\u7ed9\u8fd9\u6761\u8fb9\u6700\u4f18\u3002<\/p>\n<p>\u3002\u3002\u63a5\u4e0b\u6765\u8003\u8651\u4e0d\u52a0\u8fb9\u7684\u60c5\u51b5\u3002\u3002\u53d6\u51fa <code> s->t <\/code> \u8def\u5f84\u4e0a\u7684\u6240\u6709\u8fb9\u6743\u3002\u3002\u5728\u9884\u7b97\u8303\u56f4\u5185\u5c3d\u53ef\u80fd\u8ba9\u7ea2\u7ebf\u753b\u7684\u66f4\u9ad8\u3002\u3002\u63a8\u66f4\u591a\u7684\u6d41\u3002\u3002\u3002\u3002\u663e\u7136\u8fd9\u662f\u6811\u4e0a\u533a\u95f4 kth \u95ee\u9898\u3002\u3002\u3002\u53ef\u4ee5\u4f7f\u7528\u4e3b\u5e2d\u6811\u3002\u3002\u3002\u3002<\/p>\n<p><a target=\"_blank\" href=\"http:\/\/i1.minus.com\/ibfFsXUIhUcT4h.png\"><img decoding=\"async\" class=\"aligncenter size-full\" title=\"figure\" src=\"http:\/\/i1.minus.com\/ibfFsXUIhUcT4h.png\" alt=\"\"\/><\/a><\/p>\n<p>\u672c\u6765\u4e3b\u5e2d\u6811\u6c42 kth \u5927\u662f\u53ea\u5e26\u4e00\u4e2a logn \u7684\u3002\u3002\u3002\u6211\u6bd4\u8d5b\u7684\u65f6\u5019\u641e\u7740\u641e\u7740\u53c8\u641e\u6210\u4e8c\u5206\u90a3\u6761\u7ea2\u7ebf\u4e86\u3002\u53c8\u628a\u7b2c\u4e8c\u4e2a logn \u52a0\u56de\u6765\u8279\u3002\u3002\u3002<br \/>\n\u3002\u3002\u3002\u3002<\/p>\n<p>\u3002\u3002\u8a00\u5f52\u6b63\u4f20\u3002\u3002\u3002\u5bf9\u4e8e\u6c42 s->t \u7684\u521d\u59cb flow \u7684\u8fc7\u7a0b\u3002\u3002\u5c31\u662f\u6c42\u8fd9\u4e2a\u8def\u5f84\u4e0a rmq\u3002\u3002\u3002<br \/>\n\u3002\u3002\u73b0\u5728\u53cd\u6b63\u6709\u4e86\u4e3b\u5e2d\u6811\u90a3\u4e48\u6c42\u521d\u59cb\u6d41\u53ef\u4ee5\u7528 <code>kth()<\/code> \u3002\u3002\u89e3\u51b3\u3002\uff08\u8fd9\u91cc k \u56fa\u5b9a\u4e3a 1\uff09\u3002\u3002\u3002<br \/>\n\u3002\u3002\u5728\u9884\u7b97\u8303\u56f4\u81f3\u591a\u8fd8\u80fd\u63a8\u591a\u5c11\u6d41\u7684\u51fd\u6570\u6211\u4eec\u8bb0\u4f5c <code>kth2()<\/code> \u3002\u3002\u8fd9\u91cc\u7684 \"k\" \u8868\u793a\u9884\u7b97\u3002\u3002\u5728\u8fd9\u4e2a\u51fd\u6570\u7684\u672b\u5c3e\u3002\u3002\u6c42\u51fa\u6d41\u91cf\u540e\u6211\u4eec\u7acb\u523b\u8fd4\u56de\u6536\u76ca\u3002\u3002<br \/>\n\u3002\u3002\uff08\u6ce8\u610f\u3002\u3002\u4e3b\u5e2d\u6811\u7684\u503c\u57df\u6211\u4eec\u53ea\u5f00\u5230 10000.\u3002 \u6240\u4ee5\u53ef\u80fd\u8fd4\u56de\u6536\u76ca\u7684\u65f6\u523b\u8fd8\u6709\u6ca1\u6709\u82b1\u5b8c\u7684\u9884\u7b97\u3002\u3002\u8fd8\u53ef\u4ee5\u7ee7\u7eed\u52a0\u4e0a\u3002\u3002<\/p>\n<p>\u3002\u3002\u3002\u3002\u9700\u8981\u7ef4\u62a4\u3002\u3002\u3002<br \/>\n\u3002c[]\uff1a\u4e2a\u6570\u3002\u3002\u4ee5\u53ca<br \/>\n\u3002d[]\uff1a\u548c\u3002\u3002<\/p>\n<pre class=\"brush: cpp; light: false; title: ; toolbar: true; notranslate\" title=\"\">\r\nconst int N = 100009, M = 2 * N, LM = 18;\r\n\r\nint hd&#x5B;N], suc&#x5B;M], to&#x5B;M], wt&#x5B;N];\r\nint ST&#x5B;LM]&#x5B;M], st&#x5B;N], dep&#x5B;N]; \/\/ Euler index ...\r\nint n, tt; int T&#x5B;N], Null;\r\n\r\nconst int NN = 20 * N;\r\nint l&#x5B;NN], r&#x5B;NN], c&#x5B;NN], d&#x5B;NN], total;\r\n\/\/ Chairman tree\r\n\r\n#define lx l&#x5B;x]\r\n#define rx r&#x5B;x]\r\n#define ly l&#x5B;y]\r\n#define ry r&#x5B;y]\r\n#define cx c&#x5B;x]\r\n#define cy c&#x5B;y]\r\n\r\n#define ml (ll+rr&gt;&gt;1)\r\n#define mr (ml+1)\r\n#define lc lx, ll, ml\r\n#define rc rx, mr, rr\r\n\r\n#define lt lx = ++total, rx = ry, x = lx, y = ly, rr = ml\r\n#define rt lx = ly, rx = ++total, x = rx, y = ry, ll = mr\r\n\r\nint Tn;\r\n\r\nint new_node(){\r\n    ++total; l&#x5B;total] = r&#x5B;total] = c&#x5B;total] = d&#x5B;total] = 0;\r\n    return total;\r\n}\r\n\r\nint Insert(int y, int p){\r\n\r\n    int x = new_node(), root = x, ll = 0, rr = Tn;\r\n    c&#x5B;x] = c&#x5B;y] + 1, d&#x5B;x] = d&#x5B;y] + p;\r\n\r\n    while (ll &lt; rr){\r\n        if (p &lt; mr) lt; else rt;\r\n        c&#x5B;x] = c&#x5B;y] + 1, d&#x5B;x] = d&#x5B;y] + p;\r\n    }\r\n\r\n    return root;\r\n}\r\n\r\ninline bool elder(int a, int b){\r\n    return dep&#x5B;a] &lt; dep&#x5B;b];\r\n}\r\n\r\ninline int lca(int a, int b){\r\n    int l = st&#x5B;a], r = st&#x5B;b];\r\n    if (l &gt; r) swap(l, r); ++r; int lv = lg2(r-l); \/\/log2(r - l);\r\n    return min(ST&#x5B;lv]&#x5B;l], ST&#x5B;lv]&#x5B;r-(1&lt;&lt;lv)], elder);\r\n}\r\n\r\n#define aa to&#x5B;i^1]\r\n#define bb to&#x5B;i]\r\n#define v bb\r\n#define ww wt&#x5B;i\/2]\r\n\r\nvoid dfs(int u = 1){\r\n    ST&#x5B;0]&#x5B;st&#x5B;u] = ++tt] = u;\r\n    REP_G(i, u) if (!st&#x5B;v]){\r\n        dep&#x5B;v] = dep&#x5B;u] + 1, T&#x5B;v] = Insert(T&#x5B;u], ww);\r\n        dfs(v);\r\n        ST&#x5B;0]&#x5B;++tt] = u;\r\n    }\r\n}\r\n\r\nint kth2(int x, int y, int k){\r\n\r\n    int z = lca(x, y);\r\n    x = T&#x5B;x], y = T&#x5B;y], z = T&#x5B;z];\r\n    int ll = 0, rr = Tn, t, cc = 0, dd = 0;\r\n    int D = c&#x5B;x] + c&#x5B;y] - 2*c&#x5B;z], tc, td;\r\n\r\n    while (ll &lt; rr){\r\n        if (ml * (cc + (tc = c&#x5B;lx] + c&#x5B;ly] - 2*c&#x5B;l&#x5B;z]])) - (dd + (td = d&#x5B;lx] + d&#x5B;ly] - 2*d&#x5B;l&#x5B;z]])) &gt;= k){\r\n            x = l&#x5B;x], y = l&#x5B;y], z = l&#x5B;z];\r\n            rr = ml;\r\n        }\r\n        else {\r\n            x = r&#x5B;x], y = r&#x5B;y], z = r&#x5B;z];\r\n            cc += tc, dd += td, ll = mr;\r\n        }\r\n    }\r\n\r\n    if ((k-((cc*ll)-dd))&lt;0) --ll;\r\n    return ll + (k-((cc*ll)-dd))\/D;\r\n}\r\n\r\nint kth(int x, int y, int k){\r\n\r\n    int z = lca(x, y);\r\n    x = T&#x5B;x], y = T&#x5B;y], z = T&#x5B;z];\r\n    int ll = 0, rr = Tn, t;\r\n\r\n    while (ll &lt; rr){\r\n        if ((t = c&#x5B;l&#x5B;x]] + c&#x5B;l&#x5B;y]] - 2*c&#x5B;l&#x5B;z]]) &gt;= k){\r\n            x = l&#x5B;x], y = l&#x5B;y], z = l&#x5B;z];\r\n            rr = ml;\r\n        }\r\n        else {\r\n            x = r&#x5B;x], y = r&#x5B;y], z = r&#x5B;z];\r\n            k -= t, ll = mr;\r\n        }\r\n    }\r\n\r\n    return ll;\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;out2.txt&quot;, &quot;w&quot;, stdout);\r\n#endif\r\n\r\n    Rush{\r\n\r\n        printf(&quot;Case #%d:\\n&quot;, ++Case);\r\n\r\n        int Q; RD(n, Q); fill(hd+1, hd+n+1, 0); fill(st+1, st+n+1, 0);\r\n        Tn = 0; FOR_C(i, 2, n &lt;&lt; 1){\r\n            RD(to&#x5B;i], to&#x5B;i|1]); checkMax(Tn, RD(ww));\r\n            suc&#x5B;i] = hd&#x5B;aa], hd&#x5B;aa] = i++;\r\n            suc&#x5B;i] = hd&#x5B;aa], hd&#x5B;aa] = i;\r\n        }\r\n\r\n        total = 0, T&#x5B;1] = new_node();\r\n        tt = 0, dfs();\r\n\r\n        for ( int lv = 1 ; _1(lv) &lt;= tt ; lv ++ ){\r\n            for ( int i = 1 ; i + _1(lv)  &lt;= tt + 1 ; i ++ )\r\n                ST&#x5B;lv]&#x5B;i] = min(ST&#x5B;lv-1]&#x5B;i], ST&#x5B;lv-1]&#x5B;i + _1(lv-1)], elder);\r\n        }\r\n\r\n        DO(Q){\r\n            int s, t, k, a, b; RD(s, t, k, a, b);\r\n            int flow = kth(s, t, 1), res = a &lt;= b ? k\/a + flow : max((k&gt;=a?(k-a)\/b+1:0) + flow, kth2(s, t, k\/b));\r\n            printf(&quot;%d\\n&quot;, res);\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<h3>External link: <\/h3>\n<p><a href=\"https:\/\/gist.github.com\/lychees\/6562208\">https:\/\/gist.github.com\/lychees\/6562208<\/a><br \/>\n<a href=\"https:\/\/www.shuizilong.com\/house\/archives\/spoj-10628-count-on-a-tree\/\">https:\/\/www.shuizilong.com\/house\/archives\/spoj-10628-count-on-a-tree\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Brief description: \u3002\u3002\u3002 n \u4e2a\u7ed3\u70b9\u7684\u5e26\u5bb9\u91cf\u65e0\u5411\u6811\uff0cm \u4e2a\u8be2\u95ee\u3002 \u6bcf\u4e2a\u8be2\u95ee\u5f62\u5982 (s, t, k, a, b)\u3002\u3002\u8868\u793a\u3002\u3002 \u3002\u3002\u5141\u8bb8\u5df2 a \u7684\u4ee3\u4ef7\u4fee\u5efa\u4e00\u6761\u5355\u4f4d\u5bb9\u91cf\u7684\u65b0\u8fb9\uff0cb \u7684\u4ee3\u4ef7\u5c06\u4e00\u6761\u65e7\u8fb9\u6216\u65b0\u8fb9\u589e\u52a0\u5355\u4f4d\u6d41\u91cf\u3002\u3002 \u3002\u3002\u9884\u7b97\u4e3a k \u65f6 s->t \u7684\u6700\u5927\u6d41\u3002\u3002<\/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":[47],"tags":[],"class_list":["post-827","post","type-post","status-publish","format-standard","hentry","category-hdu"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2tdP7-dl","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/827","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=827"}],"version-history":[{"count":1,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/827\/revisions"}],"predecessor-version":[{"id":828,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/827\/revisions\/828"}],"wp:attachment":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/media?parent=827"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/categories?post=827"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/tags?post=827"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}