{"id":980,"date":"2014-09-27T07:57:20","date_gmt":"2014-09-26T23:57:20","guid":{"rendered":"http:\/\/www.shuizilong.com\/house\/?p=980"},"modified":"2014-09-27T07:57:20","modified_gmt":"2014-09-26T23:57:20","slug":"bzoj-1503-%e9%83%81%e9%97%b7%e7%9a%84%e5%87%ba%e7%ba%b3%e5%91%98","status":"publish","type":"post","link":"https:\/\/www.shuizilong.com\/house\/archives\/bzoj-1503-%e9%83%81%e9%97%b7%e7%9a%84%e5%87%ba%e7%ba%b3%e5%91%98\/","title":{"rendered":"BZOJ 1503. \u90c1\u95f7\u7684\u51fa\u7eb3\u5458"},"content":{"rendered":"<p><!--more--><\/p>\n<p><a href=\"http:\/\/www.lydsy.com\/JudgeOnline\/problem.php?id=1503\">http:\/\/www.lydsy.com\/JudgeOnline\/problem.php?id=1503<\/a><\/p>\n<h3>Brief description: <\/h3>\n<p>\u7565\u3002\uff09<\/p>\n<h3>Analysis: <\/h3>\n<p>\u6d4b\u8bd5\u6a21\u677f\u3002\uff09<\/p>\n<pre class=\"brush: cpp; light: false; title: Size Balanced Tree; toolbar: true; notranslate\" title=\"Size Balanced Tree\">\r\n\/\/}\/* .................................................................................................................................. *\/\r\n \r\nnamespace SBT{\r\n    const int NN = int(1e5) + 9;\r\n    int c&#x5B;2]&#x5B;NN], sz&#x5B;NN], ky&#x5B;NN], tot;\r\n#define lx l&#x5B;x]\r\n#define rx r&#x5B;x]\r\n#define l c&#x5B;d]\r\n#define r c&#x5B;!d]\r\n#define kx ky&#x5B;x]\r\n#define sx sz&#x5B;x]\r\n#define d 0\r\n    int new_node(int v = 0){\r\n        int x=++tot;lx=rx=0;\r\n        sx=1;kx=v;\r\n        return x;\r\n    }\r\n \r\n    void upd(int x){\r\n        sx=sz&#x5B;lx]+1+sz&#x5B;rx];\r\n    }\r\n#undef d\r\n    void rot(int &amp;x,int d){\r\n        int y=rx;rx=l&#x5B;y];l&#x5B;y]=x;\r\n        upd(x),upd(y),x=y;\r\n    }\r\n \r\n    void fix(int &amp;x,int d){\r\n        if (sz&#x5B;l&#x5B;lx]] &gt; sz&#x5B;rx]) rot(x,!d);\r\n        else{\r\n            if (sz&#x5B;r&#x5B;lx]] &gt; sz&#x5B;rx]) rot(lx,d),rot(x,!d);\r\n            else return;\r\n        }\r\n        d=0,fix(lx,0),fix(rx,1);\r\n        fix(x,0),fix(x,1);\r\n    }\r\n#define d 0\r\n \r\n    int mini(int x){\r\n        if (lx) return mini(lx);\r\n        return ky&#x5B;x];\r\n    }\r\n \r\n    void iod(int x){\r\n        if (!x) return;\r\n        iod(lx); printf(&quot;%d &quot;, ky&#x5B;x]); iod(rx);\r\n    }\r\n \r\n    void Ins(int &amp;x,int v){\r\n        if(!x) x = new_node(v);\r\n        else{\r\n            ++sz&#x5B;x]; Ins(c&#x5B;v&gt;kx]&#x5B;x],v);\r\n            fix(x,v&gt;=kx);\r\n        }\r\n    }\r\n \r\n    int d_key; void Del(int &amp;x,int v){\r\n        --sx;if(kx==v||(v&lt;kx&amp;&amp;!lx)||(v&gt;kx&amp;&amp;!rx)){\r\n            if(!lx||!rx) d_key = kx, x = lx | rx;\r\n            else Del(lx,v+1), kx = d_key;\r\n        }\r\n        else Del(c&#x5B;v&gt;kx]&#x5B;x],v);\r\n    }\r\n \r\n    int Rank(int x,int v){\r\n        int z=0;while(x){\r\n            if(kx&lt;v){\r\n                z+=sz&#x5B;lx]+1;\r\n                x=rx;\r\n            }\r\n            else x=lx;\r\n        }\r\n        return z;\r\n    }\r\n \r\n    int Select(int x,int k){\r\n        if (sz&#x5B;lx] == k) return ky&#x5B;x];\r\n        return sz&#x5B;lx]&gt;k?Select(lx,k):Select(rx,k-sz&#x5B;lx]-1);\r\n    }\r\n \r\n    bool Find(int x,int v){\r\n        if (!x) return 0;if (kx==v) return 1;\r\n        return Find(c&#x5B;v&gt;kx]&#x5B;x],v);\r\n    }\r\n \r\n    void Init(){\r\n        tot = 0;\r\n    }\r\n \r\n#undef d\r\n#undef l\r\n#undef r\r\n#undef lx\r\n#undef rx\r\n#undef sx\r\n#undef kx\r\n} using namespace SBT;\r\n \r\nint n, b, d, rt, cnt;\r\n \r\nvoid Insert(int v){\r\n    if (v &lt; b) return;\r\n    v -= d; Ins(rt, v);\r\n}\r\n \r\nint main(){\r\n \r\n#ifndef ONLINE_JUDGE\r\n    freopen(&quot;in.txt&quot;, &quot;r&quot;, stdin);\r\n    \/\/freopen(&quot;out.txt&quot;, &quot;w&quot;, stdout);\r\n#endif\r\n    while (~scanf(&quot;%d %d&quot;, &amp;n, &amp;b)){\r\n \r\n        Init(); rt = cnt = 0;\r\n \r\n        DO(n){\r\n            char cmd; RC(cmd); int k; RD(k); switch(cmd){\r\n                case 'I':\r\n                    Insert(k);\r\n                    break;\r\n                case 'A':\r\n                    d += k;\r\n                    break;\r\n                case 'S':\r\n                    d -= k;\r\n                    while (rt &amp;&amp; mini(rt) + d &lt; b) Del(rt, mini(rt)),++cnt;\r\n                    break;\r\n                default:\r\n                    if (k &gt; sz&#x5B;rt]) puts(&quot;-1&quot;);\r\n                    else{\r\n                        OT(Select(rt,sz&#x5B;rt]-k)+d);\r\n                    }\r\n            }\r\n        }\r\n \r\n        OT(cnt);\r\n    }\r\n}\r\n<\/pre>\n<p><a href=\"https:\/\/gist.github.com\/lychees\/9fb9e11fba385b4100a9>https:\/\/gist.github.com\/lychees\/9fb9e11fba385b4100a9<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"","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":[133],"class_list":["post-980","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-offset"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2tdP7-fO","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/980","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=980"}],"version-history":[{"count":0,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/980\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/media?parent=980"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/categories?post=980"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/tags?post=980"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}