{"id":295930,"date":"2019-12-17T16:32:03","date_gmt":"2019-12-17T16:32:03","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=295930"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=295930","title":{"rendered":"\u041a\u0430\u043a \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u0431\u0438\u043d\u0430\u0440\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u0437\u0430 \u043f\u0440\u0438\u0435\u043c\u043b\u0435\u043c\u043e\u0435 \u0432\u0440\u0435\u043c\u044f?"},"content":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/479142\/\">\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/p>\n<p>  \u041f\u043e\u043b\u0433\u043e\u0434\u0430 \u043d\u0430\u0437\u0430\u0434 \u044f \u0437\u0430\u0434\u0443\u043c\u0430\u043b\u0441\u044f, \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u0431\u0438\u043d\u0430\u0440\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u0437\u0430 O(log(N)). \u041e\u0442\u0432\u0435\u0442 \u043f\u0440\u0438\u0448\u0451\u043b \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0431\u044b\u0441\u0442\u0440\u043e \u2014 Lazy Propagation. \u041d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e \u0432 \u043a\u043e\u0434\u0435 \u044f \u043f\u043e\u043b\u0435\u043d\u0438\u043b\u0441\u044f. \u0421\u0435\u0439\u0447\u0430\u0441 \u043d\u0430\u0434\u043e \u0441\u0434\u0430\u0432\u0430\u0442\u044c \u0434\u0438\u043f\u043b\u043e\u043c\u043d\u044b\u0439 \u043f\u0440\u043e\u0435\u043a\u0442 \u0432 \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0438\u0442\u0435\u0442\u0435, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u044f \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0441\u044c \u0447\u0435\u043c \u0443\u0433\u043e\u0434\u043d\u043e, \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u0438\u043c. \u0418\u043c\u0435\u043d\u043d\u043e \u0442\u0430\u043a \u044f \u0438 \u0441\u0435\u043b \u044d\u0442\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c. <br \/>  <a name=\"habracut\"><\/a><br \/>  <b>\u041d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0437\u0430\u043c\u0435\u0447\u0430\u043d\u0438\u044f:<\/b><\/p>\n<ul>\n<li>\u0414\u0435\u0440\u0435\u0432\u043e \u043d\u0435 \u0441\u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0435 (\u043f\u043e\u043a\u0430 \u0447\u0442\u043e, \u0432\u0435\u0434\u044c \u0434\u0438\u043f\u043b\u043e\u043c\u043d\u044b\u0439 \u043f\u0440\u043e\u0435\u043a\u0442 \u0432\u0441\u0451 \u0436\u0435 \u043d\u0430\u0434\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c), \u0432 \u0441\u043b\u0435\u0434\u0441\u0442\u0432\u0438\u0435 \u0447\u0435\u0433\u043e \u043e\u0446\u0435\u043d\u043a\u0430 O(log(N)) \u0432\u0435\u0437\u0434\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0430\u043c\u043e\u0440\u0442\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0439 \u0434\u043b\u044f \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0445 \u0432\u0445\u043e\u0434\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445.<\/li>\n<li>\u041f\u043e\u0434\u043e\u0431\u043d\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u044f \u043d\u0435 \u043d\u0430\u0448\u0451\u043b, \u043a\u043e\u043b\u043b\u0435\u0433\u0438 \u0438 \u0434\u0440\u0443\u0437\u044c\u044f, \u0443 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0441\u043f\u0440\u0430\u0448\u0438\u0432\u0430\u043b, \u0442\u043e\u0436\u0435 \u043d\u0435 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0438\u043b\u0438 \u043d\u0438\u0447\u0435\u0433\u043e \u043f\u043e\u0445\u043e\u0436\u0435\u0433\u043e. \u0415\u0441\u043b\u0438 \u0432\u0430\u043c \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u043e\u0434\u043e\u0431\u043d\u043e\u0439 \u0438\u0434\u0435\u0438, \u043f\u0440\u043e\u0448\u0443 \u0441\u043e\u043e\u0431\u0449\u0438\u0442\u044c \u043c\u043d\u0435.<\/li>\n<li>\u0412 \u043a\u043b\u0430\u0441\u0441\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0431\u044b \u043e\u0431\u043e\u0439\u0442\u0438\u0441\u044c \u0431\u0435\u0437 \u043f\u043e\u043b\u044f parent, \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u044f \u0440\u043e\u0434\u0438\u0442\u0435\u043b\u044f \u0432 \u043c\u0435\u0442\u043e\u0434\u044b.<\/li>\n<\/ul>\n<p>  <b>\u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0438\u0434\u0435\u0438<\/b><\/p>\n<p>  \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u0435\u0440\u0448\u0438\u043d, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043b\u0435\u0432\u0435\u0435 \u043d\u0435\u0451, \u043d\u0430\u0437\u043e\u0432\u0451\u043c \u044d\u0442\u043e \u043f\u043e\u043b\u0435 \u0443 \u0432\u0435\u0440\u0448\u0438\u043d\u044b countLefter. \u041d\u043e \u0442\u043e\u0433\u0434\u0430, \u0435\u0441\u043b\u0438 \u043c\u044b \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0441\u0430\u043c\u044b\u0439 \u043b\u0435\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0432 \u0434\u0435\u0440\u0435\u0432\u043e, \u043f\u0440\u0438\u0434\u0451\u0442\u0441\u044f \u0434\u043b\u044f \u0432\u0441\u0435\u0445 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445 \u0432\u0435\u0440\u0448\u0438\u043d \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c countLefter, \u0447\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043c\u0443\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0434\u043e\u043b\u0433\u043e \u0438 \u0432 \u043a\u043e\u0440\u043d\u0435 (\u043e\u0445 \u0443\u0436 \u044d\u0442\u0430 \u0438\u0433\u0440\u0430 \u0441\u043b\u043e\u0432) \u043d\u0435 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u043f\u043e\u0434 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u044b \u0431\u0438\u043d\u0430\u0440\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430. \u0427\u0442\u043e\u0431\u044b \u044d\u0442\u043e\u0433\u043e \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0432\u0432\u0435\u0434\u0451\u043c \u043f\u043e\u043b\u0435 add, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0431\u0443\u0434\u0435\u043c \u0445\u0440\u0430\u043d\u0438\u0442\u044c, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043d\u0430\u0434\u043e \u043f\u0440\u0438\u0431\u0430\u0432\u0438\u0442\u044c \u043a \u043f\u043e\u043b\u044e countLefter \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0435\u0451 \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430, \u0432\u043a\u043b\u044e\u0447\u0430\u044f \u0441\u0430\u043c\u0443 \u044d\u0442\u0443 \u0432\u0435\u0440\u0448\u0438\u043d\u0443. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u044f \u0432 \u0434\u0435\u0440\u0435\u0432\u043e \u0441\u0430\u043c\u044b\u0439 \u043b\u0435\u0432\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442, \u043d\u0430\u0434\u043e \u0431\u0443\u0434\u0435\u0442 \u043b\u0438\u0448\u044c:<\/p>\n<ul>\n<li>\u0423\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c countLefter \u043f\u043e \u0432\u0441\u0435\u043c\u0443 \u043f\u0443\u0442\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u043c \u0434\u043e \u043c\u0435\u0441\u0442\u0430 \u0432\u0441\u0442\u0430\u0432\u043a\u0438 \u043d\u043e\u0432\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b<\/li>\n<li>\u0414\u043b\u044f \u0432\u0441\u0435\u0445 \u0432\u0435\u0440\u0448\u0438\u043d, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0430 \u043e\u0434\u043d\u0443 \u043f\u0440\u0430\u0432\u0435\u0435 \u0432\u044b\u0448\u0435\u0443\u043f\u043e\u043c\u044f\u043d\u0443\u0442\u044b\u0445, \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0442\u044c add \u043d\u0430 1<\/li>\n<\/ul>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043b\u043e\u0433\u0438\u0447\u043d\u043e \u0432\u0432\u0435\u0441\u0442\u0438 \u043c\u0435\u0442\u043e\u0434 push(), \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0438\u0431\u0430\u0432\u043b\u044f\u0442\u044c add \u043a countLefter \u0441\u0430\u043c\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0438 \u043e\u0431\u043e\u0438\u0445 \u0435\u0451 \u043f\u043e\u0442\u043e\u043c\u043a\u043e\u0432.<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0412\u043e\u0442 \u043a\u0430\u043a\u0438\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0441\u044f \u043a\u043b\u0430\u0441\u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b:<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"java\">public class UNode {      UNode parent;     int key;     int countLefter;     int add;     UNode left;     UNode right;      public UNode() {     }      public UNode(UNode parent, int key, int countLefter, int add, UNode left, UNode right) {         this.parent = parent;         this.key = key;         this.countLefter = countLefter;         this.add = add;         this.left = left;         this.right = right;     }      public void push() {         countLefter += add;         if (left != null)             left.add += add;         if (right != null)             right.add += add;         add = 0;     }      @Override     public String toString() {         return \"Node{\" +                 \"key=\" + key +                 \", countLefter=\" + countLefter +                 '}';     } } <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u041e\u0442\u043b\u0438\u0447\u043d\u043e, \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u0441\u0442\u0443\u043f\u0430\u0442\u044c \u043a <b>\u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044e \u0434\u0435\u0440\u0435\u0432\u0430!<\/b><\/p>\n<p>  \u041f\u0435\u0440\u0432\u043e\u0435, \u0447\u0442\u043e \u043c\u044b \u0434\u0435\u043b\u0430\u0435\u043c, \u0437\u0430\u0445\u043e\u0434\u044f \u0432 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u2014 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u043c\u0435\u0442\u043e\u0434 push().<\/p>\n<p>  \u0423\u0434\u0430\u043b\u044f\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0431\u0443\u0434\u0435\u043c \u043b\u0435\u0432\u044b\u043c \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435\u043c (\u0431\u0435\u0440\u0451\u043c \u0441\u0430\u043c\u0443\u044e \u043f\u0440\u0430\u0432\u0443\u044e \u0438\u0437 \u0432\u0435\u0440\u0448\u0438\u043d, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043b\u0435\u0432\u0435\u0435 \u0443\u0434\u0430\u043b\u044f\u0435\u043c\u043e\u0439).<\/p>\n<p>  \u0427\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443, \u043f\u043e\u0441\u0442\u0443\u043f\u0430\u0435\u043c \u0432\u043f\u043e\u043b\u043d\u0435 \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e: \u0435\u0441\u043b\u0438 index &lt; countLefter \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b, \u0438\u0434\u0451\u043c \u0432\u043b\u0435\u0432\u043e. \u0415\u0441\u043b\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0440\u0430\u0432\u043d\u044b, \u043c\u044b \u043d\u0430\u0448\u043b\u0438 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0441 \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u043c \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c. \u0418\u043d\u0430\u0447\u0435 \u2014 \u0438\u0434\u0451\u043c \u0432\u043f\u0440\u0430\u0432\u043e. <\/p>\n<p>  \u0423\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430, \u0432 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0435, \u043d\u0435 \u0441\u0438\u043b\u044c\u043d\u043e \u043e\u0442\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f \u043e\u0442 \u043e\u0431\u044b\u0447\u043d\u043e\u0433\u043e \u0431\u0438\u043d\u0430\u0440\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430, \u0437\u0430 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435\u043c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u043f\u043e\u043b\u0435\u0439 countLefter \u0438 add. \u0415\u0441\u043b\u0438 \u043c\u044b \u043f\u043e\u0441\u043b\u0435 \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u0433\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f\/\u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u0441\u044f \u0432 \u0432\u0435\u0440\u0448\u0438\u043d\u0443 \u0441\u043b\u0435\u0432\u0430, \u0442\u043e \u044d\u0442\u0438 \u043f\u043e\u043b\u044f \u043d\u0430\u0434\u043e \u0431\u0443\u0434\u0435\u0442 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c. \u0415\u0441\u043b\u0438 \u0441\u043f\u0440\u0430\u0432\u0430 \u2014 \u043d\u0435\u0442.<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0412\u043e\u0442 \u0442\u0430\u043a\u0438\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0441\u044f \u043a\u043e\u0434 \u0434\u0435\u0440\u0435\u0432\u0430:<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"java\">import java.util.LinkedList;  public class UTree {      private UNode root;     private int size;      public UTree() {      }      public int size() {         return size;     }      public int get(int index) {         return get(index, root);     }      public boolean add(int key) {         if (root == null) {             root = new UNode(null, key, 0, 0, null, null);             size++;             return true;         }         boolean res = add(key, root);         if (res) size++;         return res;     }      public boolean remove(int key) {          if (root == null)             return false;         if (key == this.root.key) {             root.push();             removeRoot();             size--;             return true;         }          boolean res = remove(key, root);         if (res) size--;         return res;     }      private int get(int index, UNode root) {         root.push();         if (index == root.countLefter)             return root.key;         if (index &lt; root.countLefter)             return get(index, root.left);         return get(index, root.right);     }      private boolean add(int key, UNode root) {         if (key == root.key) return false;         root.push();         if (key &lt; root.key)             if (root.left != null) {                 boolean res = add(key, root.left);                 if (res) {                     root.countLefter++;                     if (root.right != null)                         root.right.add++;                 }                 return res;             } else {                 root.left = new UNode(root, key, root.countLefter, 0, null, null);                 root.countLefter++;                 if (root.right != null)                     root.right.add++;                 return true;             }         if (root.right != null)             return add(key, root.right);         else {             root.right = new UNode(root, key, root.countLefter + 1, 0, null, null);             return true;         }     }      public boolean removeByIndex(int index) {         if(this.root == null) return false;         root.push();         if (index == this.root.countLefter) {             removeRoot();             return true;         }         boolean res = removeByIndex(index, root);         if (res) size--;         return res;     }      private boolean removeByIndex(int index, UNode root) {         if (root == null)             return false;         root.push();         if (index == root.countLefter)             return removeNode(root);         if (index &lt; root.countLefter) {             boolean res = removeByIndex(index, root.left);             if (res) {                 root.countLefter--;                 if (root.right != null)                     root.right.add--;             }             return res;         } else             return removeByIndex(index, root.right);     }      private boolean removeNode(UNode root) {         if (root.left == root.right) {             if (root.parent.left == root)                 root.parent.left = null;             else root.parent.right = null;             return true;         }         if (root.left == null) {             if (root.parent.left == root) {                 root.parent.left = root.right;                 root.right.add--;             } else {                 root.parent.right = root.right;                 root.right.add--;             }             root.right.parent = root.parent;             return true;         }         if (root.right == null) {             if (root.parent.left == root)                 root.parent.left = root.left;             else                 root.parent.right = root.left;             root.left.parent = root.parent;             return true;         }         UNode right = getRight(root.left);         cut(right);         root.key = right.key;         root.countLefter--;         root.right.add--;         return true;     }      private boolean remove(int key, UNode root) {         if (root == null)             return false;         root.push();         if (key == root.key)             return removeNode(root);         if (key &lt; root.key) {             boolean res = remove(key, root.left);             if (res) {                 root.countLefter--;                 if (root.right != null)                     root.right.add--;             }             return res;         } else             return remove(key, root.right);     }      private void removeRoot() {         if (root.left == root.right) {             root = null;             return;         }         if (root.left == null) {             root = root.right;             root.add--;             return;         }         if (root.right == null) {             root = root.left;             return;         }         UNode right = getRight(root.left);         cut(right);         root.key = right.key;         root.countLefter--;         root.right.add--;     }      private static void cut(UNode node) {         if (node.parent.left == node)             node.parent.left = node.left;         else             node.parent.right = node.left;         if (node.left != null)             node.left.parent = node.parent;      }      private UNode getRight(UNode root) {         root.push();         if (root.right == null)             return root;         return getRight(root.right);     }      public void printTree() {         printTree(root);     }      private void printTree(UNode root) {         if (root == null) return;         root.push();         printTree(root.left);         System.out.println(root);         printTree(root.right);     }      public LinkedList&lt;UNode&gt; getAll(){         LinkedList&lt;UNode&gt; res = new LinkedList&lt;&gt;();         getAll(root, res);         return res;     }      private void getAll(UNode root, LinkedList&lt;UNode&gt; res){         if (root == null) return;         root.push();         getAll(root.left, res);         res.add(root);         getAll(root.right, res);      }  }  <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u2192 <a href=\"https:\/\/github.com\/AlexanderUsatov\/UTree\" rel=\"nofollow\">\u0422\u0443\u0442<\/a> \u043a\u043e\u0434 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u043d\u0430 \u0433\u0438\u0442\u0445\u0430\u0431\u0435.<\/p>\n<p>  \u041f\u0440\u0438\u0432\u0435\u0434\u0443 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438 \u0440\u0430\u0431\u043e\u0442\u044b. \u0422\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0440\u043e\u0432\u043e\u0434\u0438\u043b\u043e\u0441\u044c \u043d\u0430:<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/ym\/h3\/i4\/ymh3i4awvgvvgxntnc8pa9n2tlo.png\"><\/p>\n<p>  <b>\u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432 \u0434\u0435\u0440\u0435\u0432\u043e:<\/b><\/p>\n<p>  \u0418\u0442\u0430\u043a, \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043c\u0438\u043b\u043b\u0438\u043e\u043d\u0430 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 [0; 1_000):<br \/>  \u041e\u043a\u043e\u043b\u043e 100 \u043c\u0441. TreeSet \u0441\u043f\u0440\u0430\u0432\u0438\u043b\u0441\u044f \u0441 \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435\u0439 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0437\u0430 130 \u043c\u0441.<\/p>\n<p>  \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043c\u0438\u043b\u043b\u0438\u043e\u043d\u0430 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 [0; 10_000):<br \/>  \u041e\u043a\u043e\u043b\u043e 150 \u043c\u0441. TreeSet \u0441\u043f\u0440\u0430\u0432\u0438\u043b\u0441\u044f \u0441 \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435\u0439 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0437\u0430 190 \u043c\u0441.<\/p>\n<p>  \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043c\u0438\u043b\u043b\u0438\u043e\u043d\u0430 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 [0; 100_000):<br \/>  \u041e\u043a\u043e\u043b\u043e 320 \u043c\u0441. TreeSet \u0441\u043f\u0440\u0430\u0432\u0438\u043b\u0441\u044f \u0441 \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435\u0439 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0437\u0430 415 \u043c\u0441.<\/p>\n<p>  \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043c\u0438\u043b\u043b\u0438\u043e\u043d\u0430 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 [0; 1_000_000):<br \/>  \u041e\u043a\u043e\u043b\u043e 510 \u043c\u0441. TreeSet \u0441\u043f\u0440\u0430\u0432\u0438\u043b\u0441\u044f \u0441 \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435\u0439 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0437\u0430 700 \u043c\u0441.<\/p>\n<p>  \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043c\u0438\u043b\u043b\u0438\u043e\u043d\u0430 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u0445 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0435 [0; 10_000_000):<br \/>  \u041e\u043a\u043e\u043b\u043e 590 \u043c\u0441. TreeSet \u0441\u043f\u0440\u0430\u0432\u0438\u043b\u0441\u044f \u0441 \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435\u0439 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0437\u0430 750 \u043c\u0441.<\/p>\n<p>  <b>\u0422\u0435\u043f\u0435\u0440\u044c \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435<\/b><\/p>\n<p>  \u0421\u043b\u0443\u0447\u0430\u0439\u043d\u044b\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043c\u0438\u043b\u043b\u0438\u043e\u043d \u0447\u0438\u0441\u0435\u043b \u0432 \u0434\u0435\u0440\u0435\u0432\u043e. \u0417\u0430\u0442\u0435\u043c \u043c\u0438\u043b\u043b\u0438\u043e\u043d \u0440\u0430\u0437 \u043f\u044b\u0442\u0430\u0435\u043c\u0441\u044f \u0443\u0434\u0430\u043b\u0438\u0442\u044c \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u0435 \u0447\u0438\u0441\u043b\u043e. \u0412 \u0442\u0435\u0441\u0442\u0430\u0445 \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u0440\u0435\u043c\u044f, \u0437\u0430\u0442\u0440\u0430\u0447\u0435\u043d\u043d\u043e\u0435 \u043d\u0430 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435.<\/p>\n<p>  \u0414\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f [0; 10_000_000):<br \/>  \u041e\u043a\u043e\u043b\u043e 740 \u043c\u0441. TreeSet \u0441\u043f\u0440\u0430\u0432\u0438\u043b\u0441\u044f \u0441 \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435\u0439 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0437\u0430 750 \u043c\u0441.<\/p>\n<p>  \u0414\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f [0; 1_000_000):<br \/>  \u041e\u043a\u043e\u043b\u043e 600 \u043c\u0441. TreeSet \u0441\u043f\u0440\u0430\u0432\u0438\u043b\u0441\u044f \u0441 \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435\u0439 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0437\u0430 800 \u043c\u0441 (\u0441\u0442\u0430\u043b\u043e \u0431\u043e\u043b\u044c\u0448\u0435, \u0447\u0435\u043c \u0432 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u043c \u0442\u0435\u0441\u0442\u0435).<\/p>\n<p>  \u0414\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f [0; 100_000):<br \/>  \u041e\u043a\u043e\u043b\u043e 130 \u043c\u0441. TreeSet \u0441\u043f\u0440\u0430\u0432\u0438\u043b\u0441\u044f \u0441 \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435\u0439 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0437\u0430 160 \u043c\u0441.<\/p>\n<p>  \u0414\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f [0; 10_000):<br \/>  \u041e\u043a\u043e\u043b\u043e 45 \u043c\u0441. TreeSet \u0441\u043f\u0440\u0430\u0432\u0438\u043b\u0441\u044f \u0441 \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435\u0439 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0437\u0430 50 \u043c\u0441.<\/p>\n<p>  \u0414\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0438 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f [0; 1_000):<br \/>  \u041e\u043a\u043e\u043b\u043e 30 \u043c\u0441. TreeSet \u0441\u043f\u0440\u0430\u0432\u0438\u043b\u0441\u044f \u0441 \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435\u0439 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0437\u0430 37 \u043c\u0441.<\/p>\n<p>  <b>\u041d\u0443 \u0438, \u043d\u0430\u043a\u043e\u043d\u0435\u0446, \u0440\u0430\u0434\u0438 \u0447\u0435\u0433\u043e \u0432\u0441\u0451 \u0437\u0430\u0442\u0435\u0432\u0430\u043b\u043e\u0441\u044c, \u0434\u043e\u0441\u0442\u0443\u043f \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443<\/b><\/p>\n<p>  \u0423 TreeSet \u043d\u0435\u0442 \u0442\u0430\u043a\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438. \u0422\u0430\u043a \u0447\u0442\u043e \u043f\u0440\u0438\u0432\u0435\u0434\u0443 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f UTree. \u0421\u043d\u043e\u0432\u0430 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043c\u0438\u043b\u043b\u0438\u043e\u043d \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432, \u043f\u043e\u0442\u043e\u043c \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u043f\u043e \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e\u043c\u0443 \u0438\u043d\u0434\u0435\u043a\u0441\u0443 \u043e\u0442 0 \u0434\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u0434\u0435\u0440\u0435\u0432\u0435. \u0412\u0440\u0435\u043c\u044f \u0443\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443.<\/p>\n<p>  \u0414\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f [0; 1000): 85 \u043c\u0441<\/p>\n<p>  \u0414\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f [0; 10_000): 140 \u043c\u0441<\/p>\n<p>  \u0414\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f [0; 100_000): 300 \u043c\u0441<\/p>\n<p>  \u0414\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f [0; 1_000_000): 655 \u043c\u0441<\/p>\n<p>  \u041d\u0430\u0434\u0435\u044e\u0441\u044c, \u043a\u0442\u043e-\u0442\u043e \u0441\u043e\u0447\u0442\u0451\u0442 \u043c\u043e\u044e \u0438\u0434\u0435\u044e \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0439, \u0430, \u043c\u043e\u0436\u0435\u0442, \u0434\u043b\u044f \u043a\u043e\u0433\u043e-\u0442\u043e \u044d\u0442\u043e \u0441\u0442\u0430\u043d\u0435\u0442 \u043f\u043e\u0432\u043e\u0434\u043e\u043c \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0441 \u0431\u0438\u043d\u0430\u0440\u043d\u044b\u043c\u0438 \u0434\u0435\u0440\u0435\u0432\u044c\u044f\u043c\u0438, \u0435\u0441\u043b\u0438 \u0435\u0449\u0451 \u0442\u0430\u043a \u043d\u0435 \u0441\u0434\u0435\u043b\u0430\u043b\u0438 \ud83d\ude42<\/p>\n<p>  <b>P.S.<\/b><\/p>\n<p>  \u041e\u0437\u0430\u0434\u0430\u0447\u0438\u0442\u044c\u0441\u044f \u0431\u0430\u043b\u0430\u043d\u0441\u0438\u0440\u043e\u0432\u043a\u043e\u0439 \u0434\u0435\u0440\u0435\u0432\u0430 \u043f\u043b\u0430\u043d\u0438\u0440\u0443\u044e \u043f\u043e\u0441\u043b\u0435 \u041d\u043e\u0432\u043e\u0433\u043e \u0433\u043e\u0434\u0430. \u0415\u0441\u043b\u0438 \u044f \u0432\u0441\u0451-\u0442\u0430\u043a\u0438 \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u044e, \u0437\u0434\u0435\u0441\u044c \u043f\u043e\u044f\u0432\u0438\u0442\u0441\u044f \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435.<\/p><\/div>\n<p>               <script class=\"js-mediator-script\">!function(e){function t(t,n){if(!(n in e)){for(var r,a=e.document,i=a.scripts,o=i.length;o--;)if(-1!==i[o].src.indexOf(t)){r=i[o];break}if(!r){r=a.createElement(\"script\"),r.type=\"text\/javascript\",r.async=!0,r.defer=!0,r.src=t,r.charset=\"UTF-8\";var d=function(){var e=a.getElementsByTagName(\"script\")[0];e.parentNode.insertBefore(r,e)};\"[object Opera]\"==e.opera?a.addEventListener?a.addEventListener(\"DOMContentLoaded\",d,!1):e.attachEvent(\"onload\",d):d() } } }t(\"\/\/mediator.mail.ru\/script\/2820404\/\",\"_mediator\")}(window);<\/script>      <br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/479142\/\"> https:\/\/habr.com\/ru\/post\/479142\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/479142\/\">\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/p>\n<p>  \u041f\u043e\u043b\u0433\u043e\u0434\u0430 \u043d\u0430\u0437\u0430\u0434 \u044f \u0437\u0430\u0434\u0443\u043c\u0430\u043b\u0441\u044f, \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0438\u0437 \u0431\u0438\u043d\u0430\u0440\u043d\u043e\u0433\u043e \u0434\u0435\u0440\u0435\u0432\u0430 \u0437\u0430 O(log(N)). \u041e\u0442\u0432\u0435\u0442 \u043f\u0440\u0438\u0448\u0451\u043b \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0431\u044b\u0441\u0442\u0440\u043e \u2014 Lazy Propagation. \u041d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u044d\u0442\u043e \u0432 \u043a\u043e\u0434\u0435 \u044f \u043f\u043e\u043b\u0435\u043d\u0438\u043b\u0441\u044f. \u0421\u0435\u0439\u0447\u0430\u0441 \u043d\u0430\u0434\u043e \u0441\u0434\u0430\u0432\u0430\u0442\u044c \u0434\u0438\u043f\u043b\u043e\u043c\u043d\u044b\u0439 \u043f\u0440\u043e\u0435\u043a\u0442 \u0432 \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0438\u0442\u0435\u0442\u0435, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u044f \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0441\u044c \u0447\u0435\u043c \u0443\u0433\u043e\u0434\u043d\u043e, \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u0438\u043c. \u0418\u043c\u0435\u043d\u043d\u043e \u0442\u0430\u043a \u044f \u0438 \u0441\u0435\u043b \u044d\u0442\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c.   <\/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-295930","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/295930","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=295930"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/295930\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=295930"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=295930"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=295930"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}