{"id":381,"date":"2017-03-04T10:51:28","date_gmt":"2017-03-04T02:51:28","guid":{"rendered":"http:\/\/kylelv.com\/?p=381"},"modified":"2017-12-28T14:49:30","modified_gmt":"2017-12-28T06:49:30","slug":"bzoj-2733-hnoi2012%e6%b0%b8%e6%97%a0%e4%b9%a1-%e7%ba%bf%e6%ae%b5%e6%a0%91","status":"publish","type":"post","link":"https:\/\/blog.kylelv.com\/?p=381","title":{"rendered":"bzoj 2733: [HNOI2012]\u6c38\u65e0\u4e61  &#8212; \u7ebf\u6bb5\u6811"},"content":{"rendered":"<p>&nbsp;<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_73 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-69fdef45041f5\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-69fdef45041f5\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/blog.kylelv.com\/?p=381\/#2733_HNOI2012%E6%B0%B8%E6%97%A0%E4%B9%A1\" title=\"2733: [HNOI2012]\u6c38\u65e0\u4e61\">2733: [HNOI2012]\u6c38\u65e0\u4e61<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/blog.kylelv.com\/?p=381\/#Description\" title=\"Description\">Description<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/blog.kylelv.com\/?p=381\/#Input\" title=\"Input\">Input<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/blog.kylelv.com\/?p=381\/#Output\" title=\"Output\">Output<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/blog.kylelv.com\/?p=381\/#Sample_Input\" title=\"Sample Input\">Sample Input<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/blog.kylelv.com\/?p=381\/#Sample_Output\" title=\"Sample Output\">Sample Output<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/blog.kylelv.com\/?p=381\/#HINT\" title=\"HINT\">HINT<\/a><\/li><\/ul><\/nav><\/div>\n<h2 style=\"text-align: center;\"><span class=\"ez-toc-section\" id=\"2733_HNOI2012%E6%B0%B8%E6%97%A0%E4%B9%A1\"><\/span>2733: [HNOI2012]\u6c38\u65e0\u4e61<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p style=\"text-align: center;\"><span class=\"green\">Time Limit:\u00a010 Sec\u00a0\u00a0Memory Limit:\u00a0128 MB<\/span><\/p>\n<h2><span class=\"ez-toc-section\" id=\"Description\"><\/span>Description<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div class=\"content\">\n<p>\u6c38\u65e0\u4e61\u5305\u542b n \u5ea7\u5c9b\uff0c\u7f16\u53f7\u4ece 1 \u5230 n\uff0c\u6bcf\u5ea7\u5c9b\u90fd\u6709\u81ea\u5df1\u7684\u72ec\u4e00\u65e0\u4e8c\u7684\u91cd\u8981\u5ea6\uff0c\u6309\u7167\u91cd\u8981\u5ea6\u53ef \u4ee5\u5c06\u8fd9 n \u5ea7\u5c9b\u6392\u540d\uff0c\u540d\u6b21\u7528 1 \u5230 n \u6765\u8868\u793a\u3002\u67d0\u4e9b\u5c9b\u4e4b\u95f4\u7531\u5de8\u5927\u7684\u6865\u8fde\u63a5\uff0c\u901a\u8fc7\u6865\u53ef\u4ee5\u4ece\u4e00\u4e2a\u5c9b \u5230\u8fbe\u53e6\u4e00\u4e2a\u5c9b\u3002\u5982\u679c\u4ece\u5c9b a \u51fa\u53d1\u7ecf\u8fc7\u82e5\u5e72\u5ea7\uff08\u542b 0 \u5ea7\uff09\u6865\u53ef\u4ee5\u5230\u8fbe\u5c9b b\uff0c\u5219\u79f0\u5c9b a \u548c\u5c9b b \u662f\u8fde \u901a\u7684\u3002\u73b0\u5728\u6709\u4e24\u79cd\u64cd\u4f5c\uff1aB x y \u8868\u793a\u5728\u5c9b x \u4e0e\u5c9b y \u4e4b\u95f4\u4fee\u5efa\u4e00\u5ea7\u65b0\u6865\u3002Q x k \u8868\u793a\u8be2\u95ee\u5f53\u524d\u4e0e\u5c9b x\u8fde\u901a\u7684\u6240\u6709\u5c9b\u4e2d\u7b2c k \u91cd\u8981\u7684\u662f\u54ea\u5ea7\u5c9b\uff0c\u5373\u6240\u6709\u4e0e\u5c9b x \u8fde\u901a\u7684\u5c9b\u4e2d\u91cd\u8981\u5ea6\u6392\u540d\u7b2c k \u5c0f\u7684\u5c9b\u662f\u54ea \u5ea7\uff0c\u8bf7\u4f60\u8f93\u51fa\u90a3\u4e2a\u5c9b\u7684\u7f16\u53f7\u3002<\/p>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Input\"><\/span>Input<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div class=\"content\">\n<p>\u8f93\u5165\u6587\u4ef6\u7b2c\u4e00\u884c\u662f\u7528\u7a7a\u683c\u9694\u5f00\u7684\u4e24\u4e2a\u6b63\u6574\u6570 n \u548c m\uff0c\u5206\u522b \u8868\u793a\u5c9b\u7684\u4e2a\u6570\u4ee5\u53ca\u4e00\u5f00\u59cb\u5b58\u5728\u7684\u6865\u6570\u3002\u63a5\u4e0b\u6765\u7684\u4e00\u884c\u662f\u7528\u7a7a\u683c\u9694\u5f00\u7684 n \u4e2a\u6570\uff0c\u4f9d\u6b21\u63cf\u8ff0\u4ece\u5c9b 1 \u5230\u5c9b n \u7684\u91cd\u8981\u5ea6\u6392\u540d\u3002\u968f\u540e\u7684 m \u884c\u6bcf\u884c\u662f\u7528\u7a7a\u683c\u9694\u5f00\u7684\u4e24\u4e2a\u6b63\u6574\u6570 ai \u548c bi\uff0c\u8868\u793a\u4e00\u5f00\u59cb\u5c31\u5b58 \u5728\u4e00\u5ea7\u8fde\u63a5\u5c9b ai \u548c\u5c9b bi \u7684\u6865\u3002\u540e\u9762\u5269\u4e0b\u7684\u90e8\u5206\u63cf\u8ff0\u64cd\u4f5c\uff0c\u8be5\u90e8\u5206\u7684\u7b2c\u4e00\u884c\u662f\u4e00\u4e2a\u6b63\u6574\u6570 q\uff0c \u8868\u793a\u4e00\u5171\u6709 q \u4e2a\u64cd\u4f5c\uff0c\u63a5\u4e0b\u6765\u7684 q \u884c\u4f9d\u6b21\u63cf\u8ff0\u6bcf\u4e2a\u64cd\u4f5c\uff0c\u64cd\u4f5c\u7684\u683c\u5f0f\u5982\u4e0a\u6240\u8ff0\uff0c\u4ee5\u5927\u5199\u5b57\u6bcd Q \u6216B \u5f00\u59cb\uff0c\u540e\u9762\u8ddf\u4e24\u4e2a\u4e0d\u8d85\u8fc7 n \u7684\u6b63\u6574\u6570\uff0c\u5b57\u6bcd\u4e0e\u6570\u5b57\u4ee5\u53ca\u4e24\u4e2a\u6570\u5b57\u4e4b\u95f4\u7528\u7a7a\u683c\u9694\u5f00\u3002 \u5bf9\u4e8e 20%\u7684\u6570\u636e n\u22641000,q\u22641000<\/p>\n<p>\u5bf9\u4e8e 100%\u7684\u6570\u636e n\u2264100000,m\u2264n\uff0cq\u2264300000<\/p>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Output\"><\/span>Output<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div class=\"content\">\n<p>\u5bf9\u4e8e\u6bcf\u4e2a Q x k \u64cd\u4f5c\u90fd\u8981\u4f9d\u6b21\u8f93\u51fa\u4e00\u884c\uff0c\u5176\u4e2d\u5305\u542b\u4e00\u4e2a\u6574\u6570\uff0c\u8868 \u793a\u6240\u8be2\u95ee\u5c9b\u5c7f\u7684\u7f16\u53f7\u3002\u5982\u679c\u8be5\u5c9b\u5c7f\u4e0d\u5b58\u5728\uff0c\u5219\u8f93\u51fa-1\u3002<\/p>\n<\/div>\n<h2><span class=\"ez-toc-section\" id=\"Sample_Input\"><\/span>Sample Input<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div class=\"content\"><span class=\"sampledata\">5 1<br \/>\n4 3 2 5 1<br \/>\n1 2<br \/>\n7<br \/>\nQ 3 2<br \/>\nQ 2 1<br \/>\nB 2 3<br \/>\nB 1 5<br \/>\nQ 2 1<br \/>\nQ 2 4<br \/>\nQ 2 3<br \/>\n<\/span><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Sample_Output\"><\/span>Sample Output<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<div class=\"content\"><span class=\"sampledata\">-1<br \/>\n2<br \/>\n5<br \/>\n1<br \/>\n2<br \/>\n<\/span><\/div>\n<h2><span class=\"ez-toc-section\" id=\"HINT\"><\/span>HINT<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><strong>\u5e76\u67e5\u96c6\u7ef4\u62a4\u4e00\u4e0b\u8fde\u901a\u6027\uff0c\u5bf9\u4e8e\u6bcf\u4e2a\u8fde\u901a\u5757\u5efa\u4e00\u9897\u6743\u503c\u7ebf\u6bb5\u6811\uff0c\u7136\u540e\u641e\u7ebf\u6bb5\u6811\u5408\u5e76\u3002<\/strong><\/p>\n<pre class=\"lang:c++ decode:true\">#include&lt;cstdio&gt;\r\n#include&lt;iostream&gt;\r\nusing namespace std;\r\n#define N 100010\r\n#define M 1800100\r\ninline int read()\r\n{\r\n    int x=0,f=1;char ch=getchar();\r\n    while(ch&lt;'0'||ch&gt;'9'){if(ch=='-')f=-1;ch=getchar();}\r\n    while(ch&gt;='0'&amp;&amp;ch&lt;='9'){x=x*10+ch-'0';ch=getchar();}\r\n    return x*f;\r\n}\r\nint fa[N];\r\nint findf(int x){return x==fa[x]?x:fa[x]=findf(fa[x]);}\r\nint rt[N],ls[M],rs[M],sum[M],cnt;\r\nvoid add(int l,int r,int &amp;y,int v)\r\n{\r\n    if(!y) y=++cnt;\r\n    if(l==r){sum[y]=1;return;}\r\n    int mid=(l+r)&gt;&gt;1;\r\n    if(v&gt;mid) add(mid+1,r,rs[y],v);\r\n    else add(l,mid,ls[y],v);\r\n    sum[y]=sum[ls[y]]+sum[rs[y]];\r\n}\r\nint que(int l,int r,int y,int k)\r\n{\r\n    if(l==r) return l;\r\n    int mid=(l+r)&gt;&gt;1;\r\n    if(sum[ls[y]]&gt;=k) return que(l,mid,ls[y],k);\r\n    else return que(mid+1,r,rs[y],k-sum[ls[y]]);\r\n}\r\nint lj(int x,int y)\r\n{\r\n    if(!x) return y;\r\n    if(!y) return x;\r\n    ls[x]=lj(ls[x],ls[y]);\r\n    rs[x]=lj(rs[x],rs[y]);\r\n    sum[x]=sum[ls[x]]+sum[rs[x]];\r\n    return x;\r\n}\r\nint n,m,a,b,q,v[N],di[N],x,k,ans;\r\nchar cc;\r\nint main()\r\n{\r\n    n=read();m=read();\r\n    for(int i=1;i&lt;=n;i++) v[i]=read();\r\n    for(int i=0;i&lt;=n;i++) fa[i]=i;\r\n    for(int i=0;i&lt;m;i++)\r\n    {\r\n        a=read();b=read();\r\n        a=findf(a);b=findf(b);\r\n        fa[a]=b;\r\n    }\r\n    for(int i=1;i&lt;=n;i++)\r\n    {\r\n        add(1,n,rt[findf(i)],v[i]);\r\n        di[v[i]]=i;\r\n    }\r\n    q=read();\r\n    while(q--)\r\n    {\r\n        cc=getchar();\r\n        while(cc&lt;'A'||cc&gt;'Z') cc=getchar();\r\n        x=read();k=read();\r\n        if(cc=='Q')\r\n        {\r\n            a=findf(x);\r\n            if(sum[rt[a]]&lt;k){puts(\"-1\");continue;}\r\n            ans=que(1,n,rt[a],k);\r\n            printf(\"%d\\n\",di[ans]);\r\n        }\r\n        else \r\n        {\r\n            a=findf(x);b=findf(k);\r\n            if(a!=b)\r\n            {\r\n                fa[a]=b;\r\n                rt[b]=lj(rt[a],rt[b]);\r\n            }\r\n        }\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; 2733: [HNOI2012]\u6c38\u65e0\u4e61 Time Limit:\u00a010 Sec\u00a0\u00a0Memory L [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[10],"class_list":["post-381","post","type-post","status-publish","format-standard","hentry","category-bzoj","tag-10"],"_links":{"self":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/381","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=381"}],"version-history":[{"count":2,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/381\/revisions"}],"predecessor-version":[{"id":383,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/381\/revisions\/383"}],"wp:attachment":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=381"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}