{"id":68,"date":"2026-04-06T14:35:03","date_gmt":"2026-04-06T06:35:03","guid":{"rendered":"https:\/\/www.sjll.top\/index.php\/2026\/04\/06\/p1637-%e4%b8%89%e5%85%83%e4%b8%8a%e5%8d%87%e5%ad%90%e5%ba%8f%e5%88%97-%e6%a0%91%e7%8a%b6%e6%95%b0%e7%bb%84%e8%a7%a3%e6%b3%95%e8%af%a6%e8%a7%a3\/"},"modified":"2026-04-06T14:35:03","modified_gmt":"2026-04-06T06:35:03","slug":"p1637-%e4%b8%89%e5%85%83%e4%b8%8a%e5%8d%87%e5%ad%90%e5%ba%8f%e5%88%97-%e6%a0%91%e7%8a%b6%e6%95%b0%e7%bb%84%e8%a7%a3%e6%b3%95%e8%af%a6%e8%a7%a3","status":"publish","type":"post","link":"https:\/\/www.sjll.top\/?p=68","title":{"rendered":"P1637 \u4e09\u5143\u4e0a\u5347\u5b50\u5e8f\u5217 | \u6811\u72b6\u6570\u7ec4\u89e3\u6cd5\u8be6\u89e3"},"content":{"rendered":"<h2>\u9898\u76ee\u63cf\u8ff0<\/h2>\n<p>\u5728\u542b\u6709 n \u4e2a\u6574\u6570\u7684\u5e8f\u5217 a1,a2,\u2026,an \u4e2d\uff0c\u4e09\u4e2a\u6570\u88ab\u79f0\u4f5c <strong>thair<\/strong> \u5f53\u4e14\u4ec5\u5f53 i&lt;j&lt;k \u4e14 a[i]&lt;a[j]&lt;a[k]\u3002\u6c42\u4e00\u4e2a\u5e8f\u5217\u4e2d thair \u7684\u4e2a\u6570\u3002<\/p>\n<h2>\u4ec0\u4e48\u662f\u4e09\u5143\u4e0a\u5347\u5b50\u5e8f\u5217\uff1f<\/h2>\n<p>\u4e09\u5143\u4e0a\u5347\u5b50\u5e8f\u5217\u662f\u6307\u5728\u6570\u7ec4\u4e2d\u9009\u53d6\u4e09\u4e2a\u4f4d\u7f6e i\u3001j\u3001k\uff08\u6ee1\u8db3 i &lt; j &lt; k\uff09\uff0c\u540c\u65f6\u6ee1\u8db3 a[i] &lt; a[j] &lt; a[k] \u7684\u4e09\u4e2a\u6570\u3002\u8fd9\u662f\u4e00\u4e2a\u7ecf\u5178\u7684\u8ba1\u6570\u95ee\u9898\uff0c\u5c5e\u4e8e\u300c\u4e0a\u5347\u5b50\u5e8f\u5217\u300d\u7cfb\u5217\u9898\u578b\u7684\u57fa\u7840\u3002<\/p>\n<h2>\u89e3\u9898\u6838\u5fc3\u601d\u60f3<\/h2>\n<p>\u5bf9\u4e8e\u6bcf\u4e2a\u4f4d\u7f6e j\uff08\u4f5c\u4e3a\u4e2d\u95f4\u5143\u7d20\uff09\uff0c\u6211\u4eec\u9700\u8981\uff1a<\/p>\n<ol>\n<li>\u7edf\u8ba1\u5b83\u5de6\u8fb9\u6bd4 a[j] \u5c0f\u7684\u5143\u7d20\u4e2a\u6570\uff0c\u8bb0\u4e3a <strong>L[j]<\/strong><\/li>\n<li>\u7edf\u8ba1\u5b83\u53f3\u8fb9\u6bd4 a[j] \u5927\u7684\u5143\u7d20\u4e2a\u6570\uff0c\u8bb0\u4e3a <strong>R[j]<\/strong><\/li>\n<li>\u4ee5 j \u4e3a\u4e2d\u95f4\u5143\u7d20\u7684\u4e09\u5143\u7ec4\u6570\u91cf\u5c31\u662f <strong>L[j] \u00d7 R[j]<\/strong><\/li>\n<\/ol>\n<h2>\u4e3a\u4ec0\u4e48\u8981\u79bb\u6563\u5316\uff1f<\/h2>\n<p>\u7531\u4e8e\u6570\u7ec4\u5143\u7d20\u53ef\u80fd\u5f88\u5927\uff081 \u2264 a[i] \u2264 10^5\uff09\uff0c\u4f46 n \u53ea\u6709 30000\uff0c\u6211\u4eec\u9700\u8981\u5c06\u5927\u8303\u56f4\u7684\u6570\u503c\u6620\u5c04\u5230\u7d27\u51d1\u7684\u7d22\u5f15\u8303\u56f4\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u53eb\u505a<strong>\u79bb\u6563\u5316<\/strong>\u3002<\/p>\n<p>\u6b65\u9aa4\uff1a<\/p>\n<ol>\n<li>\u590d\u5236\u539f\u6570\u7ec4\u5230 b[ ]<\/li>\n<li>\u5bf9 b \u6392\u5e8f\u5e76\u53bb\u91cd<\/li>\n<li>\u901a\u8fc7\u4e8c\u5206\u67e5\u627e\u5c06\u539f\u6570\u7ec4\u5143\u7d20\u6620\u5c04\u5230 1~m \u7684\u7d22\u5f15<\/li>\n<\/ol>\n<h2>\u6811\u72b6\u6570\u7ec4\uff08Fenwick Tree\uff09<\/h2>\n<p>\u6811\u72b6\u6570\u7ec4\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784\uff0c\u7528\u4e8e<strong>\u5355\u70b9\u66f4\u65b0+\u533a\u95f4\u67e5\u8be2<\/strong>\uff0c\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(log n)\u3002<\/p>\n<p>\u6838\u5fc3\u64cd\u4f5c\uff1a<\/p>\n<ul>\n<li><strong>add(x, c)<\/strong>\uff1a\u5728\u4f4d\u7f6e x \u589e\u52a0 c<\/li>\n<li><strong>query(x)<\/strong>\uff1a\u67e5\u8be2\u524d\u7f00\u548c [1, x]<\/li>\n<\/ul>\n<p>\u8fd9\u4e24\u4e2a\u64cd\u4f5c\u5b8c\u7f8e\u6ee1\u8db3\u6211\u4eec\u7684\u9700\u6c42\uff1a\u7edf\u8ba1\u5de6\u8fb9\u6bd4\u5f53\u524d\u5143\u7d20\u5c0f\u7684\u4e2a\u6570\uff0c\u4ee5\u53ca\u53f3\u8fb9\u6bd4\u5f53\u524d\u5143\u7d20\u5927\u7684\u4e2a\u6570\u3002<\/p>\n<h2>\u7b97\u6cd5\u6b65\u9aa4<\/h2>\n<ol>\n<li><strong>\u79bb\u6563\u5316<\/strong>\uff1a\u5c06\u539f\u6570\u7ec4\u5143\u7d20\u6620\u5c04\u5230 1~m \u7684\u7d22\u5f15<\/li>\n<li><strong>\u4ece\u5de6\u5230\u53f3\u626b\u63cf<\/strong>\uff1a\u7528\u6811\u72b6\u6570\u7ec4\u7edf\u8ba1\u6bcf\u4e2a\u4f4d\u7f6e\u5de6\u8fb9\u6bd4\u5b83\u5c0f\u7684\u5143\u7d20\u4e2a\u6570 L[i]<\/li>\n<li><strong>\u4ece\u53f3\u5230\u5de6\u626b\u63cf<\/strong>\uff1a\u7528\u6811\u72b6\u6570\u7ec4\u7edf\u8ba1\u6bcf\u4e2a\u4f4d\u7f6e\u53f3\u8fb9\u6bd4\u5b83\u5927\u7684\u5143\u7d20\u4e2a\u6570 R[i]<\/li>\n<li><strong>\u7d2f\u52a0\u7ed3\u679c<\/strong>\uff1aans = \u03a3(L[i] \u00d7 R[i])<\/li>\n<\/ol>\n<h2>\u5b8c\u6574\u4ee3\u7801<\/h2>\n<pre><code class=\"language-cpp\">#include&lt;iostream&gt;\n#include&lt;cstdio&gt;\n#include&lt;algorithm&gt;\nusing namespace std;\n\nconst int N=30010;\ntypedef long long LL;\n\nint n;\nint a[N];    \/\/ \u539f\u5e8f\u5217\nint b[N];    \/\/ \u79bb\u6563\u5316\u6570\u7ec4\nint tr[N];   \/\/ \u6811\u72b6\u6570\u7ec4\nint L[N], R[N];  \/\/ \u5de6\u5c0f\u3001\u53f3\u5927\u7684\u4e2a\u6570\n\nint lowbit(int x){\n    return x & -x;\n}\n\nvoid add(int x, int c){\n    for(; x&lt;=n; x+=lowbit(x))\n        tr[x] += c;\n}\n\nint query(int x){\n    int res = 0;\n    for(; x&gt;0; x-=lowbit(x))\n        res += tr[x];\n    return res;\n}\n\nint main(){\n    scanf(\"%d\", &n);\n    for(int i=1; i&lt;=n; i++){\n        scanf(\"%d\", &a[i]);\n        b[i] = a[i];\n    }\n    \n    \/\/ \u79bb\u6563\u5316\n    sort(b+1, b+1+n);\n    int m = 1;\n    for(int i=2; i&lt;=n; i++)\n        if(b[i] != b[m]) b[++m] = b[i];\n    \n    \/\/ \u7b2c\u4e00\u904d\u626b\u63cf\uff1a\u7edf\u8ba1\u5de6\u8fb9\u6bd4\u5b83\u5c0f\u7684\u4e2a\u6570\n    for(int i=1; i&lt;=n; i++){\n        int k = lower_bound(b+1, b+1+m, a[i]) - b;\n        L[i] = query(k-1);\n        add(k, 1);\n    }\n    \n    \/\/ \u6e05\u7a7a\u6811\u72b6\u6570\u7ec4\n    for(int i=1; i&lt;=n; i++) tr[i] = 0;\n    \n    \/\/ \u7b2c\u4e8c\u904d\u626b\u63cf\uff1a\u7edf\u8ba1\u53f3\u8fb9\u6bd4\u5b83\u5927\u7684\u4e2a\u6570\n    for(int i=n; i&gt;=1; i--){\n        int k = lower_bound(b+1, b+1+m, a[i]) - b;\n        R[i] = query(m) - query(k);\n        add(k, 1);\n    }\n    \n    \/\/ \u8ba1\u7b97\u7ed3\u679c\n    LL ans = 0;\n    for(int i=1; i&lt;=n; i++)\n        ans += (LL)L[i] * R[i];\n    \n    printf(\"%lld\\n\", ans);\n    return 0;\n}\n<\/code><\/pre>\n<h2>\u590d\u6742\u5ea6\u5206\u6790<\/h2>\n<ul>\n<li><strong>\u65f6\u95f4\u590d\u6742\u5ea6<\/strong>\uff1aO(n log n)\uff0c\u4e3b\u8981\u6765\u81ea\u6811\u72b6\u6570\u7ec4\u7684\u4e24\u6b21\u626b\u63cf<\/li>\n<li><strong>\u7a7a\u95f4\u590d\u6742\u5ea6<\/strong>\uff1aO(n)\uff0c\u7528\u4e8e\u5b58\u50a8\u6570\u7ec4\u548c\u6811\u72b6\u6570\u7ec4<\/li>\n<\/ul>\n<p><strong>\u6ce8\u610f<\/strong>\uff1a\u7ed3\u679c\u9700\u8981\u4f7f\u7528 <strong>long long<\/strong> \u5b58\u50a8\uff0c\u56e0\u4e3a\u6700\u5927\u53ef\u80fd\u8fbe\u5230 C(30000, 3) \u2248 4.5 \u4e07\u4ebf\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u63cf\u8ff0 \u5728\u542b\u6709 n \u4e2a\u6574\u6570\u7684\u5e8f\u5217 a1,a2,\u2026,an \u4e2d\uff0c\u4e09\u4e2a\u6570\u88ab\u79f0\u4f5c thair \u5f53\u4e14\u4ec5\u5f53 i&lt;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":[3],"tags":[],"class_list":["post-68","post","type-post","status-publish","format-standard","hentry","category-3"],"_links":{"self":[{"href":"https:\/\/www.sjll.top\/index.php?rest_route=\/wp\/v2\/posts\/68","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sjll.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sjll.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sjll.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sjll.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=68"}],"version-history":[{"count":0,"href":"https:\/\/www.sjll.top\/index.php?rest_route=\/wp\/v2\/posts\/68\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sjll.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=68"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sjll.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=68"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sjll.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=68"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}