{"id":186,"date":"2018-11-15T13:26:10","date_gmt":"2018-11-15T13:26:10","guid":{"rendered":"http:\/\/nocriz.com\/?p=186"},"modified":"2021-09-08T20:20:07","modified_gmt":"2021-09-08T12:20:07","slug":"186","status":"publish","type":"post","link":"https:\/\/nocriz.com\/?p=186","title":{"rendered":"\u300cBZOJ3757\u300d\u82f9\u679c\u6811"},"content":{"rendered":"<p>\u795e\u7287\u5bb6\u95e8\u53e3\u79cd\u4e86\u4e00\u68f5\u82f9\u679c\u6811\u3002\u82f9\u679c\u6811\u4f5c\u4e3a\u4e00\u68f5\u6811\uff0c\u5f53\u7136\u662f\u5448\u6811\u72b6\u7ed3\u6784\uff0c\u6bcf\u6839\u6811\u679d\u8fde\u63a5\u4e24\u4e2a\u82f9\u679c\uff0c\u6bcf\u4e2a\u82f9\u679c\u90fd\u53ef\u4ee5\u6cbf\u7740\u4e00\u6761\u7531\u6811\u679d\u6784\u6210\u7684\u8def\u5f84\u8fde\u5230\u6811\u6839\uff0c\u800c\u4e14\u8fd9\u6837\u7684\u8def\u5f84\u53ea\u5b58\u5728\u4e00\u6761\u3002\u7531\u4e8e\u8fd9\u68f5\u82f9\u679c\u6811\u662f\u795e\u7287\u79cd\u7684\uff0c\u6240\u4ee5\u82f9\u679c\u90fd\u53d1\u751f\u4e86\u53d8\u5f02\uff0c\u53d8\u6210\u4e86\u5404\u79cd\u5404\u6837\u7684\u989c\u8272\u3002\u6211\u4eec\u7528\u4e00\u4e2a\u5230n\u4e4b\u95f4\u7684\u6b63\u6574\u6570\u6765\u8868\u793a\u4e00\u79cd\u989c\u8272\u3002\u6811\u4e0a\u4e00\u5171\u6709n\u4e2a\u82f9\u679c\u3002\u6bcf\u4e2a\u82f9\u679c\u90fd\u88ab\u7f16\u4e86\u53f7\u7801\uff0c\u53f7\u7801\u4e3a\u4e00\u4e2a1\u5230n\u4e4b\u95f4\u7684\u6b63\u6574\u6570\u3002\u6211\u4eec\u75280\u4ee3\u8868\u6811\u6839\u3002\u53ea\u4f1a\u6709\u4e00\u4e2a\u82f9\u679c\u76f4\u63a5\u6839\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u6709\u8bb8\u8bb8\u591a\u591a\u7684\u4eba\u6765\u795e\u7287\u5bb6\u91cc\u819c\u62dc\u795e\u7287\u3002\u53ef\u795e\u7287\u53ef\u4e0d\u662f\u968f\u4fbf\u5c31\u80fd\u819c\u62dc\u7684\u3002\u524d\u6765\u819c\u62dc\u795e\u7287\u7684\u4eba\u9700\u8981\u6b63\u786e\u56de\u7b54\u4e00\u4e2a\u95ee\u9898\uff0c\u624d\u80fd\u8fdb\u5c4b\u819c\u62dc\u795e\u7287\u3002\u8fd9\u4e2a\u95ee\u9898\u5c31\u662f\uff0c\u4ece\u6811\u4e0a\u7f16\u53f7\u4e3au\u7684\u82f9\u679c\u51fa\u53d1\uff0c\u7531\u6811\u679d\u8d70\u5230\u7f16\u53f7\u4e3av\u7684\u82f9\u679c\uff0c\u8def\u5f84\u4e0a\u7ecf\u8fc7\u7684\u82f9\u679c\u4e00\u5171\u6709\u591a\u5c11\u79cd\u4e0d\u540c\u7684\u989c\u8272\uff08\u5305\u62ec\u82f9\u679cu\u548c\u82f9\u679cv\u7684\u989c\u8272\uff09\uff1f\u4e0d\u8fc7\u795e\u7287\u6ce8\u610f\u5230\uff0c\u6709\u4e9b\u6765\u819c\u62dc\u7684\u4eba\u60a3\u6709\u8272\u76f2\u75c7\u3002\u5177\u4f53\u5730\u8bf4\uff0c\u4e00\u4e2a\u4eba\u53ef\u80fd\u4f1a\u8ba4\u4e3a\u989c\u8272a\u5c31\u662f\u989c\u8272b\uff0c\u90a3\u4e48\u4ed6\u4eec\u5728\u6570\u82f9\u679c\u7684\u989c\u8272\u65f6\uff0c\u5982\u679c\u65e2\u51fa\u73b0\u4e86\u989c\u8272a\u7684\u82f9\u679c\uff0c\u53c8\u51fa\u73b0\u4e86\u989c\u8272b\u7684\u82f9\u679c\uff0c\u8fd9\u4e2a\u4eba\u53ea\u4f1a\u7b97\u5165\u989c\u8272b\uff0c\u800c\u4e0d\u4f1a\u628a\u989c\u8272a\u7b97\u8fdb\u6765\u3002<\/p>\n<p>\u795e\u7287\u662f\u4e00\u4e2a\u597d\u4eba\uff0c\u4ed6\u4e0d\u4f1a\u5f3a\u4eba\u6240\u96be\uff0c\u4e5f\u5c31\u4f1a\u63a5\u53d7\u7531\u4e8e\u8272\u76f2\u75c7\u5bfc\u81f4\u7684\u7b54\u6848\u9519\u8bef\uff08\u5f53\u7136\u7b54\u6848\u5728\u8272\u76f2\u73af\u5883\u4e0b\u4e5f\u5fc5\u987b\u662f\u6b63\u786e\u7684\uff09\u3002\u4e0d\u8fc7\u8fd9\u6837\u795e\u7287\u4e5f\u5c31\u8981\u66f4\u6539\u4ed6\u539f\u5148\u6570\u989c\u8272\u7684\u7a0b\u5e8f\u4e86\u3002\u867d\u7136\u8fd9\u5bf9\u4e8e\u795e\u7287\u6765\u8bf4\u662f\u5c0f\u83dc\u4e00\u789f\uff0c\u4f46\u662f\u4ed6\u60f3\u8003\u9a8c\u4e00\u4e0b\u4f60\u3002\u4f60\u80fd\u66ff\u795e\u7287\u5b8c\u6210\u8fd9\u9879\u4efb\u52a1\u5417\uff1f<\/p>\n<h3>Input<\/h3>\n<p>\u8f93\u5165\u7b2c\u4e00\u884c\u4e3a\u4e24\u4e2a\u6574\u6570n\u548cm\uff0c\u5206\u522b\u4ee3\u8868\u6811\u4e0a\u82f9\u679c\u7684\u4e2a\u6570\u548c\u524d\u6765\u819c\u62dc\u7684\u4eba\u6570\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\u7684\u4e00\u884c\u5305\u542bn\u4e2a\u6570\uff0c\u7b2ci\u4e2a\u6570\u4ee3\u8868\u7f16\u53f7\u4e3ai\u7684\u82f9\u679c\u7684\u989c\u8272Coli\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\u6709n\u884c\uff0c\u6bcf\u884c\u5305\u542b\u4e24\u4e2a\u6570x\u548cy\uff0c\u4ee3\u8868\u6709\u4e00\u6839\u6811\u679d\u8fde\u63a5\u4e86\u82f9\u679cx\u548cy\uff08\u6216\u8005\u6839\u548c\u4e00\u4e2a\u82f9\u679c\uff09\u3002<\/p>\n<p>\u63a5\u4e0b\u6765\u6709m\u884c\uff0c\u6bcf\u884c\u5305\u542b\u56db\u4e2a\u6574\u6570u\u3001v\u3001a\u548cb\uff0c\u4ee3\u8868\u8fd9\u4e2a\u4eba\u8981\u6570\u82f9\u679cu\u5230\u82f9\u679cv\u7684\u989c\u8272\u79cd\u6570\uff0c\u540c\u65f6\u8fd9\u4e2a\u4eba\u8ba4\u4e3a\u989c\u8272a\u5c31\u662f\u989c\u8272b\u3002\u5982\u679ca=b=0\uff0c\u5219\u4ee3\u8868\u8fd9\u4e2a\u4eba\u6ca1\u6709\u60a3\u8272\u76f2\u75c7\u3002<\/p>\n<h3>Output<\/h3>\n<p>\u8f93\u51fa\u4e00\u5171m\u884c\uff0c\u6bcf\u884c\u4ec5\u5305\u542b\u4e00\u4e2a\u6574\u6570\uff0c\u4ee3\u8868\u8fd9\u4e2a\u4eba\u5e94\u8be5\u6570\u51fa\u7684\u989c\u8272\u79cd\u6570\u3002<\/p>\n<h3>Sample Input<\/h3>\n<p>5 3<\/p>\n<p>1 1 3 3 2<\/p>\n<p>0 1<\/p>\n<p>1 2<\/p>\n<p>1 3<\/p>\n<p>2 4<\/p>\n<p>3 5<\/p>\n<p>1 4 0 0<\/p>\n<p>1 4 1 3<\/p>\n<p>1 4 1 2<\/p>\n<h3>Sample Output<\/h3>\n<p>2<\/p>\n<p>1<\/p>\n<p>2<\/p>\n<h3>HINT<\/h3>\n<p>0&lt;=x,y,a,b&lt;=N<\/p>\n<p>N&lt;=50000<\/p>\n<p>1&lt;=U,V,Coli&lt;=N<\/p>\n<p>\u6811\u4e0a\u83ab\u961f\uff0c\u53ef\u53c2\u7167\u8fd9\u7bc7<a href=\"https:\/\/blog.csdn.net\/kuribohG\/article\/details\/41458639\">\u4e0d\u9519\u7684\u535a\u5ba2<\/a><\/p>\n<pre class=\"theme:obsidian lang:default decode:true \">#include &lt;queue&gt;\n#include &lt;cmath&gt;\n#include &lt;cstdio&gt;\n#include &lt;vector&gt;\n#include &lt;cstring&gt;\n#include &lt;iostream&gt;\n#include &lt;algorithm&gt;\nusing namespace std;\ntemplate&lt;typename T&gt; void read(T &amp;x){\n\tx = 0;char ch = getchar();int f = 1;\n\twhile(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}\n\twhile(isdigit(ch)){x = x*10+ch-48;ch=getchar();}\n\tx*=f;\n}\n\nconst int N = 100050;\nint n,m,va[N],u,v,tim = 0,dfn[N],depth[N],fff[N][16],buc[N] = {0},taken[N] = {0},ans = 0,blc,rans[N];\nvector&lt;int&gt; G[N];\nvoid dfs(int num,int fa = -1){\n\ttim++;\n\tdfn[num] = tim;\n\tfff[num][0] = fa;\n\tfor(int i=1;i&lt;=15;i++)fff[num][i] = fff[fff[num][i-1]][i-1];\n\tfor(int i=0;i&lt;G[num].size();i++){\n\t\tint ct = G[num][i];\n\t\tif(ct == fa)continue;\n\t\tdepth[ct] = depth[num]+1;\n\t\tdfs(ct,num);\n\t}\n}\nint lca(int u,int v){\n\tif(depth[u]&lt;depth[v])swap(u,v);\n\tfor(int i=15;i&gt;=0;i--)if(fff[u][i]!=-1 &amp;&amp; depth[fff[u][i]] &gt;=depth[v])u = fff[u][i];\n\tfor(int i=15;i&gt;=0;i--)if(fff[u][i]!=-1&amp;&amp;fff[v][i]!=-1&amp;&amp;fff[u][i]!=fff[v][i])u=fff[u][i],v=fff[v][i];\n\tif(u == v)return u;\n\treturn fff[u][0];\n}\nstruct query{\n\tint l,r,a,b,id;\n\tbool operator &lt; (const query &amp;rhs) const{\n\t\tif(dfn[l]\/blc!=dfn[rhs.l]\/blc)\n\t\t\treturn dfn[l]\/blc&lt;dfn[rhs.l]\/blc;\n\t\treturn dfn[r]&lt;dfn[rhs.r];\n\t}\n}Q[N];\n\ninline void chg(int x){\n\tif(taken[x]){\n\t\tif(buc[va[x]] == 1)ans--;\n\t\tbuc[va[x]]--;\n\t\ttaken[x] = 0;\n\t}else{\n\t\tif(buc[va[x]] == 0)ans++;\n\t\tbuc[va[x]]++;\n\t\ttaken[x] = 1;\n\t}\n}\nvoid mov(int u,int v){\n\tif(depth[u]&lt;depth[v])swap(u,v);\n\twhile(depth[u]&gt;depth[v]){\n\t\tchg(u);\n\t\tu = fff[u][0];\n\t}\n\twhile(u!=v){\n\t\tchg(u);chg(v);\n\t\tu = fff[u][0];\n\t\tv = fff[v][0];\n\t}\n}\nint main() {\n\tread(n);read(m);\n\tblc = sqrt(n)+1;\n\tfor(int i=1;i&lt;=n;i++)read(va[i]);\n\tfor(int i=1;i&lt;=n;i++){\n\t\tread(u);read(v);\n\t\tG[u].push_back(v);\n\t\tG[v].push_back(u);\n\t}\n\tdepth[0] = 0;\n\tdfs(0);\n\tfor(int i=0;i&lt;m;i++){\n\t\tread(Q[i].l);read(Q[i].r);read(Q[i].a);read(Q[i].b);Q[i].id = i;\n\t\tif(dfn[Q[i].l]&gt;dfn[Q[i].r])swap(Q[i].l,Q[i].r);\n\t}\n\tsort(Q,Q+m);\n\tint ccl = 0,ccr = 0;\n\tfor(int i=0;i&lt;m;i++){\n\t\tint cl = Q[i].l,cr = Q[i].r;\n\t\tmov(ccl,cl);\n\t\tmov(ccr,cr);\n\t\tccl = cl;ccr = cr;\n\t\tcl = lca(ccl,ccr);\n\t\tchg(cl);\n\t\trans[Q[i].id] = ans-(Q[i].a!=Q[i].b&amp;&amp;buc[Q[i].a] &amp;&amp; buc[Q[i].b]);\n\t\tchg(cl);\n\t}\n\tfor(int i=0;i&lt;m;i++)cout&lt;&lt;rans[i]&lt;&lt;'\\n';\n\treturn 0;\n}\n<\/pre>\n<p>&nbsp;<\/p>\n ","protected":false},"excerpt":{"rendered":"<p>\u795e\u7287\u5bb6\u95e8\u53e3\u79cd\u4e86\u4e00\u68f5\u82f9\u679c\u6811\u3002\u82f9\u679c\u6811\u4f5c\u4e3a\u4e00\u68f5\u6811\uff0c\u5f53\u7136\u662f\u5448\u6811\u72b6\u7ed3\u6784\uff0c\u6bcf\u6839\u6811\u679d\u8fde\u63a5\u4e24\u4e2a\u82f9\u679c\uff0c\u6bcf\u4e2a\u82f9\u679c\u90fd\u53ef\u4ee5\u6cbf\u7740\u4e00\u6761\u7531\u6811 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[11,12,15],"_links":{"self":[{"href":"https:\/\/nocriz.com\/index.php?rest_route=\/wp\/v2\/posts\/186"}],"collection":[{"href":"https:\/\/nocriz.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nocriz.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nocriz.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/nocriz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=186"}],"version-history":[{"count":1,"href":"https:\/\/nocriz.com\/index.php?rest_route=\/wp\/v2\/posts\/186\/revisions"}],"predecessor-version":[{"id":760,"href":"https:\/\/nocriz.com\/index.php?rest_route=\/wp\/v2\/posts\/186\/revisions\/760"}],"wp:attachment":[{"href":"https:\/\/nocriz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nocriz.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=186"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nocriz.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=186"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}