{"id":42,"date":"2011-09-22T04:38:55","date_gmt":"2011-09-21T20:38:55","guid":{"rendered":"http:\/\/www.shuizilong.com\/house\/?p=42"},"modified":"2012-03-03T04:39:50","modified_gmt":"2012-03-02T20:39:50","slug":"hdu-2475-box","status":"publish","type":"post","link":"https:\/\/www.shuizilong.com\/house\/archives\/hdu-2475-box\/","title":{"rendered":"HDU 2475. Box"},"content":{"rendered":"<h3>Brief description :<\/h3>\n<p>&#8230; \u7565. .<\/p>\n<p><!--more--><\/p>\n<h3>Analysis :<\/h3>\n<p>&#8230; \u7565 ..<\/p>\n<pre class=\"brush: cpp; collapse: true; light: false; title: ; toolbar: true; notranslate\" title=\"\">\r\n\/** ` Micro Mezzo Macro Flation -- Overheated Economy ., **\/\r\n#include &lt;iostream&gt;\r\n#include &lt;cstring&gt;\r\n#include &lt;cstdio&gt;\r\nusing namespace std;\r\n#define REP_1(i, n) for (int i=1;i&lt;=int(n);++i)\r\n#define DO_C(n) int n____ = n; while(n____--)\r\ntemplate&lt;class T&gt; inline void RD(T &amp;x){char c; for (c = getchar(); c &lt; '0'; c = getchar()); x = c - '0'; for (c = getchar(); c &gt;= '0'; c = getchar()) x = x * 10 + c - '0';}\r\ninline int RD(){ int x; RD(x); return x;}\r\ninline void RS(char *s){scanf(&quot;%s&quot;, s);}\r\ntemplate&lt;class T&gt; inline void RST(T &amp;A){memset(A, 0, sizeof(A));}\r\ntemplate&lt;class T0, class T1, class T2&gt; inline void RST(T0 &amp;A0, T1 &amp;A1, T2 &amp;A2){RST(A0), RST(A1), RST(A2);}\r\ntemplate&lt;class T&gt; inline void OT(const T &amp;x){printf(&quot;%d\\n&quot;, x);}\r\n\r\n\/* .................................................................................................................................. *\/\r\n\r\nconst int N = 50009;\r\n\r\nint l&#x5B;N], r&#x5B;N], p&#x5B;N]; bool rt&#x5B;N];\r\nint n;\r\n\r\n#define lx l&#x5B;x]\r\n#define rx r&#x5B;x]\r\n\r\ninline void Set(int l&#x5B;], int y, int x){\r\n    l&#x5B;y] = x, p&#x5B;x] = y;\r\n}\r\n\r\ninline void Rotate(int x){\r\n    int y = p&#x5B;x], z = p&#x5B;y];\r\n\r\n    if (!rt&#x5B;y]) Set(y == l&#x5B;z] ? l : r, z, x);\r\n    else p&#x5B;x] = z;\r\n\r\n    if (x == l&#x5B;y]) Set(l, y, rx), Set(r, x, y);\r\n    else Set(r, y, lx), Set(l, x, y);\r\n\r\n    if (rt&#x5B;y]) rt&#x5B;y] = false, rt&#x5B;x] = true;\r\n}\r\n\r\ninline void Splay(int x){\r\n    while (!rt&#x5B;x]) Rotate(x);\r\n}\r\n\r\nint Access(int x){\r\n    int y = 0; do{\r\n        Splay(x);\r\n        rt&#x5B;rx] = true, rt&#x5B;rx = y] = false;\r\n        x = p&#x5B;y = x];\r\n    } while (x);\r\n    return y;\r\n}\r\n\r\ninline int Root(int x){\r\n    for (x = Access(x); lx ; x = lx);\r\n    return x;\r\n}\r\n\r\ninline int Lca(int x, int y){\r\n    int lca; Access(y); y = 0; do{\r\n        Splay(x); if (!p&#x5B;x]) lca = x;\r\n        rt&#x5B;rx] = true, rt&#x5B;rx = y] = false;\r\n        x = p&#x5B;y = x];\r\n    } while (x);\r\n    return lca;\r\n}\r\n\r\n\/\/ Public :\r\n\r\nvoid Link(int y, int x){\r\n\r\n    if (y &amp;&amp; (x == y || Root(x) == Root(y) &amp;&amp; Lca(x, y) == x)) return;\r\n\r\n    Access(x), Splay(x), rt&#x5B;lx] = true;\r\n    lx = p&#x5B;lx] = 0, p&#x5B;x] = y;\r\n    Access(x);\r\n}\r\n\r\nint main(){\r\n\r\n    \/\/freopen(&quot;in.txt&quot;, &quot;r&quot;, stdin);\r\n    \/\/freopen(&quot;out.txt&quot;, &quot;w&quot;, stdout);\r\n\r\n    bool first_blood = true;\r\n\r\n    while (scanf(&quot;%d&quot;, &amp;n) != EOF){\r\n\r\n        \/\/ Initializing Phase ...\r\n\r\n        if (first_blood) first_blood = false; else puts(&quot;&quot;);\r\n\r\n        REP_1(i, n) rt&#x5B;i] = true, RD(p&#x5B;i]);\r\n\r\n        \/\/ Interaction Phase ...\r\n\r\n        char cmd&#x5B;9]; DO_C(RD()){\r\n            RS(cmd); if (cmd&#x5B;0] == 'Q') OT(Root(RD()));\r\n            else Link(RD(), RD());\r\n        }\r\n\r\n        \/\/ Rececling ....\r\n\r\n        RST(p, l, r);\r\n    }\r\n}\r\n\r\n<\/pre>\n<h3>External link :<\/h3>\n<p><a href=\"http:\/\/acm.hdu.edu.cn\/showproblem.php?pid=2475\">http:\/\/acm.hdu.edu.cn\/showproblem.php?pid=2475<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Brief description : &#8230; \u7565. .<\/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":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[1],"tags":[],"class_list":["post-42","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2tdP7-G","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/42","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=42"}],"version-history":[{"count":0,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/42\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/media?parent=42"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/categories?post=42"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/tags?post=42"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}