{"id":39,"date":"2023-01-31T20:29:00","date_gmt":"2023-01-31T12:29:00","guid":{"rendered":"https:\/\/solost.cn\/?p=39"},"modified":"2023-02-02T00:36:50","modified_gmt":"2023-02-01T16:36:50","slug":"79","status":"publish","type":"post","link":"https:\/\/solost.cn\/?p=39","title":{"rendered":"AcWing-241. \u697c\u5170\u56fe\u817e"},"content":{"rendered":"<p><img decoding=\"async\" src=\"\/wp-content\/uploads\/2026\/11\/2265409832.png\" alt=\"2265409832.png\" title=\"2265409832.png\"><\/p>\n<pre><code class=\"lang-cpp\">#include&lt;map&gt;\n#include&lt;set&gt;\n#include&lt;stack&gt;\n#include&lt;queue&gt;\n#include&lt;cmath&gt;\n#include&lt;vector&gt;\n#include&lt;bitset&gt;\n#include&lt;cstdio&gt;\n#include&lt;cstring&gt;\n#include&lt;fstream&gt;\n#include&lt;cstdlib&gt;\n#include&lt;iostream&gt;\n#include&lt;algorithm&gt;\n#include&lt;unordered_map&gt;\n\nusing namespace std;\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;int,int&gt; PII;\ntypedef unsigned long long ull;\nconst int INF = 0x3f3f3f3f, mod = 1000000007;\nconst int N = 2e5 + 10;\nint n;\nll a[N], gr[N], sm[N];\/\/gr\u6bd4\u7b2ci\u4e2a\u6570\u5927\u7684\u6570\uff0csm\u6bd4\u7b2ci\u4e2a\u6570\u5c0f\u7684\u6570\nll tree[N];\/\/\u6811\u72b6\u6570\u7ec4\n\nint lowbit(int x){\n    return x &amp; (-x);\n}\n\nvoid add(int x, int c){\n    for(int i = x; i &lt;= n; i += lowbit(i))\n        tree[i] += c;\n}\n\nll query(int x){\n    ll res = 0;\n    for(int i = x; i; i -= lowbit(i))\n        res += tree[i];\n    return res;\n}\n\nint main(){\n    cin &gt;&gt; n;\n    for(int i = 1; i &lt;= n; i++){\n        cin &gt;&gt; a[i];\n    }\n    for(int i = 1; i &lt;= n; i++){\n        int t = a[i];\n        gr[i] = query(n) - query(t);\/\/\u6bd4\u5f53\u524d\u6570\u5927\u7684\u6570\u7684\u4e2a\u6570\uff0c\u76f8\u5f53\u4e8e\u524d\u7f00\u548c\u7684(t, n]\u4e2d\u6570\u7684\u4e2a\u6570\n        sm[i] = query(t - 1);\/\/\u76f8\u5f53\u4e8e\u6bd4[1, t - 1]\u7684\u6bd4\u5f53\u524d\u6570\u5c0f\u7684\u6570\n        add(t, 1);\/\/\u628a\u8fd9\u4e2a\u6570\u653e\u5230\u6811\u72b6\u6570\u7ec4\n    }\n    ll res1 = 0, res2 = 0;\n    for(int i = 1; i &lt;= n; i++){\n        int t = a[i];\n        res1 += gr[i] * (n - gr[i] - t);\/\/v\u7684\u4e2a\u6570\u5c31\u662f\u5de6\u8fb9\u6bd4t\u5927\u7684\u6570\u4e58\u53f3\u8fb9\u6bd4t\u5927\u7684\u6570\u7684\u4e2a\u6570\n                    \/\/\u56e0\u4e3a\u6bd4t\u5927\u7684\u6570\u6709n-t\u4e2a\uff0c\u5de6\u8fb9\u5360\u4e86gr\u4e2a\uff0c\u53f3\u8fb9\u5269\u4e0bn-t-gr[i]\n        res2 += sm[i] * (t - 1 - sm[i]);\n    }\n    cout &lt;&lt; res1 &lt;&lt; &quot; &quot; &lt;&lt; res2 &lt;&lt; endl;\n    return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>#include&lt;map&gt; #include&lt;set&gt; #include&lt;stack&gt; #include&lt;queue&gt; #include&lt;cmath&gt; #include&lt;vector&gt; #include&lt;bitset&gt; #include&lt;cstdio&gt; #include&lt;cstring&gt; #include&lt;fstream&gt; #include&lt;cstdlib&gt; #include&lt;iostream&gt; #include&lt;algorithm&gt; #include&lt;unordered_map&gt; using namespace std; #define endl &#039;\\n&#039; #define IOS \\ ios_base::sync_with_stdio(0); \\ cin.tie(0); \\ cout.tie(0); typedef long long ll; typedef pair&lt;int,int&gt; PII; typedef unsigned long long ull; const int INF = 0x3f3f3f3f, mod = 1000000007; const int N = 2e5 + 10; [&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":[15],"class_list":["post-39","post","type-post","status-publish","format-standard","hentry","category-solution","tag-15"],"_links":{"self":[{"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts\/39","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=39"}],"version-history":[{"count":0,"href":"https:\/\/solost.cn\/index.php?rest_route=\/wp\/v2\/posts\/39\/revisions"}],"wp:attachment":[{"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=39"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=39"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solost.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=39"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}