{"id":1188,"date":"2015-07-25T10:29:43","date_gmt":"2015-07-25T02:29:43","guid":{"rendered":"http:\/\/www.shuizilong.com\/house\/?p=1188"},"modified":"2025-03-16T17:11:37","modified_gmt":"2025-03-16T09:11:37","slug":"hdu-5306-gorgeous-sequence","status":"publish","type":"post","link":"https:\/\/www.shuizilong.com\/house\/archives\/hdu-5306-gorgeous-sequence\/","title":{"rendered":"HDU 5306. Gorgeous Sequence"},"content":{"rendered":"<h3>Breif description:<\/h3>\n<p>There is a sequence a of length <code>n<\/code>. We use <code>ai<\/code> to denote the <code>i-th<\/code> element in this sequence. You should do the following three types of operations to this sequence.<\/p>\n<ul>\n<li><code>0 x y t<\/code>: For every x\u2264i\u2264y, we use min(ai,t) to replace the original ai&#8217;s value.\uff08\u533a\u95f4\u53d6 min\uff09<\/li>\n<li><code>1 x y<\/code>: Print the maximum value of ai that x\u2264i\u2264y.\uff08\u533a\u95f4\u6c42\u6700\u5927\u5024\uff09<\/li>\n<li><code>2 x y<\/code>: Print the sum of ai that x\u2264i\u2264y.\uff08\u533a\u95f4\u6c42\u548c\uff09<\/li>\n<\/ul>\n<p><!--more--><\/p>\n<h3>Analysis:<\/h3>\n<ul>\n<li><a href=\"http:\/\/acm.hdu.edu.cn\/showproblem.php?pid=5306\">http:\/\/acm.hdu.edu.cn\/showproblem.php?pid=5306<\/a><\/li>\n<li><a href=\"http:\/\/codeforces.com\/contest\/444\/problem\/C\">http:\/\/codeforces.com\/contest\/444\/problem\/C<\/a><\/li>\n<li><a href=\"http:\/\/codeforces.com\/contest\/453\/problem\/E\">http:\/\/codeforces.com\/contest\/453\/problem\/E<\/a><\/li>\n<\/ul>\n<p>\u8fd9\u7c7b\u7ebf\u6bb5\u6811\u95ee\u9898\u7684\u6807\u8bb0\u64cd\u4f5c\u6bd4\u4e00\u822c\u7684\u8981\u590d\u6742\uff0c\u9700\u8981\u4f7f\u7528\u5747\u644a\u5206\u6790\u3002<\/p>\n<p>\u9996\u5148\u5c1d\u8bd5\u5199\u4e00\u4e2a <a href=\"https:\/\/gist.github.com\/lychees\/30304a76a2247a17b4c5\">\u66b4\u529b\u7684\u7ebf\u6bb5\u6811<\/a>\u3002<\/p>\n<pre class=\"brush: cpp; light: false; title: ; toolbar: true; notranslate\" title=\"\">\n...\nvoid upd(int x, int l, int r){  \/\/\u4e0a\u4f20\u4fe1\u606f\n    T&#x5B;x].ss = T&#x5B;lx].ss + T&#x5B;rx].ss;\n    T&#x5B;x].mx = max(T&#x5B;lx].mx, T&#x5B;rx].mx);\n}\n\nvoid pnc(int x, int l, int r, int t){ \/\/\u6253\u6807\u8bb0\n    if (T&#x5B;x].mx &amp;lt;= t) return;\n\n&lt;pre&gt;&lt;code&gt;T&#x5B;x].tag = t;\n\nif (l &amp;amp;lt; r){\n    pnc(lx, l, ml, t); pnc(rx, mr, r, t);\n    upd(xx);\n}\nelse{\n    T&#x5B;x].ss = T&#x5B;x].mx = t;\n}\n&lt;\/code&gt;&lt;\/pre&gt;\n\n}\n\nvoid rls(int x, int l, int r){ \/\/\u4e0b\u653e\u6807\u8bb0\uff0c\u56e0\u4e3a\u6211\u4eec\u662f\u66b4\u529b\u6253\u6807\u8bb0\u7684\u3002\u3002\u6240\u4ee5\u6682\u65f6\u4e0d\u9700\u8981\u3002\u3002\n    \/\/pnc(lc, T&#x5B;x].tag); pnc(rc, T&#x5B;x].tag);\n}\n<\/pre>\n<p>\u6211\u4eec\u53d1\u73b0\u4e3b\u8981\u74f6\u9888\u662f\u6253\u6807\u8bb0\u8fd9\u4e2a\u6b65\u9aa4\uff0c\u56e0\u4e3a\u5728\u6807\u8bb0\u4f5c\u7528\u4e0a\u7684\u65f6\u523b\uff0c\u6211\u4eec\u4e0d\u77e5\u9053\u5b83\u7684\u5b50\u6811\u4e2d\uff0c\u6709\u591a\u5c11\u8282\u70b9\u5c06\u4f1a\u88ab\u8fd9\u4e2a\u6807\u8bb0\u5f71\u54cd\u3002<br \/>\n\u56e0\u6b64\u9700\u8981\u9012\u5f52\u5230\u5b50\u6811\u4e2d\u53bb\u3002\u3002\u3002<\/p>\n<p>\u6ce8\u610f\u5230\u8fd9\u9898\u4e2d\u6211\u4eec\u7684 <code>tag<\/code> \u6807\u8bb0\u662f\u5355\u8c03\u7684\uff0c\u53ea\u9700\u8981\u7559\u610f\u5b50\u6811\u4e2d\u66f4\u5c0f\u7684\u6807\u8bb0\u3002<\/p>\n<pre>\n   4            4\n 2   6   -&gt;   2   -\n1 3 5 7      1 -\n<\/pre>\n<p>\u867d\u7136\u8fd9\u6837\u7b2c\u4e00\u884c\u52a0\u4e86\u526a\u679d\uff0c\u7136\u800c\u5e76\u6ca1\u6709\u4ec0\u4e48\u5375\u7528\u3002\u6700\u574f\u60c5\u51b5\u4e0b\uff0c\u9012\u51cf\u7684\u4fee\u6539\u6839\u8282\u70b9\uff0c\u6bcf\u6b21\u6211\u4eec\u9700\u8981\u9012\u5f52\u6574\u68f5\u6811\uff0c\u590d\u6742\u5ea6 O(n)\uff0c\u548c\u76f4\u63a5\u7528\u6570\u7ec4\u66b4\u529b\u7684\u590d\u6742\u5ea6\u4e00\u6837\u3002\u3002\u3002<br \/>\n\u3002\u3002\u3002\u56de\u5fc6 <a href=\"http:\/\/codeforces.com\/contest\/444\/problem\/C\">http:\/\/codeforces.com\/contest\/444\/problem\/C<\/a> \u3002\u3002\u3002<\/p>\n<p>\u6211\u4eec\u6ce8\u610f\u5230\uff0c\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6240\u6709\u8282\u70b9\u7684 tag \u4fe1\u606f\u603b\u662f\u76f8\u540c\u7684\uff0c\u6211\u4eec\u7406\u5e94\u5229\u7528\u8fd9\u4e2a\u4fe1\u606f\uff0c\u4e0d\u8981\u6bcf\u6b21\u9012\u5f52\u5230\u6574\u4e2a\u5b50\u6811\u4e2d\u53bb\u3002<br \/>\n\u800c\u8fd9\u8981\u6c42\u6211\u4eec\u591a\u7ef4\u62a4\u4e00\u4e2a\u989d\u5916\u4fe1\u606f\u3002\u3002\u3002<\/p>\n<pre class=\"brush: cpp; light: false; title: ; toolbar: true; notranslate\" title=\"\">\nstruct rec{\n    LL ss; int mx, cv;\n    int tag;\n} T&#x5B;N];\n<\/pre>\n<p>\u8fd9\u91cc <code>cv<\/code> \u8868\u793a\uff0c\u8fd9\u4e2a\u533a\u95f4\u4e2d\u6709\u591a\u5c11\u4e2a\u7ed3\u70b9\uff0c\u5df2\u88ab\u5b50\u6811\u4e2d\u7684 <code>tag<\/code> \u5f71\u54cd\u3002<\/p>\n<p>\u6211\u4eec\u6709\uff1a<\/p>\n<pre class=\"brush: cpp; light: false; title: ; toolbar: true; notranslate\" title=\"\">\nvoid upd(int x, int l, int r){\n    T&#x5B;x].ss = T&#x5B;lx].ss + T&#x5B;rx].ss;\n    T&#x5B;x].mx = max(T&#x5B;lx].mx, T&#x5B;rx].mx);\n    T&#x5B;x].cv = T&#x5B;lx].cv + T&#x5B;rx].cv;\n}\n\nvoid pnc(int x, int l, int r, int t){\n    if (T&#x5B;x].tag != Dummy &amp;amp;&amp;amp; T&#x5B;x].tag &amp;lt;= t) return;\n\n&lt;pre&gt;&lt;code&gt;T&#x5B;x].tag = t;\nif (T&#x5B;x].cv != r-l+1){\n    T&#x5B;x].mx = t;\n    T&#x5B;x].ss += (LL) t * ((r-l+1)-T&#x5B;x].cv);\n    T&#x5B;x].cv = r-l+1;\n}\n&lt;\/code&gt;&lt;\/pre&gt;\n\n}\n\nvoid rls(int x, int l, int r){\n    if (T&#x5B;x].tag != Dummy){\n        pnc(lc, T&#x5B;x].tag); pnc(rc, T&#x5B;x].tag);\n    }\n}\n\n<\/pre>\n<p>\u3002\u3002\u3002\u8fd9\u6837\u8fd8\u662f\u6211\u4eec\u719f\u6089\u7684\u666e\u901a\u7ebf\u6bb5\u6811\u7684\u5199\u6cd5\u3002\u3002\u3002\u590d\u6742\u5ea6\u662f\u5bf9\u7684\u3002\u3002\u4f46\u662f\u95ee\u9898\u5728\u4e8e\uff0c\u5728\u6253\u6807\u8bb0\u7684\u65f6\u5019\uff0c\u6211\u4eec\u5176\u5b9e\u5e76\u4e0d\u80fd\u76f4\u63a5\u7ef4\u62a4 <code>cv<\/code>\u3002<br \/>\n\u56e0\u6b64\u5728\u6bcf\u6b21\u4fee\u6539\u7684\u65f6\u5019\uff0c\u6211\u4eec\u8fd8\u9700\u8981\u6dfb\u52a0\u4e00\u4e2a <code>fix<\/code> \u51fd\u6570\uff0c\u6e05\u7406\u5b50\u6811\u4e2d\u6240\u6709\u5927\u4e8e\u7b49\u4e8e <code>t<\/code> \u7684\u6807\u8bb0\u3002<\/p>\n<pre class=\"brush: cpp; light: false; title: ; toolbar: true; notranslate\" title=\"\">\nvoid Insert(int x, int l, int r){\n\n&lt;pre&gt;&lt;code&gt;if (T&#x5B;x].mx &amp;amp;lt;= t) return;\n\nif (b &amp;amp;lt; l || r &amp;amp;lt; a) return;\nif (a &amp;amp;lt;= l &amp;amp;amp;&amp;amp;amp; r &amp;amp;lt;= b){\n    fix(xx, t);\n    if (l == r){\n        T&#x5B;x].ss = T&#x5B;x].mx = T&#x5B;x].tag = t;\n        T&#x5B;x].cv = 1;\n    }\n    else{\n        upd(xx);\n    }\n    pnc(xx, t);\n}\nelse{\n    rls(xx);\n    Insert(lc); Insert(rc);\n    upd(xx);\n}\n&lt;\/code&gt;&lt;\/pre&gt;\n\n}\n<\/pre>\n<p>\u3002\u3002\u3002\u8fd9\u6837\u6211\u4eec\u8fd8\u5269 <code>fix<\/code> \u51fd\u6570\u9700\u8981\u8003\u5bdf\u3002\u3002\u3002<\/p>\n<pre class=\"brush: cpp; light: false; title: ; toolbar: true; notranslate\" title=\"\">\nvoid fix(int x, int l, int r, int t){\n\n&lt;pre&gt;&lt;code&gt;if (T&#x5B;x].mx &amp;amp;lt; t) return;\n\nif (T&#x5B;x].tag &amp;amp;gt;= t){\n    T&#x5B;x].tag = Dummy;\n}\nif (l == r){\n    T&#x5B;x].ss = T&#x5B;x].mx = T&#x5B;x].tag = T&#x5B;x].tag;\n    T&#x5B;x].cv = T&#x5B;x].tag != Dummy;\n}\nelse{\n    rls(xx);\n    fix(lc, t); fix(rc, t);\n    upd(xx);\n}\n&lt;\/code&gt;&lt;\/pre&gt;\n\n}\n<\/pre>\n<p>\u3002\u3002\u3002\u6b64\u65f6\u5b83\u5df2\u7ecf\u548c\u6211\u4eec\u6700\u521d\u7684\u65b9\u6cd5\u6709\u4e86\u5f88\u5927\u7684\u4e0d\u540c\u3002\u3002\u3002\u3002\u3002<\/p>\n<p>\u521d\u770b\u8d77\u6765\uff0c\u6bcf\u6b21 <code>Insert<\/code> \u64cd\u4f5c\u6211\u4eec\u53ef\u80fd\u8c03\u7528 <code>O(log(n))<\/code> \u6b21 <code>fix<\/code> \u51fd\u6570\uff0c\u6bcf\u6b21 <code>fix<\/code> \u7684\u590d\u6742\u5ea6\u53c8\u53ef\u80fd\u9ad8\u8fbe <code>O(n)<\/code>\u3002<\/p>\n<p>\u6211\u4eec\u5bf9\u5176\u8fdb\u884c\u5747\u644a\u5206\u6790\uff01<\/p>\n<p>\u5f53\u4e00\u4e2a\u6807\u8bb0\u88ab\u6e05\u7406\u6210 <code>Dummy<\/code> \u65f6\uff0c\u5176\u5b50\u6811\u4e2d\u6240\u6709\u6807\u8bb0\u4e5f\u90fd\u88ab\u8d4b\u6210 <code>Dummy<\/code>\uff0c\u5e76\u4e14\u5b83\u7684 <code>mx = 0<\/code><br \/>\n\u6211\u4eec\u8003\u5bdf\u6700\u4e0a\u9762\u7684\u90a3\u4e9b <code>Dummy<\/code> \u6807\u8bb0\uff0c\u79f0\u4e4b\u4e3a <code>\u5c01\u95ed\u8282\u70b9<\/code>\u3002<br \/>\n\u6211\u4eec\u8bbe\u7f6e <code>\u5c01\u95ed\u8282\u70b9<\/code> \u7684 <code>mx<\/code> \u5024\u7b49\u4e8e 0\uff0c\u56e0\u800c\u5b83\u4eec\u4e0d\u4f1a\u53c2\u4e0e\u5230 <code>fix<\/code> \u51fd\u6570\u7684\u9012\u5f52\u4e2d\u53bb\u3002<\/p>\n<li>\u5bf9\u4e8e\u6bcf\u6b21 <code>Insert<\/code> \u64cd\u4f5c\uff0c\u6700\u591a\u53ef\u80fd\u5f62\u6210 <code>O(n)<\/code> \u4e2a <code>\u5c01\u95ed\u8282\u70b9<\/code>\u3002\n<li>\u5bf9\u4e8e\u6bcf\u6b21 <code>Insert<\/code> \u64cd\u4f5c\uff0c\u6700\u591a\u53ea\u4f1a\u6253\u5f00 <code>O(logn)<\/code> \u4e2a <code>\u5c01\u95ed\u8282\u70b9<\/code>\u3002\n<p>\u56e0\u6b64\u603b\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4ecd\u4e3a <code>O(nlogn)<\/code>\u3002<a href=\"https:\/\/gist.github.com\/lychees\/ddead737381c55c01250\">-> \u5b8c\u6574\u4ee3\u7801<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Breif description: There is a sequence a of length n. We use ai to denote the i-th element in this sequence. You should do the following three types of operations to this sequence. 0 x y t: For every x\u2264i\u2264y, we use min(ai,t) to replace the original ai&#8217;s value.\uff08\u533a\u95f4\u53d6 min\uff09 1 x y: Print the [&hellip;]<\/p>\n","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":[1],"tags":[],"class_list":["post-1188","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2tdP7-ja","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/1188","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=1188"}],"version-history":[{"count":2,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/1188\/revisions"}],"predecessor-version":[{"id":3415,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/1188\/revisions\/3415"}],"wp:attachment":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/media?parent=1188"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/categories?post=1188"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/tags?post=1188"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}