{"id":58,"date":"2011-08-30T05:18:06","date_gmt":"2011-08-29T21:18:06","guid":{"rendered":"http:\/\/www.shuizilong.com\/house\/?p=58"},"modified":"2023-05-01T16:26:45","modified_gmt":"2023-05-01T08:26:45","slug":"spoj-375-query-on-a-tree","status":"publish","type":"post","link":"https:\/\/www.shuizilong.com\/house\/archives\/spoj-375-query-on-a-tree\/","title":{"rendered":"SPOJ 375. Query on a tree"},"content":{"rendered":"<h3>Brief description :<\/h3>\n<p>\u52a8\u6001\u7ef4\u62a4\u6811\u4e2d\u4e24\u70b9\u4e4b\u95f4\u8def\u5f84\u4e0a\u8fb9\u6743\u7684\u6700\u5927\u503c\u3002<\/p>\n<p>Tags: Classical<\/p>\n<h3>Analysis :<\/h3>\n<p>\u7ecf\u5178\u95ee\u9898.. . \u6811\u94fe\u5256\u5206 300- \u884c\uff0c3.10+ s<br \/>\n\u52a8\u6001\u6811 150- \u884c\u3002\u3002\u3002 2.90+ s&#8230;<\/p>\n<p>\uff08\u76ee\u524d Yang Zhe \u4f5c\u4e1a\u91cc\u7684 \u5168\u5c40\u5e73\u8861\u4e8c\u53c9\u6811 \u8fd8\u4e0d\u77e5\u9053\u600e\u4e48\u5b9e\u73b0\u3002>_&lt;\u3002\uff09<\/p>\n<p><a href=\"http:\/\/paste.ubuntu.com\/1125993\/\">\u6811\u94fe\u5256\u5206\uff08\u3002\u3002\u8fc7\u53bb\u7684\u5199\u6cd5\u3002\u3002\u30023s+-\u3002\u3002\u3002<\/a><br \/>\n\u4e0a\u9762\u662f\u6211\u6700\u5f00\u59cb\u65f6\u7684\u5199\u6cd5\u3002\u3002\u5199\u7684\u975e\u5e38\u5c0f\u5fc3\u8c28\u614e\u3002\u3002\u3002\u4e0a\u6765\u5c31\u5206\u522b\u5bf9\u7ed3\u70b9\u3001\u8fb9\u3001\u8def\u5f84\u5404\u5b9a\u4e49\u4e86\u4e00\u7ec4\u7ed3\u6784\u4f53\u3002\u3002\u3002\uff08Vertex, Edge, Path &#8230; \u7136\u540e\u5168\u7a0b\u6307\u9488\u6307\u6765\u6307\u53bb\u3002\u3002<br \/>\n\u70b9\u548c\u8fb9\u5404\u6709\u4e00\u4e2a host \u6307\u9488\u3002\u3002\uff08\u70b9\u5bc4\u5bbf\u5728\u8fb9\u4e0a\u3002\u3002\u8fb9\u5bc4\u5bbf\u5728\u91cd\u8def\u5f84\u4e0a\u3002\u3002\u3002<br \/>\n\u3002\u3002\u8def\u5f84\u7684\u672c\u4f53\u662f\u4e00\u68f5\u7ebf\u6bb5\u6811\uff0c\uff08\u3002\u3002\u4e3a\u4e86\u5237\u699c\u8fd8\u7279\u5730\u5199\u6210\u4e86\u81ea\u4e0b\u5411\u4e0a\u7ef4\u62a4\u7684\u5f62\u5f0f\u3002\uff08\u56e0\u4e3a\u6709\u526a\u679d\u3002\u3002\u3002\u3002<br \/>\n\u3002\u7136\u540e\u8bb0\u5f55\u5176\u6700\u9ad8\u70b9\u7684 Vertex \u7684\u6307\u9488\u3002\u3002\uff08\u3002head\u3002\u3002\uff08\u3002\u3002\u8df3\u94fe\u548c\u8be2\u95ee\u7684\u65f6\u5019\u8ba1\u7b97 offset \u90fd\u8981\u7528\u5230\u3002\u3002\u3002<br \/>\n\u3002\u3002<\/p>\n<p><a href=\"http:\/\/paste.ubuntu.com\/1615760\/\">\u6811\u94fe\u5256\u5206\uff08\u3002\u3002\u3002\u73b0\u5728\u7684\u5199\u6cd5\u3002\u30023s+-\u3002\u3002\u3002<\/a><br \/>\n.. \u53ea\u4fdd\u7559\u7684 path \u8fd9\u4e00\u4e2a\u7ed3\u6784\u4f53\u3002\u3002\u76f4\u63a5\u8bb0\u5f55\u6bcf\u4e2a\u70b9\u6240\u5728\u7684\u8def\u5f84\u3002\u3002\u3002<\/p>\n<p>\/** ` Micro Mezzo Macro Flation &#8212; Overheated Economy ., **\/<\/p>\n<p>#include <iostream><br \/>\n#include <cstring><br \/>\n#include <cstdio><br \/>\n#include <cmath><br \/>\n#include <vector><\/p>\n<p>using namespace std;<\/p>\n<p>#define REP(i, n) for (int i=0;i<int(n);++i)\n#define FOR(i, a, b) for (int i=int(a);i<int(b);++i)\n#define REP_1(i, n) for (int i=1;i<=int(n);++i)\n#define FOR_C(i, a, b) for (int b____=int(b),i=a;i<b____;++i)\n#define DO(n) while(n--)\n#define SZ(A) int(A.size())\n#define PB push_back\n#define MP(A, B) make_pair(A, B)\n#define Rush int T____; RD(T____); DO(T____)\n#pragma comment(linker, \"\/STACK:36777216\")\n#pragma GCC optimize (\"O2\")\n\ntemplate<class T> inline void RD(T &#038;);<br \/>\ntemplate<class T> inline void OT(const T &#038;);<br \/>\ntemplate<class T> inline T&#038; _RD(T &#038;x){ RD(x); return x;}<br \/>\ninline void RS(char *s){scanf(&#8220;%s&#8221;, s);}<br \/>\ntemplate<class T0, class T1> inline void RD(T0 &#038;x0, T1 &#038;x1){RD(x0), RD(x1);}<br \/>\ntemplate<class T0, class T1, class T2> inline void RD(T0 &#038;x0, T1 &#038;x1, T2 &#038;x2){RD(x0), RD(x1), RD(x2);}<br \/>\ntemplate<class T> inline void RST(T &#038;A){memset(A, 0, sizeof(A));}<br \/>\ntemplate<class T0, class T1, class T2, class T3> inline void RST(T0 &#038;A0, T1 &#038;A1, T2 &#038;A2, T3 &#038;A3){RST(A0), RST(A1), RST(A2), RST(A3);}<br \/>\ntemplate<class T> inline void FLC(T &#038;A, int x){memset(A, x, sizeof(A));}<br \/>\ntemplate<class T> inline void CLR(T &#038;A){A.clear();}<br \/>\ntemplate<class T> inline void checkMax(T &#038;a,const T b){if (b>a) a=b;}<br \/>\ninline int _1(int i){return 1<<i;}\ntemplate<class T> inline void RD(T &#038;x){char c; for (c = getchar(); c < '0'; c = getchar()); x = c - '0'; for (c = getchar(); c >= &#8216;0&#8217;; c = getchar()) x = x * 10 + c &#8211; &#8216;0&#8217;;}<br \/>\ntemplate<class T> inline void OT(const T &#038;x){printf(&#8220;%d\\n&#8221;, x);}<br \/>\ntemplate<class T> inline T max(T a, T b, T c){return max(max(a, b), c);}<br \/>\n\/* &#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;. *\/<br \/>\nconst int N = 10001, M = 2 * N;<\/p>\n<p>int l[N], r[N], p[N], w0[N], w1[N]; bool rt[N];<br \/>\n\/\/ Link-cut tree<br \/>\nint hd[N], nxt[M], a[M], b[M], w[M], h[M\/2];<br \/>\n\/\/ Adjacent list<\/p>\n<p>int n, ans;<\/p>\n<p>#define lx l[x]<br \/>\n#define rx r[x]<\/p>\n<p>inline void Update(int x){<br \/>\n    w1[x] = max(w1[lx], w1[rx], w0[x]);<br \/>\n}<\/p>\n<p>inline void Set(int l[], int y, int x){<br \/>\n    l[y] = x, p[x] = y;<br \/>\n}<\/p>\n<p>inline void Rotate(int x){<br \/>\n    int y = p[x], z = p[y];<\/p>\n<p>    if (!rt[y]) Set(y == l[z] ? l : r, z, x);<br \/>\n    else p[x] = z;<\/p>\n<p>    if (x == l[y]) Set(l, y, rx), Set(r, x, y);<br \/>\n    else Set(r, y, lx), Set(l, x, y);<\/p>\n<p>    if (rt[y]) rt[y] = false, rt[x] = true; \/\/rt[0] = true;<br \/>\n    Update(y); \/\/Update(x);<\/p>\n<p>}<\/p>\n<p>inline void Splay(int x){<br \/>\n    while (!rt[x]) Rotate(x);<br \/>\n}<\/p>\n<p>void Access(int x){<br \/>\n    int y = 0; do{<br \/>\n        Splay(x);<br \/>\n        rt[rx] = true, rt[rx = y] = false, Update(x);<br \/>\n        x = p[y = x];<br \/>\n    } while (x);<br \/>\n}<\/p>\n<p>\/\/ public:<\/p>\n<p>void Query(int x, int y){<br \/>\n    Access(y), y = 0; do{<br \/>\n        Splay(x); if (!p[x]) OT(max(w1[rx], w1[y]));<br \/>\n        rt[rx] = true, rt[rx = y] = false, Update(x);<br \/>\n        x = p[y = x];<br \/>\n    } while (x);<br \/>\n}<\/p>\n<p>void Modify(int x, int val){<br \/>\n    Splay(x), w0[x] = val;<br \/>\n}<\/p>\n<p>#define v b[i]<br \/>\n#define w w[i]<\/p>\n<p>inline void dfs(int u = 1){<br \/>\n    for(int i=hd[u];i;i=nxt[i]) if (!p[v]){<br \/>\n        p[v] = u, w0[v] = w, dfs(h[i>>1] = v);<br \/>\n    }<br \/>\n}<\/p>\n<p>#undef x<br \/>\n#undef w<\/p>\n<p>int main(){<\/p>\n<p>    \/\/freopen(&#8220;in.txt&#8221;, &#8220;r&#8221;, stdin);<br \/>\n    \/\/freopen(&#8220;out.txt&#8221;, &#8220;w&#8221;, stdout);<br \/>\n    \/\/ios::sync_with_stdio(false);<\/p>\n<p>    Rush{<br \/>\n        \/\/ Initializing Phase &#8230;<\/p>\n<p>        FOR_C(i, 2, _RD(n) << 1){\n            RD(a[i], b[i], w[i]), a[i|1] = b[i], b[i|1] = a[i], w[i|1] = w[i];\n            nxt[i] = hd[a[i]], hd[a[i]] = i; ++i;\n            nxt[i] = hd[a[i]], hd[a[i]] = i;\n        }\n\n        FLC(rt, true), p[1] = -1, dfs(), p[1] = 0;\n\n        \/\/ Interaction Phase ...\n\n        int a, b; char cmd[5];\n\n        while (true){\n            RS(cmd); if (cmd[0] == 'C') RD(a, b), Modify(h[a], b);\n            else if (cmd[0] == 'Q') RD(a, b), Query(a, b);\n            else break;\n        }\n\n        \/\/ Rececling ....\n\n        RST(hd, p, l, r);\n    }\n}\n[\/cpp]\n\n\n\n<h3>External link:<\/h3>\n<p><a href=\"https:\/\/www.spoj.com\/problems\/QTREE\/\">https:\/\/www.spoj.com\/problems\/QTREE\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Brief description : \u52a8\u6001\u7ef4\u62a4\u6811\u4e2d\u4e24\u70b9\u4e4b\u95f4\u8def\u5f84\u4e0a\u8fb9\u6743\u7684\u6700\u5927\u503c\u3002 Tags: Classical Analysis : \u7ecf\u5178\u95ee\u9898.. . \u6811\u94fe\u5256\u5206 300- \u884c\uff0c3.10+ s \u52a8\u6001\u6811 150- \u884c\u3002\u3002\u3002 2.90+ s&#8230; \uff08\u76ee\u524d Yang Zhe \u4f5c\u4e1a\u91cc\u7684 \u5168\u5c40\u5e73\u8861\u4e8c\u53c9\u6811 \u8fd8\u4e0d\u77e5\u9053\u600e\u4e48\u5b9e\u73b0\u3002>_&lt;\u3002\uff09 \u6811\u94fe\u5256\u5206\uff08\u3002\u3002\u8fc7\u53bb\u7684\u5199\u6cd5\u3002\u3002\u30023s+-\u3002\u3002\u3002 \u4e0a\u9762\u662f\u6211\u6700\u5f00\u59cb\u65f6\u7684\u5199\u6cd5\u3002\u3002\u5199\u7684\u975e\u5e38\u5c0f\u5fc3\u8c28\u614e\u3002\u3002\u3002\u4e0a\u6765\u5c31\u5206\u522b\u5bf9\u7ed3\u70b9\u3001\u8fb9\u3001\u8def\u5f84\u5404\u5b9a\u4e49\u4e86\u4e00\u7ec4\u7ed3\u6784\u4f53\u3002\u3002\u3002\uff08Vertex, Edge, Path &#8230; \u7136\u540e\u5168\u7a0b\u6307\u9488\u6307\u6765\u6307\u53bb\u3002\u3002 \u70b9\u548c\u8fb9\u5404\u6709\u4e00\u4e2a host \u6307\u9488\u3002\u3002\uff08\u70b9\u5bc4\u5bbf\u5728\u8fb9\u4e0a\u3002\u3002\u8fb9\u5bc4\u5bbf\u5728\u91cd\u8def\u5f84\u4e0a\u3002\u3002\u3002 \u3002\u3002\u8def\u5f84\u7684\u672c\u4f53\u662f\u4e00\u68f5\u7ebf\u6bb5\u6811\uff0c\uff08\u3002\u3002\u4e3a\u4e86\u5237\u699c\u8fd8\u7279\u5730\u5199\u6210\u4e86\u81ea\u4e0b\u5411\u4e0a\u7ef4\u62a4\u7684\u5f62\u5f0f\u3002\uff08\u56e0\u4e3a\u6709\u526a\u679d\u3002\u3002\u3002\u3002 \u3002\u7136\u540e\u8bb0\u5f55\u5176\u6700\u9ad8\u70b9\u7684 Vertex \u7684\u6307\u9488\u3002\u3002\uff08\u3002head\u3002\u3002\uff08\u3002\u3002\u8df3\u94fe\u548c\u8be2\u95ee\u7684\u65f6\u5019\u8ba1\u7b97 offset \u90fd\u8981\u7528\u5230\u3002\u3002\u3002 \u3002\u3002 \u6811\u94fe\u5256\u5206\uff08\u3002\u3002\u3002\u73b0\u5728\u7684\u5199\u6cd5\u3002\u30023s+-\u3002\u3002\u3002 .. \u53ea\u4fdd\u7559\u7684 path \u8fd9\u4e00\u4e2a\u7ed3\u6784\u4f53\u3002\u3002\u76f4\u63a5\u8bb0\u5f55\u6bcf\u4e2a\u70b9\u6240\u5728\u7684\u8def\u5f84\u3002\u3002\u3002 \/** ` Micro Mezzo Macro Flation &#8212; Overheated Economy [&hellip;]<\/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":[21],"tags":[39],"class_list":["post-58","post","type-post","status-publish","format-standard","hentry","category-spoj","tag-39"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2tdP7-W","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/58","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=58"}],"version-history":[{"count":3,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/58\/revisions"}],"predecessor-version":[{"id":2704,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/58\/revisions\/2704"}],"wp:attachment":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/media?parent=58"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/categories?post=58"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/tags?post=58"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}