{"id":955,"date":"2014-09-03T00:51:35","date_gmt":"2014-09-02T16:51:35","guid":{"rendered":"http:\/\/www.shuizilong.com\/house\/?p=955"},"modified":"2014-09-03T01:23:42","modified_gmt":"2014-09-02T17:23:42","slug":"rmq","status":"publish","type":"post","link":"https:\/\/www.shuizilong.com\/house\/archives\/rmq\/","title":{"rendered":"RMQ"},"content":{"rendered":"<p><!--more--><\/p>\n<p><a href=\"https:\/\/13331.org\/26.html\">https:\/\/13331.org\/26.html<\/a><br \/>\n<a href=\"http:\/\/en.wikipedia.org\/wiki\/Range_minimum_query\">http:\/\/en.wikipedia.org\/wiki\/Range_minimum_query<\/a><br \/>\n<a href=\"http:\/\/www.cnblogs.com\/kuangbin\/p\/3227420.html\">http:\/\/www.cnblogs.com\/kuangbin\/p\/3227420.html<\/a><\/p>\n<h2>BIT<\/h2>\n<p>\u6811\u72b6\u6570\u7ec4\u89e3\u51b3 RMQ\uff0c\u7531\u4e8e RMQ \u95ee\u9898\u65e0\u6cd5\u533a\u95f4\u76f8\u51cf\uff0c\u56e0\u6b64\u53ea\u80fd\u8be2\u95ee\u524d\u540e\u7f00\u4ee5\u53ca\u5355\u8c03\u7684\u4fee\u6539\u3002<\/p>\n<p><a href=\"http:\/\/codeforces.com\/gym\/100460\/problem\/K\">http:\/\/codeforces.com\/gym\/100460\/problem\/K<\/a><\/p>\n<pre class=\"brush: cpp; collapse: true; light: false; title: ; toolbar: true; notranslate\" title=\"\">\r\n\/\/}\/* .................................................................................................................................. *\/\r\n\r\nconst int N = int(1e5) + 9;\r\n\r\nVII A, B;\r\nint a&#x5B;N],b&#x5B;N];\r\nint n;\r\n\r\nnamespace BIT{\r\n    int C1&#x5B;N], C2&#x5B;N];\r\n    void Add(int C&#x5B;], int x, int d){\r\n        for (;x&lt;=n;x+=low_bit(x)) checkMin(C&#x5B;x], d);\r\n    }\r\n    int Sum(int C&#x5B;], int x){\r\n        int res = INF; for (;x;x^=low_bit(x)) checkMin(res, C&#x5B;x]);\r\n        return res;\r\n    }\r\n\r\n} using namespace BIT;\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;out.txt&quot;, &quot;w&quot;, stdout);\r\n#endif\r\n\r\n    FLC(C1, C2, 0x3f);\r\n\r\n    int m; RD(n); REP(i, n) A.PB(MP(RD(), i));\r\n    REP(i, n) B.PB(MP(RD(), i));\r\n    SRT(A); SRT(B);\r\n    A.PB(MP(INF, INF)); B.PB(MP(INF, INF));\r\n\r\n    DWN(i, n, 0){\r\n        Add(C1, n-i, A&#x5B;i].se);\r\n        Add(C2, n-i, B&#x5B;i].se);\r\n    }\r\n\r\n    RD(m);\r\n    REP(i, m) RD(a&#x5B;i]); REP(i, m) RD(b&#x5B;i]);\r\n\r\n    \/\/ 1..n\r\n\r\n    REP(i, m){\r\n        int aa = Sum(C1, n-UBD(A, MP(a&#x5B;i], INF))  );\r\n        int bb = Sum(C2, n-UBD(B, MP(b&#x5B;i], INF))  );\r\n\/\/\r\n  \/\/      cout &lt;&lt; aa &lt;&lt; &quot; &quot; &lt;&lt; bb &lt;&lt; endl;\r\n\r\n        if (aa == bb) puts(&quot;Draw&quot;);\r\n        else puts( aa &lt; bb ? &quot;Mike&quot; : &quot;Constantine&quot;);\r\n    }\r\n}\r\n<\/pre>\n<h2>Sparse Table<\/h2>\n<p>\u53e6\u4e00\u79cd\u533a\u95f4\u5206\u5272\u65b9\u6848\uff0c\u7f3a\u70b9\u662f\u4e0d\u80fd\u4fee\u6539\u3002<br \/>\n\u5728 lca \u95ee\u9898\u548c\u540e\u7f00\u6570\u7ec4\u6c42 lcp \u95ee\u9898\u4e2d\u90fd\u7ecf\u5e38\u4f7f\u7528\u3002<\/p>\n<pre class=\"brush: cpp; light: false; title: ; toolbar: true; notranslate\" title=\"\">\r\nint ST&#x5B;LV]&#x5B;N];\r\n\r\nint rmq(int l, int r){\r\n    if (l &gt; r) swap(l, r); ++r; int lv = lg2(r-l);\r\n    return min(ST&#x5B;0]&#x5B;lv]&#x5B;l], ST&#x5B;0]&#x5B;lv]&#x5B;r-(1&lt;&lt;lv)]);\r\n}\r\n\r\nfor ( int lv = 1 ; (1 &lt;&lt; lv) &lt;= n ; lv ++ ){\r\n    for ( int i = 1 ; i + (1 &lt;&lt; lv)  &lt;= n + 1 ; i ++ ){\r\n        ST&#x5B;lv]&#x5B;i] = min(ST&#x5B;lv-1]&#x5B;i], ST&#x5B;lv-1]&#x5B;i + (1&lt;&lt;(lv-1))]);                \r\n    }\r\n}\r\n<\/pre>\n<p>HDU 2888 &#8211; Check Corners \uff08\u77e9\u5f62\u3002\u3002<br \/>\n<a href=\"http:\/\/acm.hust.edu.cn\/vjudge\/problem\/viewSource.action?id=1778847\">http:\/\/acm.hust.edu.cn\/vjudge\/problem\/viewSource.action?id=1778847<\/a><\/p>\n<pre class=\"brush: cpp; light: false; title: ; toolbar: true; notranslate\" title=\"\">\r\n        REP(xx, lg2(n)+1) REP(yy, lg2(m)+1) if (xx+yy){\r\n            REP(x, n-_1(xx)+1) REP(y, m-_1(yy)+1){\r\n                if (xx){\r\n                    ST&#x5B;xx]&#x5B;yy]&#x5B;x]&#x5B;y] = max(ST&#x5B;xx-1]&#x5B;yy]&#x5B;x]&#x5B;y], ST&#x5B;xx-1]&#x5B;yy]&#x5B;x+_1(xx-1)]&#x5B;y]);\r\n                }\r\n                else{\r\n                    ST&#x5B;xx]&#x5B;yy]&#x5B;x]&#x5B;y] = max(ST&#x5B;xx]&#x5B;yy-1]&#x5B;x]&#x5B;y], ST&#x5B;xx]&#x5B;yy-1]&#x5B;x]&#x5B;y+_1(yy-1)]);\r\n                }\r\n            }\r\n        }\r\n<\/pre>\n<p>POJ &#8211; 2019 Cornfields  \uff08\u6b63\u65b9\u5f62\u3002\u3002\u65f6\u7a7a\u4e0a\u964d\u4e00\u4e2a log\u3002\u3002<br \/>\n<a href=\"http:\/\/acm.hust.edu.cn\/vjudge\/problem\/viewSource.action?id=1778846\">http:\/\/acm.hust.edu.cn\/vjudge\/problem\/viewSource.action?id=1778846<\/a><\/p>\n<p>ZOJ &#8211; 3614 Choir \uff08\u7ef4\u62a4\u65b9\u5dee\u3002\u3002<br \/>\n<a href=\"http:\/\/acm.hust.edu.cn\/vjudge\/problem\/viewSource.action?id=1778842\">http:\/\/acm.hust.edu.cn\/vjudge\/problem\/viewSource.action?id=1778842<\/a><\/p>\n<h5>\u7ebf\u6bb5\u6811<\/h5>\n<p>\u6700\u5927\u7684\u597d\u5904\u662f\u652f\u6301\u4fee\u6539\u3002<br \/>\n<a href=\"https:\/\/www.shuizilong.com\/house\/archives\/spoj-3557-ioi04-hermes\/\">https:\/\/www.shuizilong.com\/house\/archives\/spoj-3557-ioi04-hermes\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"","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":[34],"tags":[],"class_list":["post-955","post","type-post","status-publish","format-standard","hentry","category-34"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/s2tdP7-rmq","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/955","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=955"}],"version-history":[{"count":1,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/955\/revisions"}],"predecessor-version":[{"id":956,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/955\/revisions\/956"}],"wp:attachment":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/media?parent=955"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/categories?post=955"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/tags?post=955"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}