{"id":274,"date":"2010-06-05T15:10:00","date_gmt":"2010-06-05T07:10:00","guid":{"rendered":"http:\/\/localhost\/?p=274"},"modified":"2010-06-05T15:10:00","modified_gmt":"2010-06-05T07:10:00","slug":"ceoi2006queue","status":"publish","type":"post","link":"https:\/\/www.shuizilong.com\/wjmzbmr\/?p=274","title":{"rendered":"[Ceoi2006]Queue"},"content":{"rendered":"\n<p>[Ceoi2006]Queue<\/p>\n<p>Time Limit:10000MS  Memory Limit:65536K<br \/>Total Submit:17 Accepted:12 <br \/>Case Time Limit:1000MS<\/p>\n<p><strong>Description <\/strong><\/p>\n<p>If you are an average football fan, you probably know that obtaining tickets for this year&rsquo;s World Cup in Germany was an impossible task. Greedy organizers and football federations grabbed the majority or available tickets and divided them among sponsors, friends and family members. As a result, jet setters have flooded the venues, while die-hard fans sit at home enjoying the games between advertisements featuring crappy beer and sugar-free chewing gum. <br \/>A couple of tickets are left for the final game and a huge queue has formed in front of the ticker office. As fans were arriving, they were labeled with successive integers. The first person in queue was labeled with number one, the second with number two, etc. <br \/>Since the fans arrived the night before, they had to wait for a long time before the ticket counter was open and, naturally, some of them had to use the restroom. Each time a person needs to use the restroom, he\/she steps out of the queue and, and, after completing the task, steps back into the queue, though not necessarily at the same position as before. Since there is only one restroom available no person leaves the queue before the previous person has returned (hence, at any moment there is at most one person missing from the queue). <br \/>During the course of the night, a total of N restroom visits have occurred. Each visit is described by two positive integers A and B, denoting that the person labeled A stepped out of the queue and the stepped back into the queue immediately in front of the person labeled B. Now that all the visits have completed, the officials have to answer a series of questions. Each question is either of the form &lsquo;P X&rsquo;, asking for the position lf the person labeled X, or of the form &lsquo;L X&rsquo;, asking for the label of the person at position X. <br \/>The first person in queue is considered to be at position one, the second at position two, etc. <br \/>Write a program that, given the description of the visits and a number of questions, answers all of the questions. <\/p>\n<p>\u4e00\u5f00\u59cb\u5c06\u4e00\u5217\u4eba\u6309\u7f16\u53f7\u4ece1\u5f00\u59cb\u987a\u5e8f\u6392\u5217\uff0c\u7136\u540e\u8fdb\u884cN\u6b21\u64cd\u4f5c\uff0c\u6bcf\u6b21\u5c06\u7f16\u53f7\u4e3aA\u7684\u4eba\u62ff\u51fa\uff0c\u653e\u5728\u7f16\u53f7\u4e3aB\u7684\u4eba\u524d\u9762\uff0c\u6240\u6709\u64cd\u4f5c\u8fdb\u884c\u5b8c\u540e\u6709Q\u4e2a\u95ee\u9898\uff0c\u8be2\u95ee\u7f16\u53f7\u4e3aX\u7684\u4eba\u7684\u4f4d\u7f6e\uff0c\u6216\u8005\u8be2\u95ee\u5728X\u53f7\u4f4d\u7f6e\u4e0a\u7684\u4eba\u7684\u7f16\u53f7\u3002 <\/p>\n<p><strong>Input <\/strong><\/p>\n<p>The first line of input contains an integer N(2 \u2264 N \u2264 50 000)- the total number of restroom visits. <br \/>Each of the following N lines contains two different integers A and B (1 \u2264 A, B \u2264 109), describing one restroom visit. The nest line contains an integer Q (1 \u2264 Q \u2264 50 000) &ndash; the total number of questions. <br \/>Each of the following Q lines contains a single character (either the uppercase letter &lsquo;P&rsquo; or the uppercase letter &lsquo;L&rsquo;) and an integer X (1\u2264 X \u2264109) , describing one question. <\/p>\n<p><strong>Output <\/strong><\/p>\n<p>The output should consist of a total of Q lines. <br \/>The ith line or output should contain a single integer R &ndash; the answer to the ith question. <br \/>If the corresponding question is of the form &lsquo;P Xi&rsquo; then R should be the final position of the person labeled Xi. <br \/>If the corresponding question is of the form &lsquo;L Xi&rsquo; then R should be the label of the person at position Xi. <\/p>\n<p>Grading <br \/>Partial credit is awarded for incorrect solutions that correctly answer all questions of one type. If all questions of the form &lsquo;P X&rsquo; are answered correctly or if all questions of the form &lsquo;L X&rsquo; are answered correctly, you will receive 50% of the corresponding test case. <br \/>In order to receive partial credit, the output should be formatted according to the specifications. Therefore, even if you choose to answer only one type of questions, you should still produce output for all questions of the other type. <\/p>\n<p><strong>Sample Input <\/strong><\/p>\n<\/p>\n<p><strong>Sample Output <\/strong><\/p>\n<\/p>\n<p><strong>Source <br \/>\u989d\u3002\u3002\u3002\u8fd9\u9898\u5b9e\u9645\u4e0a\u53ea\u8981\u6ce8\u610f\u5230\u4e00\u79cd\u6280\u672f\u53eb\u5b9e\u65f6\u79bb\u6563\u5316\uff0c\u7531\u4e8e\u8303\u56f4\u5f88\u5927\u800cn\u5f88\u5c0f\uff0c\u5f88\u591a\u5757\u90fd\u662f\u8fde\u7eed\u7684\u3002\u3002\u90a3\u4e48\u4fe1\u606f\u53ef\u4ee5\u5ffd\u7565\uff0c\u53ea\u9700\u8981\u8bb0\u5f55\u4e00\u4e9b\u8f6c\u89d2\u5730\u65b9\u7684\u70b9\u524d\u9a71\u548c\u540e\u7ee7\u5c31\u53ef\u4ee5\u4e86\u3002\u3002\u7136\u540e\u4e00\u904d\u626b\u63cf\u8fc7\u53bb\u5c31\u53ef\u4ee5\u5f97\u51fa\u6240\u6709\u7684\u7b54\u6848\u4e86\u3002\u3002\u6211\u7684\u7a0b\u5e8f\u9b3c\u901f\u5230\u4e86\u6781\u70b9\u3002\u3002\u56e7\u554a\u3002\u3002<br \/>Code\uff1a<br \/>#include &lt;vector&gt;<\/strong><br \/>#include &lt;algorithm&gt;<br \/>#include &lt;utility&gt;<br \/>#include &lt;cstdio&gt;<br \/>#include &lt;map&gt;<br \/>#define rep(i,n) for(int i=0;i&lt;n;i++)<br \/>#define pb push_back<br \/>#define All(x) x.begin(),x.end()<br \/>const int inf=2e9,maxn=50000+10;<br \/>using namespace std;<br \/>map&lt;int,int&gt; Pre,Next;<br \/>int n,m,Ans[maxn];<br \/>int pre(int x){return Pre.count(x)?Pre[x]:x-1;}<br \/>int next(int x){return Next.count(x)?Next[x]:x+1;}<br \/>typedef pair&lt;int,int&gt; ii;<br \/>typedef vector&lt;ii&gt;::iterator it;<br \/>vector&lt;ii&gt; L,P;<br \/>void init()<br \/>{<br \/>&nbsp;&nbsp;&nbsp;  scanf(&quot;%d&quot;,&amp;n);int a,b;<br \/>&nbsp;&nbsp;&nbsp;  rep(i,n)<br \/>&nbsp;&nbsp;&nbsp;  {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  scanf(&quot;%d%d&quot;,&amp;a,&amp;b);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  Next[pre(a)]=next(a);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  Pre[next(a)]=pre(a);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  Next[pre(b)]=a;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  Pre[a]=pre(b);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  Next[a]=b;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  Pre[b]=a;<br \/>&nbsp;&nbsp;&nbsp;  }<br \/>&nbsp;&nbsp;&nbsp;  Next[inf]=inf+1;<br \/>&nbsp;&nbsp;&nbsp;  char c;scanf(&quot;%d&quot;,&amp;m);<br \/>&nbsp;&nbsp;&nbsp;  rep(i,m)<br \/>&nbsp;&nbsp;&nbsp;  {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  scanf(&quot; %c %d&quot;,&amp;c,&amp;a);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  if(c==&#8217;P&#8217;)P.pb(ii(a,i));<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  else L.pb(ii(a,i));<br \/>&nbsp;&nbsp;&nbsp;  }<br \/>&nbsp;&nbsp;&nbsp;  L.pb(ii(inf,m));P.pb(ii(inf,m));<br \/>&nbsp;&nbsp;&nbsp;  sort(All(L));sort(All(P));<br \/>}<br \/>void Solve()<br \/>{<br \/>&nbsp;&nbsp;&nbsp;  int now=0,Min,pos=0;<br \/>&nbsp;&nbsp;&nbsp;  while(now&lt;inf)<br \/>&nbsp;&nbsp;&nbsp;  {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  int a=Next.lower_bound(now)-&gt;first-now;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  int b=lower_bound(All(L),ii(pos,0))-&gt;first-pos;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  int c=lower_bound(All(P),ii(now,0))-&gt;first-now;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  Min=a&lt;?b&lt;?c;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  now+=Min;pos+=Min;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  if(Min==b)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  it l=lower_bound(All(L),ii(pos,0)),r=lower_bound(All(L),ii(pos+1,0));<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  for(it i=l;i!=r;i++)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  Ans[i-&gt;second]=now;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  }<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  if(Min==c)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  it l=lower_bound(All(P),ii(now,0)),r=lower_bound(All(P),ii(now+1,0));<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  for(it i=l;i!=r;i++)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  Ans[i-&gt;second]=pos;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  }<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  now=next(now);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  ++pos;<br \/>&nbsp;&nbsp;&nbsp;  }<br \/>&nbsp;&nbsp;&nbsp;  rep(i,m)printf(&quot;%dn&quot;,Ans[i]);<br \/>}<br \/>int main()<br \/>{<br \/>&nbsp;&nbsp;&nbsp;  init();Solve();<br \/>}<\/p>\n<p>103154411000009999926 39 68L 1L 2L 3L 4P 1P 2P 3P 4Output12961256Input57 22 79 710 110005 99959L 1P 2L 2P 7L 7P 9P 10P 99999P 100000 <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[Ceoi2006]Queue Time Limit:10000MS Memory Limit:65536KTotal Submit:17 Accepted:12 Case Time Limit:1000MS Description If you are an average football fan, you probably know that obtaining tickets for this year&rsquo;s World Cup in Germany was an impossible task. Greedy organizers and football federations grabbed the majority or available tickets and divided them among sponsors, friends and family members. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[10],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.shuizilong.com\/wjmzbmr\/index.php?rest_route=\/wp\/v2\/posts\/274"}],"collection":[{"href":"https:\/\/www.shuizilong.com\/wjmzbmr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shuizilong.com\/wjmzbmr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shuizilong.com\/wjmzbmr\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shuizilong.com\/wjmzbmr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=274"}],"version-history":[{"count":0,"href":"https:\/\/www.shuizilong.com\/wjmzbmr\/index.php?rest_route=\/wp\/v2\/posts\/274\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shuizilong.com\/wjmzbmr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shuizilong.com\/wjmzbmr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=274"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shuizilong.com\/wjmzbmr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}