{"id":46,"date":"2011-09-21T04:54:09","date_gmt":"2011-09-20T20:54:09","guid":{"rendered":"http:\/\/www.shuizilong.com\/house\/?p=46"},"modified":"2012-03-03T05:23:44","modified_gmt":"2012-03-02T21:23:44","slug":"poj-3237-tree","status":"publish","type":"post","link":"https:\/\/www.shuizilong.com\/house\/archives\/poj-3237-tree\/","title":{"rendered":"POJ 3237. Tree"},"content":{"rendered":"<h3>Brief description :<\/h3>\n<p>&#8230; QTREE \u57fa\u7840\u4e0a\u589e\u52a0\u4e00\u4e2a\u53d6\u53cd\u64cd\u4f5c. <\/p>\n<h3>Analysis :<\/h3>\n<p>&#8230; \u7565<\/p>\n<p><!--more--><\/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\r\n#include &lt;iostream&gt;\r\n#include &lt;cstring&gt;\r\n#include &lt;cstdio&gt;\r\n#include &lt;cmath&gt;\r\n#include &lt;vector&gt;\r\n\r\nusing namespace std;\r\n\r\n#define REP(i, n) for (int i=0;i&lt;int(n);++i)\r\n#define FOR(i, a, b) for (int i=int(a);i&lt;int(b);++i)\r\n#define REP_1(i, n) for (int i=1;i&lt;=int(n);++i)\r\n#define FOR_C(i, a, b) for (int b____=int(b),i=a;i&lt;b____;++i)\r\n#define DO(n) while(n--)\r\n#define SZ(A) int(A.size())\r\n#define PB push_back\r\n#define MP(A, B) make_pair(A, B)\r\n#define Rush int T____; RD(T____); DO(T____)\r\n#pragma comment(linker, &quot;\/STACK:36777216&quot;)\r\n#pragma GCC optimize (&quot;O2&quot;)\r\n\r\ntemplate&lt;class T&gt; inline void RD(T &amp;);\r\ntemplate&lt;class T&gt; inline void OT(const T &amp;);\r\ntemplate&lt;class T&gt; inline T&amp; _RD(T &amp;x){ RD(x); return x;}\r\ninline void RS(char *s){scanf(&quot;%s&quot;, s);}\r\ntemplate&lt;class T0, class T1&gt; inline void RD(T0 &amp;x0, T1 &amp;x1){RD(x0), RD(x1);}\r\ntemplate&lt;class T0, class T1, class T2&gt; inline void RD(T0 &amp;x0, T1 &amp;x1, T2 &amp;x2){RD(x0), RD(x1), RD(x2);}\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, class T3&gt; inline void RST(T0 &amp;A0, T1 &amp;A1, T2 &amp;A2, T3 &amp;A3){RST(A0), RST(A1), RST(A2), RST(A3);}\r\ntemplate&lt;class T&gt; inline void FLC(T &amp;A, int x){memset(A, x, sizeof(A));}\r\ntemplate&lt;class T&gt; inline void CLR(T &amp;A){A.clear();}\r\ntemplate&lt;class T&gt; inline void checkMax(T &amp;a,const T b){if (b&gt;a) a=b;}\r\ninline int _1(int i){return 1&lt;&lt;i;}\r\ntemplate&lt;class T&gt; inline void RD(T &amp;x){\r\n   \/\/ 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\n   scanf(&quot;%d&quot;, &amp;x);\r\n    }\r\ntemplate&lt;class T&gt; inline void OT(const T &amp;x){printf(&quot;%d\\n&quot;, x);}\r\ntemplate&lt;class T&gt; inline T min(T a, T b, T c){return min(min(a, b), c);}\r\ntemplate&lt;class T&gt; inline T max(T a, T b, T c){return max(max(a, b), c);}\r\n\r\nconst int INF = 0x7fffffff;\r\n\/* .................................................................................................................................. *\/\r\n\r\nconst int N = 10001, M = 2 * N;\r\n\r\nint l&#x5B;N], r&#x5B;N], p&#x5B;N], w0&#x5B;N], w1&#x5B;N] = {-INF}, w2&#x5B;N] = {INF}, neg&#x5B;N]; bool rt&#x5B;N];\r\n\/\/ Link-cut tree\r\nint hd&#x5B;N], nxt&#x5B;M], a&#x5B;M], b&#x5B;M], w&#x5B;M], h&#x5B;M\/2];\r\n\/\/ Adjacent list\r\nint n, res;\r\n\r\n#define lx l&#x5B;x]\r\n#define rx r&#x5B;x]\r\n\r\n\r\ninline void Neg(int x){\r\n    if (x == 0) return;\r\n    w0&#x5B;x] = -w0&#x5B;x]; int t = w1&#x5B;x]; w1&#x5B;x] = -w2&#x5B;x], w2&#x5B;x] = -t, neg&#x5B;x] ^= 1;\r\n}\r\n\r\ninline void Release(int x){\r\n    if (x == 0 || !neg&#x5B;x]) return;\r\n    Neg(lx), Neg(rx), neg&#x5B;x] = 0;\r\n}\r\n\r\ninline void Update(int x){\r\n    w1&#x5B;x] = max(w0&#x5B;x], w1&#x5B;lx], w1&#x5B;rx]);\r\n    w2&#x5B;x] = min(w0&#x5B;x], w2&#x5B;lx], w2&#x5B;rx]);\r\n}\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\n#define z p&#x5B;y]\r\ninline void Rotate(int x){\r\n    int y = p&#x5B;x];\r\n\r\n    if (!rt&#x5B;y]) Release(z), Set(y == l&#x5B;z] ? l : r, z, x);\r\n    else p&#x5B;x] = z;\r\n\r\n    Release(y), Release(x);\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    Update(y);\r\n}\r\n\r\ninline void Splay(int x){\r\n    while (!rt&#x5B;x]) Rotate(x);\r\n}\r\n\r\nvoid Access(int x){\r\n    int y = 0; do{\r\n        Splay(x), Release(x);\r\n        rt&#x5B;rx] = true, rt&#x5B;rx = y] = false, Update(x);\r\n        x = p&#x5B;y = x];\r\n    } while (x);\r\n}\r\n\r\n#define v b&#x5B;i]\r\n#define w w&#x5B;i]\r\n\r\ninline void dfs(int u = 1){\r\n    for(int i=hd&#x5B;u];i;i=nxt&#x5B;i]) if (!p&#x5B;v]){\r\n        p&#x5B;v] = u, w0&#x5B;v] = w, dfs(h&#x5B;i&gt;&gt;1] = v);\r\n    }\r\n}\r\n\r\n#undef x\r\n#undef w\r\n\r\nvoid Query(int x, int y){\r\n    Access(y); y = 0; do{\r\n        Splay(x), Release(x);if (!p&#x5B;x]) res = max(w1&#x5B;rx], w1&#x5B;y]);\r\n        rt&#x5B;rx] = true, rt&#x5B;rx = y] = false, Update(x);\r\n        x = p&#x5B;y = x];\r\n    } while (x);\r\n}\r\n\r\nvoid Modify(int x, int val){\r\n    \/\/Access(x),\r\n    Splay(x), w0&#x5B;x] = val;\r\n    \/\/ Update(x);\r\n}\r\n\r\nvoid Negate(int x, int y){\r\n    Access(y); y = 0; do{\r\n        Splay(x), Release(x); if (!p&#x5B;x]) Neg(rx), Neg(y);\r\n        rt&#x5B;rx] = true, rt&#x5B;rx = y] = false, Update(x);\r\n        x = p&#x5B;y = x];\r\n    } while (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    \/\/ios::sync_with_stdio(false);\r\n\r\n    Rush{\r\n        \/\/ Initializing Phase ...\r\n\r\n        FOR_C(i, 2, _RD(n) &lt;&lt; 1){\r\n            RD(a&#x5B;i], b&#x5B;i], w&#x5B;i]), a&#x5B;i|1] = b&#x5B;i], b&#x5B;i|1] = a&#x5B;i], w&#x5B;i|1] = w&#x5B;i];\r\n            nxt&#x5B;i] = hd&#x5B;a&#x5B;i]], hd&#x5B;a&#x5B;i]] = i; ++i;\r\n            nxt&#x5B;i] = hd&#x5B;a&#x5B;i]], hd&#x5B;a&#x5B;i]] = i;\r\n        }\r\n\r\n        FLC(rt, true), p&#x5B;1] = -1, dfs(), p&#x5B;1] = 0;\r\n\r\n        \/\/ Interaction Phase ...\r\n\r\n        int a, b; char cmd&#x5B;5];\r\n\r\n        while (true){\r\n            RS(cmd); if (cmd&#x5B;0] == 'Q') RD(a, b), Query(a, b), OT(res);\r\n            else if (cmd&#x5B;0] == 'C') RD(a, b), Modify(h&#x5B;a], b);\r\n            else if (cmd&#x5B;0] == 'N') RD(a, b), Negate(a, b);\r\n            else break;\r\n        }\r\n\r\n        \/\/ Rececling ....\r\n\r\n        RST(hd, p, l, r);\r\n    }\r\n}\r\n\r\n<\/pre>\n<h3>External link :<\/h3>\n<p><a href=\"http:\/\/poj.org\/problem?id=3237\">http:\/\/poj.org\/problem?id=3237<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Brief description : &#8230; QTREE \u57fa\u7840\u4e0a\u589e\u52a0\u4e00\u4e2a\u53d6\u53cd\u64cd\u4f5c. Analysis : &#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":[19],"tags":[],"class_list":["post-46","post","type-post","status-publish","format-standard","hentry","category-poj"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2tdP7-K","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/46","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=46"}],"version-history":[{"count":0,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/46\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/media?parent=46"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/categories?post=46"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/tags?post=46"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}