{"id":3010,"date":"2023-06-09T11:33:24","date_gmt":"2023-06-09T03:33:24","guid":{"rendered":"https:\/\/www.shuizilong.com\/house\/?p=3010"},"modified":"2023-06-09T16:07:50","modified_gmt":"2023-06-09T08:07:50","slug":"luogu-p2086-noi2012-%e9%ad%94%e5%b9%bb%e6%a3%8b%e7%9b%98","status":"publish","type":"post","link":"https:\/\/www.shuizilong.com\/house\/archives\/luogu-p2086-noi2012-%e9%ad%94%e5%b9%bb%e6%a3%8b%e7%9b%98\/","title":{"rendered":"Luogu P2086. [NOI2012] \u9b54\u5e7b\u68cb\u76d8"},"content":{"rendered":"<ul>\n<li><a href=\"https:\/\/www.luogu.com.cn\/problem\/P2086\">https:\/\/www.luogu.com.cn\/problem\/P2086<\/a><\/li>\n<\/ul>\n<p>\u5dee\u5206\u7ef4\u62a4 gcd \u662f\u4e00\u4e2a\u5f88\u5e38\u89c1\u7684\u6280\u5de7\u4e86\u3002\u3002<br \/>\n\u5148\u5199\u4e2a\u7ebf\u6bb5\u6811\u62ff\u70b9\u90e8\u5206\u5206\u5427\u3002\u3002\u3002\uff08\u7136\u540e\u53d1\u73b0\u8ba8\u8bba n,m \u5927\u5c0f\u3002\u3002\u66b4\u529b\u5f00 o2 \u80fd\u8fc7\u3002\uff09<\/p>\n<pre class=\"brush: cpp; light: false; title: ; toolbar: true; notranslate\" title=\"\">\r\n#include &lt;lastweapon\/io&gt;\r\n\/\/ #include &lt;lastweapon\/segtree&gt;\r\nusing namespace lastweapon;\r\n\r\nconst int N = int(5e5)+9;\r\n\r\n\/*\r\nLL gcd(LL a,LL b)\r\n{\r\n    return b?gcd(b,a%b):abs(a);\r\n}*\/\r\n\r\ninline LL gcd(LL a,LL b)\r\n{\r\n\tif(a&lt;0) a=-a;\r\n\tif(b&lt;0) b=-b;\r\n\tif(!a) return b;\r\n\tif(!b) return a;\r\n\tLL t=__builtin_ctzll(a|b),tmp;\r\n\ta&gt;&gt;=__builtin_ctzll(a);\r\n\tdo\r\n\t{\r\n\t\tb&gt;&gt;=__builtin_ctzll(b);\r\n\t\tif(a&gt;b)\r\n\t\t{\r\n\t\t\ttmp=a,a=b,b=tmp;\r\n\t\t}\r\n\t\tb-=a;\r\n\t} while(b);\r\n\treturn a&lt;&lt;t;\r\n}\r\n\r\nLL op(LL a, LL b) { return gcd(a, b); }\r\nLL e() { return 0; }\r\nint m, n;\r\n\r\nconst int TN = 4*N, M = 710;\r\n\r\n\r\n#define lx (x&lt;&lt;1)\r\n#define rx (lx|1)\r\n#define ml ((l+r)&gt;&gt;1)\r\n#define mr (ml+1)\r\n#define lc lx,l,ml\r\n#define rc rx,mr,r\r\n#define rt 1,0,n-1\r\n\r\nstruct SegTree{\r\n\r\n    vector&lt;LL&gt; T;\r\n\r\n    void add(int x, LL d) {\r\n        T&#x5B;x] += d;\r\n    }\r\n\r\n    void Build(int x, int l, int r, vector&lt;LL&gt; &amp;a) {\r\n        if (l &lt; r) {\r\n            T&#x5B;x] = 0;\r\n            Build(lc, a), Build(rc, a);\r\n        } else {\r\n            T&#x5B;x] = a&#x5B;l];\r\n        }\r\n    }\r\n\r\n    LL Query(int x, int l, int r, const int p) {\r\n        if (l == r) {\r\n            return T&#x5B;x];\r\n        } else {\r\n            return T&#x5B;x] + (p &lt; mr ? Query(lc, p) : Query(rc, p));\r\n        }\r\n    }\r\n\r\n    void Add(int x, int l, int r, const int a, const int b, const LL d) {\r\n        if (b &lt; l || r &lt; a) return;\r\n        if (a &lt;= l &amp;&amp; r &lt;= b) {\r\n            add(x, d);\r\n        } else {\r\n            Add(lc, a, b, d); Add(rc, a, b, d);\r\n        }\r\n    }\r\n} S&#x5B;M];\r\n\r\n\r\nstruct SegTree2{\r\n\r\n    vector&lt;LL&gt; T;\r\n\r\n    void upd(int x) {\r\n        T&#x5B;x] = gcd(T&#x5B;lx], T&#x5B;rx]);\r\n    }\r\n\r\n    void add(int x, LL d) {\r\n        T&#x5B;x] += d;\r\n    }\r\n\r\n    void Build(int x, int l, int r, vector&lt;LL&gt; &amp;a) {\r\n        if (l &lt; r) {\r\n            Build(lc, a), Build(rc, a);\r\n            upd(x);\r\n        } else {\r\n            T&#x5B;x] = a&#x5B;l];\r\n        }\r\n    }\r\n\r\n    LL Query(int x, int l, int r, const int a, const int b) {\r\n        if (b &lt; l || r &lt; a) return 0;\r\n        if (a &lt;= l &amp;&amp; r &lt;= b) {\r\n            return T&#x5B;x];\r\n        } else {\r\n            return gcd(Query(lc, a, b), Query(rc, a, b));\r\n        }\r\n    }\r\n\r\n    void Add(int x, int l, int r, const int p, const LL d) {\r\n        if (l == r) {\r\n            add(x, d);\r\n        } else {\r\n            if (p &lt; mr) Add(lc, p, d);\r\n            else Add(rc, p, d);\r\n            upd(x);\r\n        }\r\n    }\r\n} T&#x5B;M];\r\n\r\nbool flip;\r\n\r\nint main() {\r\n\r\n#ifndef ONLINE_JUDGE\r\n    freopen(&quot;in.txt&quot;, &quot;r&quot;, stdin);\r\n    \/\/freopen(&quot;out.txt&quot;, &quot;w&quot;, stdout);\r\n#endif\r\n\r\n    RD(m, n); \/\/ if (m != 1) return 0;\r\n    int y0, x0; RD(y0, x0); --x0; --y0; int Q; RD(Q);\r\n\r\n    if (m &gt; n) {\r\n        swap(m, n);\r\n        flip = 1;\r\n    }\r\n\r\n    vector&lt;vector&lt;LL&gt;&gt; a, d; a.resize(m); d.resize(m);\r\n\r\n    if (flip) {\r\n        REP(i, n) REP(j, m) a&#x5B;j].PB(RD());\r\n    } else {\r\n        REP(j, m) REP(i, n) a&#x5B;j].PB(RD());\r\n    }\r\n\r\n    REP(j, m) {\r\n        REP(i, n-1) d&#x5B;j].PB(a&#x5B;j]&#x5B;i+1] - a&#x5B;j]&#x5B;i]);\r\n        d&#x5B;j].PB(0);\r\n    }\r\n\r\n    REP(i, m) S&#x5B;i].T.resize(4*n), S&#x5B;i].Build(rt, a&#x5B;i]);\r\n    REP(i, m) T&#x5B;i].T.resize(4*n), T&#x5B;i].Build(rt, d&#x5B;i]);\r\n\r\n    DO(Q) {\r\n\r\n        int cmd, u, l, d, r; RD(cmd, u, l, d, r);\r\n\r\n        if (!cmd) {\r\n            u = y0 - u; d = y0 + d; l = x0 - l; r = x0 + r;\r\n            if (flip) swap(u, l), swap(d, r);\r\n\r\n            LL z = 0; FOR_1(i, u, d) {\r\n                z = gcd(z, gcd(S&#x5B;i].Query(rt, l), l &lt; r ? T&#x5B;i].Query(rt, l, r-1) : 0));\r\n            }\r\n            printf(&quot;%lld\\n&quot;, abs(z));\r\n        } else {\r\n            LL delta; RD(delta); \/\/continue;\r\n            --l; --r; --u; --d; if (flip) swap(u, l), swap(d, r);\r\n\r\n            FOR_1(i, u, d) {\r\n                S&#x5B;i].Add(rt, l, r, delta);\r\n                if (l) T&#x5B;i].Add(rt, l-1, delta);\r\n                T&#x5B;i].Add(rt, r, -delta);\r\n            }\r\n        }\r\n    }\r\n}\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/www.luogu.com.cn\/problem\/P2086 \u5dee\u5206\u7ef4\u62a4 gcd \u662f\u4e00\u4e2a\u5f88\u5e38\u89c1\u7684\u6280\u5de7\u4e86\u3002\u3002 \u5148\u5199\u4e2a\u7ebf\u6bb5\u6811\u62ff\u70b9\u90e8\u5206\u5206\u5427\u3002\u3002\u3002\uff08\u7136\u540e\u53d1\u73b0\u8ba8\u8bba n,m \u5927\u5c0f\u3002\u3002\u66b4\u529b\u5f00 o2 \u80fd\u8fc7\u3002\uff09 #include &lt;lastweapon\/io&gt; \/\/ #include &lt;lastweapon\/segtree&gt; using namespace lastweapon; const int N = int(5e5)+9; \/* LL gcd(LL a,LL b) { return b?gcd(b,a%b):abs(a); }*\/ inline LL gcd(LL a,LL b) { if(a&lt;0) a=-a; if(b&lt;0) b=-b; if(!a) return b; if(!b) return a; LL t=__builtin_ctzll(a|b),tmp; a&gt;&gt;=__builtin_ctzll(a); do { b&gt;&gt;=__builtin_ctzll(b); if(a&gt;b) { [&hellip;]<\/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":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[1],"tags":[],"class_list":["post-3010","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2tdP7-My","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/3010","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=3010"}],"version-history":[{"count":3,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/3010\/revisions"}],"predecessor-version":[{"id":3013,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/posts\/3010\/revisions\/3013"}],"wp:attachment":[{"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/media?parent=3010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/categories?post=3010"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.shuizilong.com\/house\/wp-json\/wp\/v2\/tags?post=3010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}