{"id":262838,"date":"2015-08-03T22:28:02","date_gmt":"2015-08-03T18:28:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=262838"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=262838","title":{"rendered":"\u0414\u0435\u0440\u0435\u0432\u043e \u043e\u0442\u0440\u0435\u0437\u043a\u043e\u0432 \u0441 \u0433\u0440\u0443\u043f\u043f\u043e\u0432\u043e\u0439 \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0435\u0439 \u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c \u0441\u0443\u043c\u043c\u044b"},"content":{"rendered":"<p>     \t\u041f\u0440\u0438\u0432\u0435\u0442, \u0445\u0430\u0431\u0440!<\/p>\n<p>  \u042d\u0442\u043e \u043c\u043e\u044f \u0432\u0442\u043e\u0440\u0430\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u0438\u0437 \u0441\u0435\u0440\u0438\u0438 \u00ab\u0442\u043e \u0447\u0442\u043e \u044f \u043d\u0435 \u043d\u0430\u0448\u0435\u043b \u0432 \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442\u0430\u0445\u00bb. \u041a\u043e\u043c\u0443 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e \u2014 \u0434\u043e\u0431\u0440\u043e \u043f\u043e\u0436\u0430\u043b\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434 \u043a\u0430\u0442.<br \/>  <a name=\"habracut\"><\/a><\/p>\n<p>  \u041d\u0430\u0447\u043d\u0435\u043c \u0441 \u043f\u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u0437\u0430\u0434\u0430\u0447\u0438:   <\/p>\n<ul>\n<li>\u0414\u0430\u043d \u043c\u0430\u0441\u0441\u0438\u0432 \u0438\u0437 N \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c, \u0433\u0434\u0435 1 &lt;= N &lt;= 100 000, <\/li>\n<li> \u0438 K \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 (1 &lt;= K &lt;= 100 000) \u0432\u0438\u0434\u0430:<br \/> \n<ol>\n<li> \u043f\u0440\u0438\u0441\u0432\u043e\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c \u0441 L \u043f\u043e R \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 V (0 &lt;= V &lt;= 100 000 000)<\/li>\n<li> \u043d\u0430\u0439\u0442\u0438 \u0441\u0443\u043c\u043c\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043e\u0442\u0440\u0435\u0437\u043a\u0430 [L; R]<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0417\u0430\u0434\u0430\u0447\u0430<\/b><\/p>\n<div class=\"spoiler_text\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/b19\/81f\/b4e\/b1981fb4e8c949bb817337face243152.png\" alt=\"image\"\/><br \/>  <a href=\"http:\/\/codeforces.com\/gym\/100093\/attachments\">\u0441\u0441\u044b\u043b\u043a\u0430<\/a>  <\/div>\n<\/div>\n<p>  \u0417\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441\u0443\u043c\u043c\u044b \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 value, \u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u0438\u0441\u0432\u043e\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430 \u0431\u0443\u0434\u0435\u043c \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 delta. \u041f\u0435\u0440\u0432\u0438\u0447\u043d\u043e\u0435 \u0437\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u043d\u0438\u0447\u0435\u043c \u043d\u0435 \u043e\u0442\u043b\u0438\u0447\u0430\u0435\u0442\u0441\u044f \u043e\u0442 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u043e\u0442\u0440\u0435\u0437\u043a\u043e\u0432:  <\/p>\n<pre><code class=\"java\">long[] value; long[] delta; long default_delta = -1; \/\/ \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c void init(long[] a){         int nodes = a.length &lt;&lt; 2;         tree = new long[nodes];         delta = new long[nodes];         build(a, 1, 0, a.length-1); } void build(long[] a, int v,int tl,int tr){         if(tl == tr)             tree[v] = a[tl];         else{             int mid = (tl+tr)&gt;&gt;1;             build(a, 2*v, tl, mid);             build(a, 2*v+1, mid+1, tr);             tree[v] = tree[v*2]+tree[v*2+1];             delta[v] = default_delta;         } } <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0443\u0436\u043d\u043e \u043d\u0430\u0443\u0447\u0438\u0442\u044c\u0441\u044f \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430 \u043e\u0442\u0440\u0435\u0437\u043a\u0435. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u00ab\u043e\u0442\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0435 \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438\u00bb, \u0442\u043e \u0435\u0441\u0442\u044c \u0431\u0443\u0434\u0435\u043c \u043f\u043e\u043c\u0435\u0447\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u043c \u0442\u043e\u043b\u044c\u043a\u043e \u043a\u043e\u0440\u043d\u0438 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0432\u0443\u044e\u0449\u0438\u0445 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u044c\u0435\u0432:  <\/p>\n<pre><code class=\"java\">void update(int root, int left, int right, int from, int to, long delta) { \t\tif (left &gt; to || right &lt; from) \t\t\treturn; \t\tif (left &gt;= from && right &lt;= to) { \t\t\tupdateFull(root, left, right, from, to, delta); \t\t\treturn; \t\t} \t\tint middle = (left + right) &gt;&gt; 1; \t\tupdatePreProcess(root, left, right, from, to, delta, middle); \t\tupdate(2 * root + 1, left, middle, from, to, delta); \t\tupdate(2 * root + 2, middle + 1, right, from, to, delta); \t\tupdatePostProcess(root, left, right, from, to, delta, middle); } void updateFull(int root, int left, int right, int from, int to, long delta) { \t\tvalue[root] = accumulate(value[root], delta, right - left + 1); \t\tdelta[root] = joinDelta(delta[root], delta); } void updatePreProcess(int root, int left, int right, int from, int to, long delta, int middle) { \t\tpushDown(root, left, middle, right); } void updatePostProcess(int root, int left, int right, int from, int to, long delta, int middle) { \t\tvalue[root] = joinValue(value[2 * root + 1], value[2 * root + 2]); } void pushDown(int root, int left, int middle, int right) { \t\tvalue[2 * root + 1] = accumulate(value[2 * root + 1], delta[root], middle - left + 1); \t\tvalue[2 * root + 2] = accumulate(value[2 * root + 2], delta[root], right - middle); \t\tdelta[2 * root + 1] = joinDelta(delta[2 * root + 1], delta[root]); \t\tdelta[2 * root + 2] = joinDelta(delta[2 * root + 2], delta[root]); \t\tdelta[root] = default_delta; } long accumulate(long value, long delta, int length) {         if(delta != default_delta)             return delta * length;         return value; } long joinDelta(long was, long delta) {         if(delta != default_delta) \/\/             return delta;         return was; } long joinValue(long left, long right) {         return left + right; }  <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430\u0443\u0447\u0438\u0442\u044c\u0441\u044f \u043f\u0435\u0440\u0435\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u0441\u0443\u043c\u043c\u0443 \u043d\u0430 \u043e\u0442\u0440\u0435\u0437\u043a\u0435, \u0435\u0441\u043b\u0438 \u044d\u0442\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e:  <\/p>\n<pre><code class=\"java\">long query(int root, int left, int right, int from, int to) { \t\tif (left &gt; to || right &lt; from) \t\t\treturn 0; \t\tif (left &gt;= from && right &lt;= to) \t\t\treturn queryFull(root, left, right, from, to); \t\tint middle = (left + right) &gt;&gt; 1; \t\tqueryPreProcess(root, left, right, from, to, middle); \t\tlong leftResult = query(2 * root + 1, left, middle, from, to); \t\tlong rightResult = query(2 * root + 2, middle + 1, right, from, to); \t\treturn queryPostProcess(root, left, right, from, to, middle, leftResult, rightResult); } void queryPreProcess(int root, int left, int right, int from, int to, int middle) { \t\tpushDown(root, left, middle, right); } long queryPostProcess(int root, int left, int right, int from, int to, int middle, long leftResult, long rightResult) { \t\treturn joinValue(leftResult, rightResult); } long queryFull(int root, int left, int right, int from, int to) { \t\treturn value[root]; } <\/code><\/pre>\n<p>  \u041d\u0430 \u044d\u0442\u043e\u043c, \u0432\u0440\u043e\u0434\u0435, \u0432\u0441\u0435. \u0421\u043d\u043e\u0432\u0430 \u043d\u0430\u0434\u0435\u044e\u0441\u044c, \u0447\u0442\u043e \u043a\u043e\u043c\u0443-\u0442\u043e \u043f\u0440\u0438\u0433\u043e\u0434\u0438\u0442\u0441\u044f.<br \/>  p.s. \u0427\u0430\u0441\u0442\u044c \u043a\u043e\u0434 \u0432\u0437\u044f\u0442\u0430 \u0438 \u0432 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043c\u0435\u0441\u0442\u0430\u0445 \u043f\u0435\u0440\u0435\u0434\u0435\u043b\u0430\u043d\u043d\u0430 \u0438\u0437 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 Chelper&#8217;a.     \t<\/p>\n<div class=\"clear\"><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/264087\/\"> http:\/\/habrahabr.ru\/post\/264087\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>     \t\u041f\u0440\u0438\u0432\u0435\u0442, \u0445\u0430\u0431\u0440!<\/p>\n<p>  \u042d\u0442\u043e \u043c\u043e\u044f \u0432\u0442\u043e\u0440\u0430\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u0438\u0437 \u0441\u0435\u0440\u0438\u0438 \u00ab\u0442\u043e \u0447\u0442\u043e \u044f \u043d\u0435 \u043d\u0430\u0448\u0435\u043b \u0432 \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442\u0430\u0445\u00bb. \u041a\u043e\u043c\u0443 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e \u2014 \u0434\u043e\u0431\u0440\u043e \u043f\u043e\u0436\u0430\u043b\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0434 \u043a\u0430\u0442.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-262838","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/262838","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=262838"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/262838\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=262838"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=262838"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=262838"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}