{"id":128,"date":"2023-04-14T22:00:00","date_gmt":"2023-04-14T14:00:00","guid":{"rendered":"https:\/\/solost.cn\/?p=128"},"modified":"2023-04-14T22:00:44","modified_gmt":"2023-04-14T14:00:44","slug":"235","status":"publish","type":"post","link":"https:\/\/solost.cn\/?p=128","title":{"rendered":"ST\u8868"},"content":{"rendered":"<h2>ST\u8868<\/h2>\n<h2>\u539f\u7406<\/h2>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2026\/11\/image-20.png\" alt=\"image.png\" title=\"image.png\"><\/p>\n<h4>\u9996\u5148\u58f0\u660e\u51e0\u70b9\uff1a<\/h4>\n<ol>\n<li><code>st[i][j]<\/code>\u8868\u793a\u8fd9\u6bb5\u533a\u95f4\u4e0a\u4ece\u7b2ci\u4e2a\u4f4d\u7f6e\u81ea\u5df1\u5f00\u59cb\u8d77\uff0c\u5f80\u540e\u6570<code>2^i<\/code>\u4e2a\u6570\u4e4b\u95f4\u7684\u6700\u5927\u503c<\/li>\n<li><code>lg[i]<\/code>\u8868\u793a\u4e0d\u8d85\u8fc7i\u7684\u6700\u59272\u6b21\u5e42\u6307\u6570\uff08\u5c31\u662f2\u7684i\u6b21\u65b9\u4e0d\u8d85\u8fc7i\u4e14i\u5c3d\u91cf\u5927\uff09<\/li>\n<li>\u8f93\u51faRMQ\u6700\u503c\u7684\u65f6\u5019\uff0c\u6700\u597d\u7528\u51fd\u6570\u6765\u5199<\/li>\n<\/ol>\n<h3>\u7b2c\u4e00\u90e8\u5206\uff1a\u8f93\u5165\u5e76\u9884\u5904\u7406lg\u6570\u7ec4<\/h3>\n<p>\u7531\u9884\u5907\u533a1\u53ef\u4ee5\u5f88\u5bb9\u6613\u63a8\u51fa\uff0c\u5bf9\u4e8e\u6211\u4eec\u8f93\u5165\u7684\u6bcf\u4e2a\u6570\uff0c\u5176\u5b9e\u5c31\u662fsti,~(\u56e0\u4e3a2^0=1 \u5f80\u540e\u65701\u8fd8\u662f\u81ea\u5df1 \u8fd9\u5f88\u663e\u7136\u5427)~<\/p>\n<p>\u5bf9\u4e8elg\u6570\u7ec4\u5462\uff0c\u56e0\u4e3a\u5b83\u53ea\u6709\u57282\u7684\u6574\u6570\u6b21\u5e42\u7684\u4f4d\u7f6e\u624d\u4f1a\u66f4\u65b0\u51fa\u503c\uff0c\u8fd9\u4e2a\u4e0d\u592a\u597d\u8bb2\uff0c\u5176\u5b9e\u53ef\u4ee5\u4ecelog[0]=-1\u5f00\u59cb\u5f80\u540e\u63a8\u4e00\u4e0b(\u6ce8\uff1ai&gt;&gt;1\uff0ci\u53f3\u79fb1\uff0c\u5373i\/2\u3002\u5728\u8fd9\u91cc\u7528\u4f4d\u8fd0\u7b97\u4f1a\u5feb\u5f88\u591a\uff0c\u4eb2\u6d4b)\uff0c\u63a8\u523016\uff0c\u751a\u81f38\u5c31\u80fd\u770b\u51fa\u6765\u4e86\u3002\u8fd9\u4e2alg\u5e38\u6570\u4fbf\u662f\u500d\u589e\u4f18\u5316\u4e86\u3002<\/p>\n<p>\u5177\u4f53\u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n<pre><code class=\"lang-cpp\">cin &gt;&gt; n &gt;&gt; m;\nlg[0] = -1;\nfor (int i = 1; i &lt;= n; i++) {\n    cin &gt;&gt; f[i][0];\n    lg[i] = lg[i &gt;&gt; 1] + 1;\n}<\/code><\/pre>\n<h3>\u7b2c\u4e8c\u90e8\u5206\uff1a\u66f4\u65b0\u6700\u503c<\/h3>\n<p>\u5927\u5bb6\u53ef\u4ee5\u624b\u753b\u4e2a\u56fe\uff0c\u6ca1\u9519\u5c31\u753b\u4e00\u6761\u7ebf\u6bb5\u3002\u7528\u4e8c\u5206\u7684\u65b9\u6cd5\u628a\u5b83\u8986\u76d6\u4f4f\u3002\u89c2\u5bdf\u6bcf\u6761\u7ebf\u6bb5\u7684\u5de6\u53f3\u7aef\u70b9\u3002<\/p>\n<p><code>st[i][j]=max(st[i][j-1],st[i+(1&lt;&lt;(j-1))][j-1]);<\/code><\/p>\n<p>\u8bf7\u56de\u60f3\u9884\u5907\u533a1\uff0c\u5219\u8fd9\u4e2a\u8f6c\u79fb\u5f0f\u6240\u8868\u8fbe\u7684\u610f\u4e49\u5c31\u662f\uff1a\u4e0d\u59a8\u5047\u8bbe\u4ece\u7b2ci\u4e2a\u4f4d\u7f6e\u5f00\u59cb\uff0c\u5f80\u540e\u6570<code>2^(j-1)<\/code>\u7684\u533a\u95f4\u5185\u7684\u6700\u503c\u4e3am1\uff0c\u5219sti\u8868\u793a\u7684\u5c31\u662fm1\uff1b\u90a3\u7b2c\u4e8c\u6bb5\u533a\u95f4\u7684\u8d77\u70b9\u5373\u4e3a<code>2^(j-1)+1<\/code>\uff08\u6ce8\u610f\uff01\u8981+1\uff01\uff09\u4f9d\u65e7\u5f80\u540e\u6570<code>2^(j-1)<\/code>\u4e2a\u6570\uff0c\u8be5\u533a\u95f4\u5185\u6700\u503c\u4e3am2\uff0c\u5219<code>st[i][j]<\/code>\u7684\u503c\u4e3a<code>max(m1,m2)<\/code>\u3002<\/p>\n<p>\u5177\u4f53\u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n<pre><code class=\"lang-cpp\">for (int j = 1; j &lt;= lg[n]; j++) {\n    for (int i = 1; i + (1 &lt;&lt; j) - 1 &lt;= n; i++) {\n        f[i][j] = max(f[i][j - 1], f[i + (1 &lt;&lt; (j - 1))][j - 1]);\n    }\n}<\/code><\/pre>\n<p>\u53c8\u6709\u4eba\u4f1a\u95ee\u4e86\uff0c\u4e3a\u4ec0\u4e48j\u8981\u5728\u5916\u5c42\uff1f<\/p>\n<p>\u89c2\u5bdf<code>st[i][j]<\/code>\u662f\u600e\u4e48\u5f97\u6765\u7684\uff0c\u8054\u60f3\u4e00\u4e0b\u5b83\u7684\u5177\u4f53\u542b\u4e49\u3002<\/p>\n<p>\u8fd9\u662f\u56e0\u4e3a\u6211\u4eec\u5728for\u5faa\u73af\u751f\u6210st\u8868\u65f6\uff0c\u662f\u6839\u636e\u533a\u95f4\u957f\u5ea6\u7531\u5c0f\u5230\u5927\u9010\u4e2a\u751f\u6210\u7684\uff0c\u5bf9\u4e8e\u6bcf\u4e2a\u8d77\u59cb\u4f4d\u7f6e\u90fd\u8981\u6c42\u51fa\u80fd\u8986\u76d6\u6574\u6761\u7ebf\u6bb5\u7684\u4e8c\u5206\u65b9\u6cd5\u4e2d\u7684\u6700\u5c0f\u503c\u3002\u53ea\u6709\u524d\u4e00\u4e2a\u4f4d\u7f6e\u7684\u503c\u5168\u90e8\u751f\u6210\u5b8c\u540e\uff0c\u624d\u80fd\u53bb\u5904\u7406\u4e0b\u4e00\u4e2a\u70b9\u3002<\/p>\n<p>\u73b0\u5728\u518d\u56de\u60f3\u4e00\u4e0b\u521a\u624d\u7684\u8fc7\u7a0b\uff0c\u6211\u4eec\u786e\u5b9e\u80fd\u5b9e\u73b0log\u7ea7\u522b\u7684\u9884\u5904\u7406\u4e86\u3002<\/p>\n<h3>\u7b2c\u4e09\u90e8\u5206\uff1a\u67e5\u8be2\uff08query\uff09<\/h3>\n<p>\u67e5\u8be2\u65f6\u53c8\u4f1a\u51fa\u73b0\u5f88\u591a\u95ee\u9898\uff1a\u82e5\u662f\u4ecei^(j-1)+1\u7684\u4f4d\u7f6e\u5f80\u540e\u53d6\u533a\u95f4\u6700\u5c0f\u503c\uff0c\u5f88\u5bb9\u6613\u53d6\u4e0d\u5168\u6216\u8005\u662f\u591a\u53d6\u3002\u6bd4\u5982\u8bf4\u67e5\u8be2\uff081\uff0c7\uff09\uff0c\u8fd4\u56de\u503c\u5e94\u8be5\u662fRMQ(1,4)\u3002\u95ee\u9898\u6765\u4e86\uff0c\u7b2c\u4e8c\u4e2a\u533a\u95f4\u7684\u957f\u5ea6\u662f3\uff0cst\u6570\u7ec4\u7684j\u7a76\u7adf\u8be5\u53d6\u591a\u5c11\uff1f1\uff1f2^1=2\uff0c\u53d6\u4e0d\u5168\uff1b2\uff1f2^2=4\uff0c\u4f1a\u5f80\u540e\u591a\u53d6\u4e86\u4e00\u4e2a\u6570\uff0c\u8fd9\u65f6\u7b54\u6848\u5c31\u4e0d\u5bf9\u4e86\u3002\u600e\u4e48\u89e3\u51b3\u5462\uff1f<\/p>\n<p>\u6211\u4eec\u60f3\u4e00\u4e0b\u521a\u521a\u7684\u95ee\u9898\u6839\u672c\u6240\u5728\uff0c\u662f\u65e0\u6cd5\u786e\u5b9a\u540e\u534a\u533a\u95f4\u5982\u4f55\u8986\u76d6\u3002\u90a3\u4e48\u6211\u4eec~\u7b80\u5355\u7c97\u66b4\u4e00\u70b9~\u5e72\u8106\u4ece\u540e\u5f80\u524d\u53d6\uff0c\u8bbe\u53f3\u7aef\u70b9\u4e3ar\uff0c\u90a3st\u6570\u7ec4\u7684\u7b2c\u4e00\u7ef4\u53ef\u4ee5\u5199\u6210r-(2^z)+1\uff0c\u4f9d\u65e7\u8981+1\uff0cz\u5177\u4f53\u4ee3\u8868\u4ec0\u4e48\u540e\u9762\u63ed\u6653\u3002\uff08\u5176\u5b9e\u5f88\u591a\u4eba\u4e5f\u80fd\u770b\u51fa\u6765\u4e86\u5427\uff09<\/p>\n<p>\u4ece\u540e\u5f80\u524d\u53d6\u91cd\u590d\u4e86\u600e\u4e48\u529e\uff1f\u8fd9\u6ca1\u95ee\u9898\uff0c\u53d6\u7684\u662f\u6700\u503c\uff0c\u91cd\u590d\u533a\u95f4\u5e76\u4e0d\u5f71\u54cd\u6700\u503c\u3002<\/p>\n<p>\u5177\u4f53\u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n<pre><code class=\"lang-cpp\">int query(int l, int r)\n{\n    int t = lg[r - l + 1];\n    return max(f[l][t], f[r - (1 &lt;&lt; t) + 1][t]);\n}<\/code><\/pre>\n<p>t\u4ee3\u8868\u7684\u662f\u5c0f\u4e8e\u7b49\u4e8e<code>i + (1 &lt;&lt; (j - 1))\u6700\u53f3\u8fb9\u7684\u6570<\/code>\u4e5f\u5c31\u662f\u6700\u77ed\u548c\u524d\u9762\u4e00\u534a\u7684\u533a\u95f4\u76f8\u4ea4\u7684\u5730\u65b9\u3002<\/p>\n<h2>\u4f8b\u9898<\/h2>\n<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2026\/11\/image1-6.png\" alt=\"image(1).png\" title=\"image(1).png\"><\/p>\n<h2>\u9898\u76ee\u94fe\u63a5<\/h2>\n<p><a href=\"https:\/\/www.luogu.com.cn\/problem\/P3865\">\u94fe\u63a5<\/a><\/p>\n<h2>\u9898\u89e3<\/h2>\n<pre><code class=\"lang-cpp\">#include &lt;algorithm&gt;\n#include &lt;bitset&gt;\n#include &lt;cmath&gt;\n#include &lt;cstdio&gt;\n#include &lt;cstdlib&gt;\n#include &lt;cstring&gt;\n#include &lt;fstream&gt;\n#include &lt;iostream&gt;\n#include &lt;map&gt;\n#include &lt;queue&gt;\n#include &lt;set&gt;\n#include &lt;stack&gt;\n#include &lt;unordered_map&gt;\n#include &lt;unordered_set&gt;\n#include &lt;vector&gt;\n\nusing namespace std;\n#define x first\n#define y second\n#define endl &#039;\\n&#039;\n#define IOS                       \\\n    ios_base::sync_with_stdio(0); \\\n    cin.tie(0);                   \\\n    cout.tie(0);\ntypedef long long ll;\ntypedef pair&lt;ll, ll&gt; pll;\ntypedef pair&lt;int, int&gt; pii;\ntypedef unsigned long long ull;\n\nconst int INF = 0x3f3f3f3f, mod = 1000000007;\nconst int N = 2e6 + 10;\nint n, m;\nint f[N][22];\nint lg[N];\n\nint query(int l, int r)\n{\n    int t = lg[r - l + 1];\n    return max(f[l][t], f[r - (1 &lt;&lt; t) + 1][t]);\/\/\u6ce8\u610f\u5728\u8fd9\u662f1&lt;&lt;t\uff0c\u4e0d\u662ft\uff0c\u51cf\u5c11\u7684\u662f\u957f\u5ea6\n}\n\nvoid solve()\n{\n    cin &gt;&gt; n &gt;&gt; m;\n    lg[0] = -1;\n    for (int i = 1; i &lt;= n; i++) {\n        cin &gt;&gt; f[i][0];\n        lg[i] = lg[i &gt;&gt; 1] + 1;\n    }\n    for (int j = 1; j &lt;= lg[n]; j++) {\n        for (int i = 1; i + (1 &lt;&lt; j) - 1 &lt;= n; i++) {\n            f[i][j] = max(f[i][j - 1], f[i + (1 &lt;&lt; (j - 1))][j - 1]);\n        }\n    }\n    while (m--) {\n        int x, y;\n        cin &gt;&gt; x &gt;&gt; y;\n        cout &lt;&lt; query(x, y) &lt;&lt; endl;\n    }\n}\nint main()\n{\n    IOS;\n    int t = 1;\n    \/\/ cin &gt;&gt; t;\n    while (t--) {\n        solve();\n    }\n    return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>ST\u8868 \u539f\u7406 \u9996\u5148\u58f0\u660e\u51e0\u70b9\uff1a st[i][j]\u8868\u793a\u8fd9\u6bb5\u533a\u95f4\u4e0a\u4ece\u7b2ci\u4e2a\u4f4d\u7f6e\u81ea\u5df1\u5f00\u59cb\u8d77\uff0c\u5f80\u540e\u65702^i\u4e2a\u6570\u4e4b\u95f4\u7684\u6700\u5927\u503c lg[i]\u8868\u793a\u4e0d\u8d85\u8fc7i\u7684\u6700\u59272\u6b21\u5e42\u6307\u6570\uff08\u5c31\u662f2\u7684i\u6b21\u65b9\u4e0d\u8d85\u8fc7i\u4e14i\u5c3d\u91cf\u5927\uff09 \u8f93\u51faRMQ\u6700\u503c\u7684\u65f6\u5019\uff0c\u6700\u597d\u7528\u51fd\u6570\u6765\u5199 \u7b2c\u4e00\u90e8\u5206\uff1a\u8f93\u5165\u5e76\u9884\u5904\u7406lg\u6570\u7ec4 \u7531\u9884\u5907\u533a1\u53ef\u4ee5\u5f88\u5bb9\u6613\u63a8\u51fa\uff0c\u5bf9\u4e8e\u6211\u4eec\u8f93\u5165\u7684\u6bcf\u4e2a\u6570\uff0c\u5176\u5b9e\u5c31\u662fsti,~(\u56e0\u4e3a2^0=1 \u5f80\u540e\u65701\u8fd8\u662f\u81ea\u5df1 \u8fd9\u5f88\u663e\u7136\u5427)~ \u5bf9\u4e8elg\u6570\u7ec4\u5462\uff0c\u56e0\u4e3a\u5b83\u53ea\u6709\u57282\u7684\u6574\u6570\u6b21\u5e42\u7684\u4f4d\u7f6e\u624d\u4f1a\u66f4\u65b0\u51fa\u503c\uff0c\u8fd9\u4e2a\u4e0d\u592a\u597d\u8bb2\uff0c\u5176\u5b9e\u53ef\u4ee5\u4ecelog[0]=-1\u5f00\u59cb\u5f80\u540e\u63a8\u4e00\u4e0b(\u6ce8\uff1ai&gt;&gt;1\uff0ci\u53f3\u79fb1\uff0c\u5373i\/2\u3002\u5728\u8fd9\u91cc\u7528\u4f4d\u8fd0\u7b97\u4f1a\u5feb\u5f88\u591a\uff0c\u4eb2\u6d4b)\uff0c\u63a8\u523016\uff0c\u751a\u81f38\u5c31\u80fd\u770b\u51fa\u6765\u4e86\u3002\u8fd9\u4e2alg\u5e38\u6570\u4fbf\u662f\u500d\u589e\u4f18\u5316\u4e86\u3002 \u5177\u4f53\u4ee3\u7801\u5b9e\u73b0\uff1a cin &gt;&gt; n &gt;&gt; m; lg[0] = -1; for (int i = 1; i &lt;= n; i++) { cin &gt;&gt; f[i][0]; lg[i] = lg[i &gt;&gt; 1] + 1; } \u7b2c\u4e8c\u90e8\u5206\uff1a\u66f4\u65b0\u6700\u503c \u5927\u5bb6\u53ef\u4ee5\u624b\u753b\u4e2a\u56fe\uff0c\u6ca1\u9519\u5c31\u753b\u4e00\u6761\u7ebf\u6bb5\u3002\u7528\u4e8c\u5206\u7684\u65b9\u6cd5\u628a\u5b83\u8986\u76d6\u4f4f\u3002\u89c2\u5bdf\u6bcf\u6761\u7ebf\u6bb5\u7684\u5de6\u53f3\u7aef\u70b9\u3002 st[i][j]=max(st[i][j-1],st[i+(1&lt;&lt;(j-1))][j-1]); \u8bf7\u56de\u60f3\u9884\u5907\u533a1\uff0c\u5219\u8fd9\u4e2a\u8f6c\u79fb\u5f0f\u6240\u8868\u8fbe\u7684\u610f\u4e49\u5c31\u662f\uff1a\u4e0d\u59a8\u5047\u8bbe\u4ece\u7b2ci\u4e2a\u4f4d\u7f6e\u5f00\u59cb\uff0c\u5f80\u540e\u65702^(j-1)\u7684\u533a\u95f4\u5185\u7684\u6700\u503c\u4e3am1\uff0c\u5219sti\u8868\u793a\u7684\u5c31\u662fm1\uff1b\u90a3\u7b2c\u4e8c\u6bb5\u533a\u95f4\u7684\u8d77\u70b9\u5373\u4e3a2^(j-1)+1\uff08\u6ce8\u610f\uff01\u8981+1\uff01\uff09\u4f9d\u65e7\u5f80\u540e\u65702^(j-1)\u4e2a\u6570\uff0c\u8be5\u533a\u95f4\u5185\u6700\u503c\u4e3am2\uff0c\u5219st[i][j]\u7684\u503c\u4e3amax(m1,m2)\u3002 \u5177\u4f53\u4ee3\u7801\u5b9e\u73b0\uff1a for (int j = 1; j &lt;= lg[n]; j++) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[72],"tags":[50],"class_list":["post-128","post","type-post","status-publish","format-standard","hentry","category-acm","tag-st"],"_links":{"self":[{"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts\/128","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=128"}],"version-history":[{"count":0,"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts\/128\/revisions"}],"wp:attachment":[{"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}