{"id":1005,"date":"2014-10-03T13:39:49","date_gmt":"2014-10-03T05:39:49","guid":{"rendered":"http:\/\/www.shuizilong.com\/house\/?p=1005"},"modified":"2016-08-24T19:32:50","modified_gmt":"2016-08-24T11:32:50","slug":"bzoj-2733-hnoi2012%e6%b0%b8%e6%97%a0%e4%b9%a1","status":"publish","type":"post","link":"https:\/\/www.shuizilong.com\/house\/archives\/bzoj-2733-hnoi2012%e6%b0%b8%e6%97%a0%e4%b9%a1\/","title":{"rendered":"BZOJ 2733. [HNOI2012]\u6c38\u65e0\u4e61"},"content":{"rendered":"<p><!--more--><\/p>\n<h3>Brief description: <\/h3>\n<p>\u7565\u3002\uff09<\/p>\n<h3>Analysis: <\/h3>\n<p>\u5e73\u8861\u6811\u542f\u53d1\u5f0f\u5408\u5e76\u3002<\/p>\n<pre class=\"brush: cpp; light: false; title: ; toolbar: true; notranslate\" title=\"\">\r\n\r\n\/\/}\/* .................................................................................................................................. *\/\r\n\r\nconst int N = int(1e5) + 9;\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);\/\/upd(x);\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            Ins(c&#x5B;v&gt;kx]&#x5B;x],v);\r\n            fix(x,v&gt;=kx);\r\n        }\r\n        upd(x);\r\n    }\r\n\r\n    void Ins2(int &amp;x, int xx){\r\n        if (!x) x = xx;\r\n        else{\r\n            Ins2(c&#x5B;ky&#x5B;xx]&gt;kx]&#x5B;x], xx);\r\n            fix(x, ky&#x5B;xx] &gt;= kx);\r\n        }\r\n        upd(x);\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 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    int Q&#x5B;N], cz, op;\r\n\r\n    void Merge(int x, int&amp; y){ \/\/ x -&gt; y\r\n        cz = 0, op = 1; Q&#x5B;cz] = x;\r\n        while (cz &lt; op){\r\n            x = Q&#x5B;cz++];\r\n            if (lx) Q&#x5B;op++] = lx;\r\n            if (rx) Q&#x5B;op++] = rx;\r\n            lx = rx = 0;\r\n            Ins2(y, x);\r\n        }\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\n\r\nint T&#x5B;N], P&#x5B;N], sz&#x5B;N]; \/\/ DSU\r\nint n;\r\n\r\nint Find(int x){\r\n    return P&#x5B;x] == x ? x : P&#x5B;x] = Find(P&#x5B;x]);\r\n}\r\n\r\nvoid Union(int x, int y){\r\n    x = Find(x), y = Find(y); if (x == y) return;\r\n    if (sz&#x5B;T&#x5B;x]] &gt; sz&#x5B;T&#x5B;y]]) swap(x, y);\r\n    P&#x5B;x] = y; SBT::Merge(T&#x5B;x], T&#x5B;y]);\r\n}\r\n\r\nvoid Init(){\r\n    SBT::Init(); int m; RD(n, m);\r\n    REP_1(i, n) T&#x5B;i] = SBT::new_node(RD()), P&#x5B;i] = i, sz&#x5B;i] = 1;\r\n    DO(m){\r\n        int x, y; RD(x, y);\r\n        Union(x, y);\r\n    }\r\n}\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\r\n\r\n    Init(); Rush{\r\n        char cmd; int x, y;\r\n        RC(cmd); RD(x, y);\r\n        if (cmd == 'B'){\r\n            Union(x, y);\r\n        }\r\n        else{\r\n            x = Find(x);\r\n            if (y &gt; SBT::sz&#x5B;T&#x5B;x]]){\r\n                OT(-1);\r\n            }\r\n            else{\r\n                --y;\r\n                OT(SBT::Select(T&#x5B;x], y));\r\n            }\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<h3>References: <\/h3>\n<p>\u9519\u7684\uff1f\uff09<a href=\"http:\/\/blog.sina.com.cn\/s\/blog_6e63f59e0101bj9b.html\">http:\/\/blog.sina.com.cn\/s\/blog_6e63f59e0101bj9b.html<\/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":[22],"tags":[137],"class_list":["post-1005","post","type-post","status-publish","format-standard","hentry","category-bzoj","tag-137"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2tdP7-gd","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/1005","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=1005"}],"version-history":[{"count":0,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/1005\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/media?parent=1005"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/categories?post=1005"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/tags?post=1005"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}