{"id":295,"date":"2010-07-13T19:34:22","date_gmt":"2010-07-13T11:34:22","guid":{"rendered":"http:\/\/www.shuizilong.com\/house\/?p=295"},"modified":"2013-05-17T00:52:38","modified_gmt":"2013-05-16T16:52:38","slug":"poj-3321-apple-tree","status":"publish","type":"post","link":"https:\/\/www.shuizilong.com\/house\/archives\/poj-3321-apple-tree\/","title":{"rendered":"POJ 3321. Apple Tree"},"content":{"rendered":"<h3>Brief description: <\/h3>\n<p>\u7ed9\u5b9a\u4e00\u68f5\u6811\uff0c\u6bcf\u4e2a\u7ed3\u70b9\u6709 0\/1 \u4e24\u79cd\u72b6\u6001\uff0c\u8981\u6c42\u52a8\u6001\u7ef4\u62a4\u4ee5\u4e0b\u64cd\u4f5c\u3002\uff08\u521d\u59cb\u5168 1 .. .<\/p>\n<ul>\n<li>C x: \u5c06 x \u70b9\u7684\u72b6\u6001\u53d6\u53cd\u3002<\/li>\n<li>Q x: \u8be2\u95ee\u4ee5 x \u7ed3\u70b9\u4e3a\u6839\u5b50\u6811\u4e2d\uff0c1 \u7684\u4e2a\u6570\u3002<\/li>\n<\/ul>\n<p><!--more--><\/p>\n<h3>Analysis: <\/h3>\n<p>&#8230; \u7565\uff09\u3002\u3002\uff08 DFS \u5e8f\u5217 + (\u7ebf\u6bb5\u6811 or \u6811\u72b6\u6570\u7ec4)\u3002\u3002<\/p>\n<p><a href=\"http:\/\/acm.hust.edu.cn\/vjudge\/problem\/viewSource.action?id=1169124\">http:\/\/acm.hust.edu.cn\/vjudge\/problem\/viewSource.action?id=1169124<\/a><\/p>\n<pre class=\"brush: cpp; collapse: true; first-line: 1; light: false; title: \u7ebf\u6bb5\u6811.cpp; toolbar: true; notranslate\" title=\"\u7ebf\u6bb5\u6811.cpp\">\r\nconst int N = 100009, M = 2 * N;\r\n\r\nint L&#x5B;N], R&#x5B;N], cnt;\r\n\/\/ Vertex .. .\r\nint hd&#x5B;N], nxt&#x5B;M], a&#x5B;M], b&#x5B;M];\r\n\/\/ Adjacent List .. .\r\nint C&#x5B;4*N];\r\n\/\/ Interval Tree\r\nint n;\r\n\r\n#define root 1, 1, n\r\n#define lx (x&lt;&lt;1)\r\n#define rx (lx|1)\r\n#define m ((l+r)&gt;&gt;1)\r\n#define lc lx, l, m, a, b\r\n#define rc rx, m+1, r, a, b\r\n\r\n#define pos a\r\n#define delta b\r\nvoid Modify(int x, int l, int r, int pos, int delta){\r\n    C&#x5B;x] += delta; if (l &lt; r){\r\n        if (pos &lt;= m) Modify(lc);\r\n        else Modify(rc);\r\n    }\r\n}\r\n#undef pos\r\n\r\nint Sum(int x, int l, int r, int a, int b){\r\n    if (a &lt;= l &amp;&amp; r &lt;= b) return C&#x5B;x];\r\n    else return (a &lt;= m ? Sum(lc) : 0) + (m &lt; b ? Sum(rc) : 0);\r\n}\r\n\r\n#define v b&#x5B;i]\r\n\r\ninline void dfs(int u = 0){\r\n    L&#x5B;u] = ++cnt;\r\n    for (int i=hd&#x5B;u];i;i=nxt&#x5B;i]) if (!L&#x5B;v]){\r\n        dfs(v);\r\n    }\r\n    R&#x5B;u] = cnt;\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#endif\r\n\r\n    FOR_C(i, 2, _RD(n) &lt;&lt; 1){\r\n        RD(a&#x5B;i], b&#x5B;i]), --a&#x5B;i], --b&#x5B;i], a&#x5B;i|1] = b&#x5B;i], b&#x5B;i|1] = a&#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    dfs(); REP_1(i, n) Modify(root, i, 1);\r\n\r\n    char cmd; int pos; DO_C(RD()){\r\n        RC(cmd); --_RD(pos); if (cmd == 'C'){\r\n            Modify(root, L&#x5B;pos], Sum(root, L&#x5B;pos], L&#x5B;pos]) ? -1 : 1);\r\n        }\r\n        else{\r\n            OT(Sum(root, L&#x5B;pos], R&#x5B;pos]));\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p><a href=\"http:\/\/acm.hust.edu.cn\/vjudge\/problem\/viewSource.action?id=1169082\">http:\/\/acm.hust.edu.cn\/vjudge\/problem\/viewSource.action?id=1169082<\/a><\/p>\n<pre class=\"brush: cpp; collapse: true; first-line: 1; light: false; title: \u6811\u72b6\u6570\u7ec4.cpp; toolbar: true; notranslate\" title=\"\u6811\u72b6\u6570\u7ec4.cpp\">\r\nconst int N = 100009, M = 2 * N;\r\n\r\nint L&#x5B;N], R&#x5B;N], cnt;\r\n\/\/ Vertex .. .\r\nint hd&#x5B;N], nxt&#x5B;M], a&#x5B;M], b&#x5B;M];\r\n\/\/ Adjacent List .. .\r\nint C&#x5B;N];\r\n\/\/ Interval Tree\r\nint n;\r\n\r\n\r\nvoid Modify(int x, int d){\r\n    while (x &lt;= n) C&#x5B;x] += d, x += low_bit(x);\r\n}\r\n\r\nint Sum(int x){\r\n    int res = 0; while (x) res += C&#x5B;x], x ^= low_bit(x);\r\n    return res;\r\n}\r\n\r\nint Sum(int l, int r){\r\n    return Sum(r) - Sum(l-1);\r\n}\r\n\r\nint Single(int x){\r\n    int res = C&#x5B;x], p = low_bit(x); --x;\r\n    while (x &amp;&amp; low_bit(x) &lt; p){\r\n        res -= C&#x5B;x];\r\n        x ^= low_bit(x);\r\n    }\r\n    return res;\r\n}\r\n\r\n#define v b&#x5B;i]\r\n\r\ninline void dfs(int u = 0){\r\n    L&#x5B;u] = ++cnt;\r\n    for (int i=hd&#x5B;u];i;i=nxt&#x5B;i]) if (!L&#x5B;v]){\r\n        dfs(v);\r\n    }\r\n    R&#x5B;u] = cnt;\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#endif\r\n\r\n    FOR_C(i, 2, _RD(n) &lt;&lt; 1){\r\n        RD(a&#x5B;i], b&#x5B;i]), --a&#x5B;i], --b&#x5B;i], a&#x5B;i|1] = b&#x5B;i], b&#x5B;i|1] = a&#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    dfs(); REP_1(i, n) Modify(i, 1);\r\n\r\n    char cmd; int pos; DO_C(RD()){\r\n        RC(cmd); --_RD(pos); if (cmd == 'C'){\r\n            Modify(L&#x5B;pos], Single(L&#x5B;pos]) ? -1 : 1);\r\n        }\r\n        else{\r\n            OT(Sum(L&#x5B;pos], R&#x5B;pos]));\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<h3>External link: <\/h3>\n<p><a href=\"http:\/\/poj.org\/problem?id=3321\">http:\/\/poj.org\/problem?id=3321<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Brief description: \u7ed9\u5b9a\u4e00\u68f5\u6811\uff0c\u6bcf\u4e2a\u7ed3\u70b9\u6709 0\/1 \u4e24\u79cd\u72b6\u6001\uff0c\u8981\u6c42\u52a8\u6001\u7ef4\u62a4\u4ee5\u4e0b\u64cd\u4f5c\u3002\uff08\u521d\u59cb\u5168 1 .. . C x: \u5c06 x \u70b9\u7684\u72b6\u6001\u53d6\u53cd\u3002 Q x: \u8be2\u95ee\u4ee5 x \u7ed3\u70b9\u4e3a\u6839\u5b50\u6811\u4e2d\uff0c1 \u7684\u4e2a\u6570\u3002<\/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-295","post","type-post","status-publish","format-standard","hentry","category-poj"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2tdP7-4L","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/295","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=295"}],"version-history":[{"count":1,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/295\/revisions"}],"predecessor-version":[{"id":296,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/295\/revisions\/296"}],"wp:attachment":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/media?parent=295"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/categories?post=295"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/tags?post=295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}