{"id":354,"date":"2010-08-22T20:11:00","date_gmt":"2010-08-22T12:11:00","guid":{"rendered":"http:\/\/localhost\/?p=354"},"modified":"2010-08-22T20:11:00","modified_gmt":"2010-08-22T12:11:00","slug":"sgu_311_ice-cream_tycoon","status":"publish","type":"post","link":"https:\/\/www.shuizilong.com\/wjmzbmr\/?p=354","title":{"rendered":"SGU 311. Ice-cream Tycoon"},"content":{"rendered":"<p> 311. Ice-cream TycoonTime limit per test: 3 second(s)<br \/>Memory limit: 65536 kilobytesinput: standard<br \/>output: standard<\/p>\n<p>You&#8217;ve recently started an ice-cream business in a local school. During a day you have many suppliers delivering the ice-cream for you, and many students buying it from you. You are not allowed to set the prices, as you are told the price for each piece of ice-cream by the suppliers.<\/p>\n<p>The day is described with a sequence of queries. Each query can be eitherARRIVE <em>n<\/em> <em>c<\/em>, meaning that a supplier has delivered&#160;<em>n<\/em>&#160;pieces of ice-cream priced<em>c<\/em>&#160;each to you, or&#160;BUY <em>n<\/em> <em>t<\/em>, meaning that a student wants to buy&#160;<em>n<\/em>&#160;pieces of ice-cream, having a total of&#160;<em>t<\/em>&#160;money. The latter is processed as follows: in case<em>n<\/em>&#160;cheapest pieces of ice-cream you have cost no more than&#160;<em>t<\/em>&#160;(together), you sell those&#160;<em>n<\/em>&#160;cheapest pieces to the student; in case they cost more, she gets nothing. You start the day with no ice-cream.<\/p>\n<p>For each student, output&#160;HAPPY&#160;if she gets her ice-cream, and&#160;UNHAPPYif she doesn&#8217;t.<\/p>\n<p><strong>Input<\/strong>The input file contains between 1 and 10<sup>5<\/sup>&#160;queries (inclusive), each on a separate line. The queries are formatted as described above, either&#160;ARRIVE <em>n<\/em> <em>c<\/em>or&#160;BUY <em>n<\/em> <em>t<\/em>, 1 \u2264&#160;<em>n<\/em>,&#160;<em>c<\/em>&#160;\u2264 10<sup>6<\/sup>, 1 \u2264&#160;<em>t<\/em>&#160;\u2264 10<sup>12<\/sup>.<\/p>\n<p><strong>Output<\/strong>For each&#160;BUY-query output one line, containing either the word&#160;HAPPY&#160;or the word&#160;UNHAPPY&#160;(answers should be in the same order as the corresponding queries).<\/p>\n<p><strong>Example(s)<\/strong>                                    sample input                                    sample output                                                    ARRIVE 1 1<br \/>ARRIVE 10 200<br \/>BUY 5 900<br \/>BUY 5 900<br \/>BUY 5 1000                                    HAPPY<br \/>UNHAPPY<br \/>HAPPY                        <br \/>\u8fd9\u4e2a\u9898\u76ee\u4e00\u770b\u5c31\u662f\u88f8\u7684\u7ebf\u6bb5\u6811\u56e7\u3002\u3002\u5c31\u662f\u8001TLE\u7011\u5e03\u6c57\u3002\u3002\u3002\u8fd9\u6b21\u6211\u706b\u4e86\u3002\u3002\u5199\u4e86\u4e2a\u5e26\u6807\u8bb0\u7684\u975e\u9012\u5f52<br \/>\u7ebf\u6bb5\u6811\u7ec8\u4e8eA\u4e86\u5185\u6d41\u6ee1\u9762\u56e7\u3002\u3002\u3002<br \/>Code\uff1a<br \/>#include &lt;vector&gt;<br \/>#include &lt;algorithm&gt;<br \/>#include &lt;utility&gt;<br \/>#include &lt;iostream&gt;<br \/>#include &lt;cstdio&gt;<br \/>#include &lt;cmath&gt;<br \/>#include &lt;cstdlib&gt;<br \/>#include &lt;cstring&gt;<br \/>#include &lt;time.h&gt;<br \/>#define rep(i,n) for(int i=0;i&lt;n;i++)<br \/>#define pb push_back<br \/>#define Debug(x) cout&lt;&lt;#x&lt;&lt;&quot;=&quot;&lt;&lt;x&lt;&lt;endl;<br \/>#define For(i,l,r) for(int i=l;i&lt;=r;i++)<br \/>#define tr(e,x) for(typeof(x.begin()) e=x.begin();e!=x.end();e++)<br \/>#define printTime cout&lt;&lt;&quot;Time:&quot;&lt;&lt;pre-clock()&lt;&lt;endl;pre=clock();<br \/>const int inf=~0U&gt;&gt;1,maxc=1&lt;&lt;20;<br \/>using namespace std;<br \/>typedef long long ll;<br \/>char cmd[100];<br \/>#define scan(t)<br \/>{<br \/>    char pt;<br \/>    while(pt=getchar(),pt&lt;&#8216;0&#8217;||pt&gt;&#8217;9&#8217;);<br \/>    t=pt-&#8216;0&#8217;;<br \/>    while(pt=getchar(),pt&gt;=&#8217;0&#8217;&amp;&amp;pt&lt;=&#8217;9&#8242;)t=t*10+pt-&#8216;0&#8217;;<br \/>}<br \/>bool Mark[1&lt;&lt;21];<br \/>ll sum[1&lt;&lt;21]={},num[1&lt;&lt;21]={};<br \/>void set(int t)<br \/>{<br \/>    sum[t]=sum[t*2]+sum[t*2+1];<br \/>    num[t]=num[t*2]+num[t*2+1];<br \/>}<br \/>void MarkIt(int t)<br \/>{<br \/>    sum[t]=num[t]=0;<br \/>    Mark[t]=true;<br \/>}<br \/>void PushDown(int t)<br \/>{<br \/>    if(Mark[t])<br \/>        MarkIt(t*2),MarkIt(t*2+1);<br \/>    Mark[t]=false;<br \/>}<br \/>void Update(int t,ll c)<br \/>{<br \/>    int i,l,r;<br \/>    for(i=1,l=0,r=maxc;i&lt;maxc;)<br \/>    {<br \/>        PushDown(i);i&lt;&lt;=1;<br \/>        int m=l+r&gt;&gt;1;<br \/>        if(t&gt;=m)i++,l=m;<br \/>        else r=m;<br \/>    }<br \/>    i=t+maxc;sum[i]+=t*c;num[i]+=c;<br \/>    for(i&gt;&gt;=1;i;i&gt;&gt;=1)set(i);<br \/>}<br \/>ll get(int n)<br \/>{<br \/>    if(num[1]&lt;n)return -1;<br \/>    ll ret=0;int i;<br \/>    for(i=1;i&lt;maxc;)<br \/>    {<br \/>        PushDown(i);i&lt;&lt;=1;<br \/>        if(num[i]&gt;=n)continue;<br \/>        ret+=sum[i];n-=num[i];i++;<br \/>    }<br \/>    return ll(n)*(i-maxc)+ret;<br \/>}<br \/>void clear(int n)<br \/>{<br \/>    int i;<br \/>    for(i=1;i&lt;maxc;)<br \/>    {<br \/>        PushDown(i);i&lt;&lt;=1;<br \/>        if(num[i]&gt;=n)continue;<br \/>        n-=num[i];MarkIt(i);i++;<br \/>    }<br \/>    Update(i-maxc,-n);<br \/>}<br \/>int main()<br \/>{<br \/>    \/\/freopen(&quot;in&quot;,&quot;r&quot;,stdin);<br \/>    int n;ll c;<br \/>    while(scanf(&quot;%s&quot;,cmd)==1)<br \/>    {<br \/>        scan(n);scan(c);<br \/>        if(cmd[0]==&#8217;A&#8217;)Update(c,n);<br \/>        else<br \/>        {<br \/>            ll tmp=get(n);<br \/>            if(tmp==-1||tmp&gt;c)<br \/>                puts(&quot;UNHAPPY&quot;);<br \/>            else<br \/>                puts(&quot;HAPPY&quot;),clear(n);<br \/>        }<br \/>    }<br \/>} <\/p>\n","protected":false},"excerpt":{"rendered":"<p>311. Ice-cream TycoonTime limit per test: 3 second(s)Memory limit: 65536 kilobytesinput: standardoutput: standard You&#8217;ve recently started an ice-cream business in a local school. During a day you have many suppliers delivering the ice-cream for you, and many students buying it from you. You are not allowed to set the prices, as you are told the [&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\/354"}],"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=354"}],"version-history":[{"count":0,"href":"https:\/\/www.shuizilong.com\/wjmzbmr\/index.php?rest_route=\/wp\/v2\/posts\/354\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shuizilong.com\/wjmzbmr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=354"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shuizilong.com\/wjmzbmr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=354"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shuizilong.com\/wjmzbmr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=354"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}