{"id":161,"date":"2012-04-01T20:29:14","date_gmt":"2012-04-01T12:29:14","guid":{"rendered":"http:\/\/www.shuizilong.com\/house\/?p=161"},"modified":"2012-04-01T20:29:14","modified_gmt":"2012-04-01T12:29:14","slug":"sgu-505-prefixes-and-suffixes","status":"publish","type":"post","link":"https:\/\/www.shuizilong.com\/house\/archives\/sgu-505-prefixes-and-suffixes\/","title":{"rendered":"SGU 505. Prefixes and suffixes"},"content":{"rendered":"<h3>Brief description :<\/h3>\n<p>\u7ed9\u5b9a n \u4e2a\u5b57\u7b26\u4e32\uff0c\u8be2\u95ee\u5176\u4e2d\u4ee5\u7ed9\u5b9a\u4e32\u4e3a\u524d\u7f00\uff0c\u7ed9\u5b9a\u4e32\u4e3a\u540e\u7f00\u7684\u5b57\u4e32\u6570\u76ee\uff0c\u8be2\u95ee\u6709 m \u4e2a\u3002<br \/>\n\uff08 n, m <= 100000 )\n<!--more--><\/p>\n<h3>Analysis :<\/h3>\n<p>\u5206\u522b\u5bf9\u539f\u548c\u548c\u9006\u4e32\u8fdb\u884c\u6392\u5e8f\uff0c\u5219\u539f\u9898\u7b49\u4ef7\u4e3a\u7ed9\u5b9a\u4e00\u4e2a\u6392\u5217\uff0c\u67e5\u8be2\u4e0b\u6807\u5728 [l, r] \u4e4b\u95f4\uff0c\u9879 [a, b] \u4e4b\u95f4\u7684\u6570\u76ee\u3002<br \/>\n\u79bb\u7ebf\u6811\u72b6\u6570\u7ec4\u53ef\u505a\u3002<\/p>\n<pre class=\"brush: cpp; light: false; title: ; toolbar: true; notranslate\" title=\"\">\r\nconst int N = 100009;\r\n\r\nstring S&#x5B;N], R&#x5B;N];\r\nint A&#x5B;N], ans&#x5B;N], n, m;\r\n\r\nstruct BIT{\r\n    int C&#x5B;N];\r\n    BIT(){RST(C);}\r\n    void modify(int x){++x; do ++C&#x5B;x], x += low_bit(x); while (x &lt;= n);}\r\n    int operator&#x5B;](int x){++x; int s = 0; do s += C&#x5B;x], x -= low_bit(x); while(x); return s;}\r\n} T;\r\n\r\nstruct Event{\r\n    int l, r, d, id;\r\n    Event(int _l, int _r, int _d, int _id):l(_l), r(_r), d(_d), id(_id){}\r\n    void process(){ans&#x5B;id] += (T&#x5B;r] - T&#x5B;l]) * d;}\r\n};\r\n\r\nvector&lt;Event&gt; E&#x5B;N];\r\n\r\nvoid locate(string S&#x5B;], string x, int &amp;l, int &amp;r){\r\n    l = lower_bound(S, S + n, x) - S - 1, ++x&#x5B;SZ(x)-1];\r\n    r = lower_bound(S, S + n, x) - S - 1;\r\n}\r\n\r\nint main(){\r\n\r\n    \/\/freopen(&quot;in.txt&quot;, &quot;r&quot;, stdin);\r\n\r\n    REP_C(i, _RD(n)){\r\n        cin &gt;&gt; S&#x5B;i], R&#x5B;i] = S&#x5B;i], reverse(ALL(R&#x5B;i]));\r\n    }\r\n\r\n    sort(S, S + n), sort(R, R + n);\r\n\r\n    REP(i, n){\r\n        string t = S&#x5B;i]; reverse(ALL(t));\r\n        A&#x5B;i] = lower_bound(R, R + n, t) - R;\r\n    }\r\n\r\n    string pre, suf; int l, r, a, b;\r\n    REP_C(i, _RD(m)){\r\n        cin &gt;&gt; pre &gt;&gt; suf; locate(S, pre, l, r); if (l == r) continue;\r\n        reverse(ALL(suf)); locate(R, suf, a, b); if (a == b) continue;\r\n        E&#x5B;r].PB(Event(a, b, 1, i));\r\n        if (l &gt;= 0) E&#x5B;l].PB(Event(a, b, -1, i));\r\n    }\r\n\r\n    REP(i, n){\r\n        T.modify(A&#x5B;i]); ECH(it, E&#x5B;i]) it-&gt;process();\r\n    }\r\n\r\n    REP(i, m) OT(ans&#x5B;i]);\r\n}\r\n\r\n<\/pre>\n<h3>External link:<\/h3>\n<p><a href=\"http:\/\/acm.sgu.ru\/problem.php?contest=0&#038;problem=505\">http:\/\/acm.sgu.ru\/problem.php?contest=0&#038;problem=505<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Brief description : \u7ed9\u5b9a n \u4e2a\u5b57\u7b26\u4e32\uff0c\u8be2\u95ee\u5176\u4e2d\u4ee5\u7ed9\u5b9a\u4e32\u4e3a\u524d\u7f00\uff0c\u7ed9\u5b9a\u4e32\u4e3a\u540e\u7f00\u7684\u5b57\u4e32\u6570\u76ee\uff0c\u8be2\u95ee\u6709 m \u4e2a\u3002 \uff08 n, m<\/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-161","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2tdP7-2B","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/161","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=161"}],"version-history":[{"count":0,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/161\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/media?parent=161"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/categories?post=161"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/tags?post=161"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}