{"id":232,"date":"2016-11-09T15:45:36","date_gmt":"2016-11-09T07:45:36","guid":{"rendered":"http:\/\/kylelv.com\/?p=232"},"modified":"2017-12-13T12:45:52","modified_gmt":"2017-12-13T04:45:52","slug":"bzoj-1636-usaco2007-janbalanced-lineup-%e7%ba%bf%e6%ae%b5%e6%a0%91","status":"publish","type":"post","link":"https:\/\/blog.kylelv.com\/?p=232","title":{"rendered":"bzoj 1636: [Usaco2007 Jan]Balanced Lineup  &#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-69fdeecb07f8d\" 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-69fdeecb07f8d\"  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=232\/#1636_Usaco2007_JanBalanced_Lineup\" title=\"1636: [Usaco2007 Jan]Balanced Lineup\">1636: [Usaco2007 Jan]Balanced Lineup<\/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=232\/#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=232\/#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=232\/#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=232\/#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=232\/#Sample_Output\" title=\"Sample Output\">Sample Output<\/a><\/li><\/ul><\/nav><\/div>\n<h2 style=\"text-align: center;\"><span class=\"ez-toc-section\" id=\"1636_Usaco2007_JanBalanced_Lineup\"><\/span>1636: [Usaco2007 Jan]Balanced Lineup<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><span class=\"green\">Time Limit:\u00a05 Sec\u00a0\u00a0Memory Limit:\u00a064 MB<\/span><\/p>\n<p>&nbsp;<\/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>For the daily milking, Farmer John&#8217;s N cows (1 &lt;= N &lt;= 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height. Farmer John has made a list of Q (1 &lt;= Q &lt;= 200,000) potential groups of cows and their heights (1 &lt;= height &lt;= 1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the group.<\/p>\n<p>\u6bcf\u5929,\u519c\u592b John \u7684N(1 &lt;= N &lt;= 50,000)\u5934\u725b\u603b\u662f\u6309\u540c\u4e00\u5e8f\u5217\u6392\u961f. \u6709\u4e00\u5929, John\u00a0\u51b3\u5b9a\u8ba9\u4e00\u4e9b\u725b\u4eec\u73a9\u4e00\u573a\u98de\u76d8\u6bd4\u8d5b. \u4ed6\u51c6\u5907\u627e\u4e00\u7fa4\u5728\u5bf9\u5217\u4e2d\u4e3a\u7f6e\u8fde\u7eed\u7684\u725b\u6765\u8fdb\u884c\u6bd4\u8d5b.\u00a0\u4f46\u662f\u4e3a\u4e86\u907f\u514d\u6c34\u5e73\u60ac\u6b8a,\u725b\u7684\u8eab\u9ad8\u4e0d\u5e94\u8be5\u76f8\u5dee\u592a\u5927.\u00a0John \u51c6\u5907\u4e86Q (1 &lt;= Q &lt;= 180,000) \u4e2a\u53ef\u80fd\u7684\u725b\u7684\u9009\u62e9\u548c\u6240\u6709\u725b\u7684\u8eab\u9ad8 (1 &lt;=\u00a0\u8eab\u9ad8 &lt;= 1,000,000). \u4ed6\u60f3\u77e5\u9053\u6bcf\u4e00\u7ec4\u91cc\u9762\u6700\u9ad8\u548c\u6700\u4f4e\u7684\u725b\u7684\u8eab\u9ad8\u5dee\u522b.<\/p>\n<p><em id=\"__mceDel\"><\/em><em id=\"__mceDel\">\u6ce8\u610f: \u5728\u6700\u5927\u6570\u636e\u4e0a, \u8f93\u5165\u548c\u8f93\u51fa\u5c06\u5360\u7528\u5927\u90e8\u5206\u8fd0\u884c\u65f6\u95f4.\u00a0<\/em><\/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>* Line 1: Two space-separated integers, N and Q. * Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i * Lines N+2..N+Q+1: Two integers A and B (1 &lt;= A &lt;= B &lt;= N), representing the range of cows from A to B inclusive.<\/p>\n<div>\u7b2c1\u884c\uff1aN,Q<\/div>\n<div>\u7b2c2\u5230N+1\u884c\uff1a\u6bcf\u5934\u725b\u7684\u8eab\u9ad8<\/div>\n<div>\u7b2cN+2\u5230N+Q+1\u884c\uff1a\u4e24\u4e2a\u6574\u6570A\u548cB\uff0c\u8868\u793a\u4eceA\u5230B\u7684\u6240\u6709\u725b\u3002\uff081&lt;=A&lt;=B&lt;=N\uff09<\/div>\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<div>6 3<\/div>\n<div>1<\/div>\n<div>7<\/div>\n<div>3<\/div>\n<div>4<\/div>\n<div>2<\/div>\n<div>5<\/div>\n<div>1 5<\/div>\n<div>4 6<\/div>\n<div>2 2<\/div>\n<div><\/div>\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\">* Lines 1..Q: Each line contains a single integer that is a response<br \/>\nto a reply and indicates the difference in height between the<br \/>\ntallest and shortest cow in the range.<br \/>\n<\/span><\/div>\n<div class=\"content\"><\/div>\n<div class=\"content\"><span class=\"sampledata\">\u8f93\u51fa\u6bcf\u884c\u4e00\u4e2a\u6570\uff0c\u4e3a\u6700\u5927\u6570\u4e0e\u6700\u5c0f\u6570\u7684\u5dee<\/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\">6<br \/>\n3<br \/>\n0<\/span><\/div>\n<div><\/div>\n<div><\/div>\n<div>\u7ebf\u6bb5\u6811\u88f8\u9898\u3002\u3002\u3002<\/div>\n<div><\/div>\n<div>\n<pre class=\"lang:default decode:true \">#include&lt;cstdio&gt;\r\n#include &lt;iostream&gt;\r\n#define M 50010\r\nint max(int a,int b){return a&gt;b?a:b;}\r\nint min(int a,int b){return a&lt;b?a:b;}\r\nstruct tree{int l,r,minn,maxx;}tr[4*M];\r\nint a[M];\r\nvoid make(int l,int r,int p)\r\n{\r\n    tr[p].l=l;\r\n    tr[p].r=r;\r\n    if(l==r){\r\n        tr[p].minn=a[l];\r\n        tr[p].maxx=a[l];\r\n        return ;\r\n    }\r\n    int mid=(l+r)&gt;&gt;1;\r\n    make(l,mid,p&lt;&lt;1);\r\n    make(mid+1,r,p&lt;&lt;1|1);\r\n    tr[p].minn=min(tr[p&lt;&lt;1].minn,tr[p&lt;&lt;1|1].minn);\r\n    tr[p].maxx=max(tr[p&lt;&lt;1].maxx,tr[p&lt;&lt;1|1].maxx);\r\n}\r\nint fmin(int l,int r,int x)\r\n{\r\n    if(tr[x].l==l&amp;&amp;tr[x].r==r) return tr[x].minn; \r\n    int mid=(tr[x].l+tr[x].r)&gt;&gt;1,q=x&lt;&lt;1; \r\n    if(r&lt;=mid) return fmin(l,r,q); \r\n    else if(l&gt;mid) return fmin(l,r,q+1); \r\n    else return min(fmin(l,mid,q),fmin(mid+1,r,q+1)); \r\n}\r\nint fmax(int l,int r,int x)\r\n{\r\n    if(tr[x].l==l&amp;&amp;tr[x].r==r) return tr[x].maxx; \r\n    int mid=(tr[x].l+tr[x].r)&gt;&gt;1; \r\n    if(r&lt;=mid) return fmax(l,r,x&lt;&lt;1); \r\n    else if(l&gt;mid) return fmax(l,r,x&lt;&lt;1|1); \r\n    else return max(fmax(l,mid,x&lt;&lt;1),fmax(mid+1,r,x&lt;&lt;1|1)); \r\n}\r\nint main()\r\n{\r\n    int n,m,i,x,y;\r\n    scanf(\"%d%d\",&amp;n,&amp;m);\r\n    for(i=1;i&lt;=n;i++) scanf(\"%d\",&amp;a[i]);    \r\n    make(1,n,1);\r\n    for(i=0;i&lt;m;i++){\r\n        scanf(\"%d%d\",&amp;x,&amp;y);\r\n        printf(\"%d\\n\",fmax(x,y,1)-fmin(x,y,1));\r\n    }\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; 1636: [Usaco2007 Jan]Balanced Lineup Time Limit: [&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-232","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\/232","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=232"}],"version-history":[{"count":2,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/232\/revisions"}],"predecessor-version":[{"id":245,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=\/wp\/v2\/posts\/232\/revisions\/245"}],"wp:attachment":[{"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.kylelv.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}