{"id":842,"date":"2020-10-11T10:43:20","date_gmt":"2020-10-11T02:43:20","guid":{"rendered":"http:\/\/www.zyhcoding.club\/?p=842"},"modified":"2020-10-11T10:53:48","modified_gmt":"2020-10-11T02:53:48","slug":"cdq%e5%88%86%e6%b2%bb","status":"publish","type":"post","link":"http:\/\/www.zyhcoding.club\/index.php\/2020\/10\/11\/cdq%e5%88%86%e6%b2%bb\/","title":{"rendered":"cdq\u5206\u6cbb"},"content":{"rendered":"<h3>CDQ\u5206\u6cbb<\/h3>\n<p>cdq\u5206\u6cbb\u662f\u4e00\u79cd\u5206\u6cbb\u601d\u60f3\uff0c\u5176\u5b9e\u5c31\u662f\u5f52\u5e76\u6392\u5e8f\uff0c\uff0c\uff0c\u3002\u89e3\u51b3\u4e00\u4e2a\u533a\u95f4\u95ee\u9898\uff0c\u5206\u800c\u6cbb\u4e4b\uff0c[l, r]-&gt;[l, mid] + [mid+1, r]\u3002\u540c\u65f6\u5728\u89e3\u51b3\u5b8c\u5b50\u95ee\u9898\u540e\uff0c\u8fd8\u8981\u8003\u8651\u8fd9\u4e24\u4e2a\u5b50\u95ee\u9898\u4e4b\u95f4\u7684\u76f8\u4e92\u5f71\u54cd\uff0c\u6c42\u4e00\u4e2a\u5b50\u95ee\u9898\u5bf9\u53e6\u4e00\u4e2a\u5b50\u95ee\u9898\u7684\u8d21\u732e\u3002<\/p>\n<h4><a href=\"https:\/\/www.luogu.com.cn\/problem\/P3810\">P3810 \u4e09\u7ef4\u504f\u5e8f<\/a><\/h4>\n<p>\u5728\u89e3\u51b3\u79bb\u7ebf\u4e09\u7ef4\u504f\u5e8f\u95ee\u9898\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u5148\u628a\u539f\u6765\u7684\u5143\u7d20\u6309\u7b2c\u4e00\u5173\u952e\u8bcd\uff0c\u7b2c\u4e8c\u5173\u952e\u8bcd\uff0c\u7b2c\u4e09\u5173\u952e\u8bcd\u5347\u5e8f\u6392\u5e8f\uff0c\u8fd9\u6837\u53ef\u4ee5\u4fdd\u8bc1\u4e00\u4e2a\u5143\u7d20\u540e\u9762\u7684\u5143\u7d20\u5bf9\u5b83\u4e0d\u4f1a\u6709\u8d21\u732e(\u6ce8\u610f\u5148\u53bb\u91cd)\u3002\u7136\u540e\u5206\u800c\u6cbb\u4e4b\uff0c\u5728\u4e24\u4e2a\u5b50\u95ee\u9898\u90fd\u88ab\u89e3\u51b3\u4e4b\u540e\uff0c\u8fd9\u4e24\u4e2a\u5b50\u533a\u95f4\u5185\u7684\u5143\u7d20\u5e94\u5df2\u7ecf\u6309\u7b2c\u4e8c\u5173\u952e\u8bcd\u6392\u597d\u5e8f\u4e86\uff0c\u7136\u540e\u6211\u4eec\u4f9d\u7167\u5f52\u5e76\u6392\u5e8f\u7684\u65b9\u5f0f\u4e00\u6b21\u62ff\u4e0b\u4e24\u5b50\u533a\u95f4\u7684\u5143\u7d20\uff0c\u540c\u65f6\u628a\u5143\u7d20\u7684\u7b2c\u4e09\u5173\u952e\u8bcd\u7684\u503c\u52a0\u5165\u6811\u72b6\u6570\u7ec4\u3002\u90a3\u4e48\u5bf9\u4e8e\u53f3\u533a\u95f4\u7684\u6bcf\u4e00\u4e2a\u5143\u7d20\uff0c\u5f53\u8f6e\u5230\u5b83\u88ab\u62ff\u4e0b\u65f6\uff0c\u5728\u6811\u72b6\u6570\u7ec4\u91cc\u5bfb\u627e\u7b2c\u4e09\u5173\u952e\u5b57\u503c\u6bd4\u5b83\u5c0f\u7684\u5143\u7d20\u4e2a\u6570\uff0c\u52a0\u5165\u5230\u5b83\u7684\u8d21\u732e\u91cc\u5373\u53ef\u3002<\/p>\n<pre><code class=\"language-c++\">#include &lt;bits\/stdc++.h&gt;\n\nusing namespace std;\nconst int maxn = 1e5+5, maxk = 2e5+5;\nint n, k, m;\nstruct zyh{\n    int a, b, c;\n    int w, t;\n}nums[maxn], tmp[maxn];\nint c[maxk], ans[maxn];\ninline int lowbit(int i){\n    return i &amp; (-i);\n}\nvoid add(int i, int d){\n    while (i&lt;=k){\n        c[i] += d;\n        i += lowbit(i);\n    }\n    return;\n}\nint sum(int i){\n    int ans = 0;\n    while (i){\n        ans += c[i];\n        i -= lowbit(i);\n    }\n    return ans;\n}\nvoid cdq(int l, int r){\n    if (l==r)   return;\n    int mid = (l+r) &gt;&gt; 1;\n    cdq(l, mid);    cdq(mid+1, r);\n    int i = l, j = mid+1, cnt = 0;\n    for ( ; j&lt;=r; j++){\n        while (i&lt;=mid &amp;&amp; nums[i].b&lt;=nums[j].b)  add(nums[i++].c, nums[i].t);\n        nums[j].w += sum(nums[j].c);\n        \/\/add(nums[j].c, 1);\n    }\n    \/\/for (j=mid+1; j&lt;=r; j++)    add(nums[j].c, -1);\n    for (i--; i&gt;=l; i--)  add(nums[i].c, -nums[i].t);\n    i = mid;\n    \/\/while (nums[i].a)\n    i = l, j = mid+1;\n    while (i&lt;=mid &amp;&amp; j&lt;=r){\n        if (nums[i].b &lt;= nums[j].b) tmp[++cnt] = nums[i++];\n        else    tmp[++cnt] = nums[j++];\n    }\n    while (i&lt;=mid)  tmp[++cnt] = nums[i++];\n    while (j&lt;=r)    tmp[++cnt] = nums[j++];\n    for (i=l, j=1; i&lt;=r; i++, j++)  nums[i] = tmp[j];\n    return;\n}\ninline bool cmp(struct zyh x, struct zyh y){\n    if (x.a != y.a) return x.a &lt; y.a;\n    if (x.b != y.b) return x.b &lt; y.b;\n    return x.c &lt; y.c;\n}\ninline bool equal(struct zyh x, struct zyh y){\n    if (x.a==y.a &amp;&amp; x.b==y.b &amp;&amp; x.c==y.c)   return true;\n    return false;\n}\nint main(){\n    scanf (&quot;%d %d&quot;, &amp;n, &amp;k);\n    for (int i=1; i&lt;=n; i++){\n        scanf (&quot;%d %d %d&quot;, &amp;nums[i].a, &amp;nums[i].b, &amp;nums[i].c);\n        nums[i].w = 0, nums[i].t = 1;\n    }\n    sort (nums+1, nums+n+1, cmp);\n    int cnt = 1;\n    tmp[1] = nums[1];\n    for (int i=2; i&lt;=n; i++){\n        if (!equal(nums[i], nums[i-1])) tmp[++cnt] = nums[i];\n        else    tmp[cnt].t++;\n    }\n    m = n, n = cnt;\n    for (int i=1; i&lt;=n; i++)    nums[i] = tmp[i], nums[i].w = nums[i].t-1;\n    cdq(1, n);\n    for (int i=1; i&lt;=n; i++)    ans[nums[i].w] += nums[i].t;\n    for (int i=0; i&lt;m; i++)\n        printf (&quot;%d\\n&quot;, ans[i]);\n    return 0;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>CDQ\u5206\u6cbb cdq\u5206\u6cbb\u662f\u4e00\u79cd\u5206\u6cbb\u601d\u60f3\uff0c\u5176\u5b9e\u5c31\u662f\u5f52\u5e76\u6392\u5e8f\uff0c\uff0c\uff0c\u3002\u89e3\u51b3\u4e00\u4e2a\u533a\u95f4\u95ee\u9898\uff0c\u5206\u800c\u6cbb\u4e4b\uff0c[l, r [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[63],"tags":[],"_links":{"self":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/842"}],"collection":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/comments?post=842"}],"version-history":[{"count":2,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/842\/revisions"}],"predecessor-version":[{"id":844,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/posts\/842\/revisions\/844"}],"wp:attachment":[{"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/media?parent=842"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/categories?post=842"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.zyhcoding.club\/index.php\/wp-json\/wp\/v2\/tags?post=842"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}