{"id":611,"date":"2010-12-26T02:31:23","date_gmt":"2010-12-25T18:31:23","guid":{"rendered":"http:\/\/www.shuizilong.com\/house\/?p=611"},"modified":"2012-12-26T04:57:17","modified_gmt":"2012-12-25T20:57:17","slug":"noi-2004-%e9%83%81%e9%97%b7%e7%9a%84%e5%87%ba%e7%ba%b3%e5%91%98","status":"publish","type":"post","link":"https:\/\/www.shuizilong.com\/house\/archives\/noi-2004-%e9%83%81%e9%97%b7%e7%9a%84%e5%87%ba%e7%ba%b3%e5%91%98\/","title":{"rendered":"NOI 2004. \u90c1\u95f7\u7684\u51fa\u7eb3\u5458"},"content":{"rendered":"<h3>Brief description: <\/h3>\n<p><!--more--><\/p>\n<h3>Analysis: <\/h3>\n<p>&#8230;<\/p>\n<pre class=\"brush: cpp; collapse: true; first-line: 1; light: false; title: NOI_2004_Cashier.cpp; toolbar: true; notranslate\" title=\"NOI_2004_Cashier.cpp\">\r\n\/* .................................................................................................................................. *\/\r\n \r\n\/** Header **\/\r\n \r\n#include &lt;cstdio&gt;\r\n#include &lt;cstring&gt;\r\n#define DO(N) while(N--)\r\n \r\ntemplate&lt;class T&gt; inline void RD(T &amp;x){ \/\/ ... :&quot; I am I\/O Accelerator ... Use me at your own risk ;) ... &quot; ..\r\n   \/\/scanf(&quot;%d&quot;, &amp;x);\r\n     char c; c = getchar(); x = c - '0';\r\n     for (c = getchar(); c &gt;= '0'; c = getchar()) x = x * 10 + c - '0';\r\n}\r\ntemplate&lt;class T&gt; inline void RD(T &amp;x0, T &amp;x1){RD(x0), RD(x1);}\r\ntemplate&lt;class T&gt; inline void RST(T &amp;A){memset(A, 0, sizeof(A));}\r\ntemplate&lt;class T&gt; inline void RST(T &amp;A0, T &amp;A1){RST(A0), RST(A1);}\r\ntemplate&lt;class T&gt; inline void RST(T &amp;A0, T &amp;A1, T &amp;A2){RST(A0), RST(A1), RST(A2);}\r\ntemplate&lt;class T&gt; inline void checkMin(T &amp;a, T b){if (b&lt;a) a=b;}\r\n \r\nconst int INF = 0x7fffffff;\r\nconst int N = 100001;\r\n \r\nint l&#x5B;N], r&#x5B;N], p&#x5B;N], ky&#x5B;N], sz&#x5B;N];\r\nint root, total;\r\nint n, b, d, ans;\r\n \r\n \r\ninline void Rotate(int x){\r\n    int y = p&#x5B;x], z = p&#x5B;y], lc = l&#x5B;x], rc = r&#x5B;x];\r\n    if (y == l&#x5B;z]) l&#x5B;z] = x; else r&#x5B;z] = x;\r\n    if (x == l&#x5B;y]) {\r\n        p&#x5B;p&#x5B;p&#x5B;rc] = y] = x] = z, l&#x5B;r&#x5B;x] = y] = rc;\r\n        sz&#x5B;x] = sz&#x5B;y], sz&#x5B;y] -= sz&#x5B;lc] + 1;\r\n    }\r\n    else {\r\n        p&#x5B;p&#x5B;p&#x5B;lc] = y] = x] = z, r&#x5B;l&#x5B;x] = y] = lc;\r\n        sz&#x5B;x] = sz&#x5B;y], sz&#x5B;y] -= sz&#x5B;rc] + 1;\r\n    }\r\n}\r\n \r\ninline void Splay(int x){\r\n    while (p&#x5B;x]) Rotate(x);\r\n    root = x;\r\n}\r\n \r\nvoid Insert(int val){\r\n    val -= b - d; if (val &lt; d) return;\r\n    int x = root, y = 0; while (x != 0){\r\n        ++sz&#x5B;y = x];\r\n        if (val &lt; ky&#x5B;x]) x = l&#x5B;x];\r\n        else x = r&#x5B;x];\r\n    }\r\n \r\n    x = ++total, ky&#x5B;x] = val, sz&#x5B;x] = 1, p&#x5B;x] = y;\r\n    if (val &lt; ky&#x5B;y]) l&#x5B;y] = x; else r&#x5B;y] = x;\r\n \r\n    Splay(x);\r\n}\r\n \r\nint Search(){\r\n    int val = d, res = 0, x = root; while (x != 0){\r\n        if (val &gt; ky&#x5B;x]) x = r&#x5B;x];\r\n        else {\r\n            if (ky&#x5B;x] &lt;= ky&#x5B;res]) res = x;\r\n            x = l&#x5B;x];\r\n        }\r\n    }\r\n    return res;\r\n}\r\n \r\nint Select(int k){\r\n    int x = root; while (sz&#x5B;l&#x5B;x]] + 1 != k){\r\n        if (sz&#x5B;l&#x5B;x]] + 1 &lt; k) k -= sz&#x5B;l&#x5B;x]] + 1, x = r&#x5B;x];\r\n        else x = l&#x5B;x];\r\n    }\r\n    Splay(x); return x;\r\n}\r\n \r\nint main(){\r\n \r\n    \/\/freopen(&quot;in.txt&quot;, &quot;r&quot;, stdin);\r\n    \/\/freopen(&quot;out.txt&quot;, &quot;w&quot;, stdout);\r\n \r\n#define gc getchar()\r\n \r\n    while (scanf(&quot;%d %d&quot;, &amp;n, &amp;b)  != EOF){\r\n \r\n        gc; RST(p, l, r); RST(ky, sz); root = total = ans = d = 0;\r\n        Insert(INF), ky&#x5B;0] = INF; int t; DO(n){\r\n \r\n            char cmd = gc; gc; \/\/scanf(&quot; %c&quot;, &amp;cmd);\r\n            RD(t); switch (cmd) {\r\n            case 'I':\r\n                Insert(t);\r\n                break;\r\n            case 'A':\r\n                d -= t;\r\n                break;\r\n            case 'S':\r\n                d += t, Splay(Search());\r\n                ans += sz&#x5B;l&#x5B;root]], sz&#x5B;root] -= sz&#x5B;l&#x5B;root]], l&#x5B;root] = 0;\r\n                break;\r\n            default:\r\n                if (t &gt;= sz&#x5B;root]) puts(&quot;-1&quot;);\r\n                else printf(&quot;%d\\n&quot;, ky&#x5B;Select(sz&#x5B;root] - t)] + b - d);\r\n            }\r\n        }\r\n        printf(&quot;%d\\n&quot;, ans);\r\n    }\r\n}\r\n<\/pre>\n<h3>External link: <\/h3>\n<p><a href=\"http:\/\/acm.hit.edu.cn\/judge\/show.php?Proid=1819&#038;Contestid=0\">http:\/\/acm.hit.edu.cn\/judge\/show.php?Proid=1819&#038;Contestid=0<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Brief description:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[1],"tags":[],"class_list":["post-611","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2tdP7-9R","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/611","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/comments?post=611"}],"version-history":[{"count":0,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/611\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/media?parent=611"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/categories?post=611"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/tags?post=611"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}