{"id":34644,"date":"2024-11-01T09:30:25","date_gmt":"2024-11-01T09:30:25","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34644"},"modified":"2024-11-01T11:40:23","modified_gmt":"2024-11-01T11:40:23","slug":"javascript-coding-test-course-segment-tree","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34644\/","title":{"rendered":"JavaScript Coding Test Course, Segment Tree"},"content":{"rendered":"<p><body><\/p>\n<h2>Problem Description<\/h2>\n<div class=\"problem\">\n<h3>Problem: Calculate the Sum of a Given Range<\/h3>\n<p>\n        An array <code>arr<\/code> containing <code>n<\/code> integers is given,<br \/>\n        and you need to write a function to handle the following queries:\n    <\/p>\n<ul>\n<li>1. <code>update(index, value)<\/code> : Updates the <code>index<\/code>th value in the array <code>arr<\/code> to <code>value<\/code>.<\/li>\n<li>2. <code>rangeSum(left, right)<\/code> : Calculates the sum from the <code>left<\/code>th to the <code>right<\/code>th (0-indexing) in the array <code>arr<\/code>.<\/li>\n<\/ul>\n<p>\n        Use the given array and queries to efficiently handle the requirements of <code>update<\/code> and <code>rangeSum<\/code>.<br \/>\n        The size of the array is up to 10^5 and the number of queries is also up to 10^5.\n    <\/p>\n<\/div>\n<h2>Solution Method<\/h2>\n<p>\n    This problem requires efficiently calculating range sums and processing updates, so we can use a <strong>Segment Tree<\/strong>.<br \/>\n    A Segment Tree is a binary tree-based data structure that stores the given array in intervals (for range sum queries).\n<\/p>\n<h3>Definition of Segment Tree<\/h3>\n<p>\n    A Segment Tree has the following properties:\n<\/p>\n<ul>\n<li>Each node stores information about one array interval. This information can be set as the sum, minimum, maximum, etc. of the interval.<\/li>\n<li>The height of the tree is <code>O(log n)<\/code>, meaning that both query and update operations take <code>O(log n)<\/code> time.<\/li>\n<\/ul>\n<h3>Steps to Implement a Segment Tree<\/h3>\n<p>To implement a Segment Tree, follow these steps:<\/p>\n<ol>\n<li><strong>Initialization:<\/strong> Initialize the Segment Tree based on the given array.<\/li>\n<li><strong>Range Sum Query:<\/strong> Recursively retrieve the nodes necessary to calculate the sum for a specific interval.<\/li>\n<li><strong>Update:<\/strong> Update the value at a specific index and refresh the relevant segment nodes.<\/li>\n<\/ol>\n<h3>JavaScript Code Implementation<\/h3>\n<div class=\"code\">\n<pre><code>\nclass SegmentTree {\n    constructor(arr) {\n        this.n = arr.length;\n        this.tree = new Array(this.n * 4);\n        this.build(arr, 0, 0, this.n - 1);\n    }\n\n    build(arr, node, start, end) {\n        if (start === end) {\n            \/\/ Store integer value at leaf node\n            this.tree[node] = arr[start];\n        } else {\n            const mid = Math.floor((start + end) \/ 2);\n            \/\/ Define left child\n            this.build(arr, node * 2 + 1, start, mid);\n            \/\/ Define right child\n            this.build(arr, node * 2 + 2, mid + 1, end);\n            \/\/ Define parent node as the sum of both children\n            this.tree[node] = this.tree[node * 2 + 1] + this.tree[node * 2 + 2];\n        }\n    }\n\n    rangeSum(left, right) {\n        return this.sum(0, 0, this.n - 1, left, right);\n    }\n\n    sum(node, start, end, left, right) {\n        if (right < start || end < left) {\n            \/\/ Return 0 if requested range does not overlap\n            return 0;\n        }\n        if (left <= start &#038;&#038; end <= right) {\n            \/\/ Return node if requested range is fully included\n            return this.tree[node];\n        }\n        const mid = Math.floor((start + end) \/ 2);\n        const leftSum = this.sum(node * 2 + 1, start, mid, left, right);\n        const rightSum = this.sum(node * 2 + 2, mid + 1, end, left, right);\n        return leftSum + rightSum;\n    }\n\n    update(index, value) {\n        this.updateValue(0, 0, this.n - 1, index, value);\n    }\n\n    updateValue(node, start, end, index, value) {\n        if (start === end) {\n            \/\/ Update leaf node\n            this.tree[node] = value;\n        } else {\n            const mid = Math.floor((start + end) \/ 2);\n            if (index <= mid) {\n                this.updateValue(node * 2 + 1, start, mid, index, value);\n            } else {\n                this.updateValue(node * 2 + 2, mid + 1, end, index, value);\n            }\n            \/\/ Update parent node\n            this.tree[node] = this.tree[node * 2 + 1] + this.tree[node * 2 + 2];\n        }\n    }\n}\n\n\/\/ Example usage\nconst arr = [1, 3, 5, 7, 9, 11];\nconst segmentTree = new SegmentTree(arr);\nconsole.log(segmentTree.rangeSum(1, 3)); \/\/ 15\nsegmentTree.update(1, 10);\nconsole.log(segmentTree.rangeSum(1, 3)); \/\/ 22\n<\/code><\/pre>\n<\/div>\n<h2>Conclusion<\/h2>\n<p>\n    The Segment Tree is a powerful tool for efficiently handling the range sum of arrays.<br \/>\n    This data structure allows for updates and range sum calculations with a time complexity of <code>O(log n)<\/code>.<br \/>\n    When faced with complex problems in practice, using a Segment Tree can provide many advantages.\n<\/p>\n<h2>Additional Practice Problems<\/h2>\n<p>Try practicing the following problems:<\/p>\n<ul>\n<li>Use a Segment Tree to find the minimum value in a given array<\/li>\n<li>Add a query to add a specific value over an interval<\/li>\n<li>Find the maximum value using a Segment Tree<\/li>\n<\/ul>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Description Problem: Calculate the Sum of a Given Range An array arr containing n integers is given, and you need to write a function to handle the following queries: 1. update(index, value) : Updates the indexth value in the array arr to value. 2. rangeSum(left, right) : Calculates the sum from the leftth to &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34644\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;JavaScript Coding Test Course, Segment Tree&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[141],"tags":[],"class_list":["post-34644","post","type-post","status-publish","format-standard","hentry","category-javascript-coding-test"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>JavaScript Coding Test Course, Segment Tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/34644\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"JavaScript Coding Test Course, Segment Tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Problem Description Problem: Calculate the Sum of a Given Range An array arr containing n integers is given, and you need to write a function to handle the following queries: 1. update(index, value) : Updates the indexth value in the array arr to value. 2. rangeSum(left, right) : Calculates the sum from the leftth to &hellip; \ub354 \ubcf4\uae30 &quot;JavaScript Coding Test Course, Segment Tree&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34644\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:30:25+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:40:23+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"2\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/34644\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34644\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"JavaScript Coding Test Course, Segment Tree\",\"datePublished\":\"2024-11-01T09:30:25+00:00\",\"dateModified\":\"2024-11-01T11:40:23+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34644\/\"},\"wordCount\":315,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Javascript Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34644\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34644\/\",\"name\":\"JavaScript Coding Test Course, Segment Tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:30:25+00:00\",\"dateModified\":\"2024-11-01T11:40:23+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34644\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34644\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34644\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript Coding Test Course, Segment Tree\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"JavaScript Coding Test Course, Segment Tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/34644\/","og_locale":"ko_KR","og_type":"article","og_title":"JavaScript Coding Test Course, Segment Tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Problem Description Problem: Calculate the Sum of a Given Range An array arr containing n integers is given, and you need to write a function to handle the following queries: 1. update(index, value) : Updates the indexth value in the array arr to value. 2. rangeSum(left, right) : Calculates the sum from the leftth to &hellip; \ub354 \ubcf4\uae30 \"JavaScript Coding Test Course, Segment Tree\"","og_url":"https:\/\/atmokpo.com\/w\/34644\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:30:25+00:00","article_modified_time":"2024-11-01T11:40:23+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"2\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/34644\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34644\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"JavaScript Coding Test Course, Segment Tree","datePublished":"2024-11-01T09:30:25+00:00","dateModified":"2024-11-01T11:40:23+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34644\/"},"wordCount":315,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Javascript Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34644\/","url":"https:\/\/atmokpo.com\/w\/34644\/","name":"JavaScript Coding Test Course, Segment Tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:30:25+00:00","dateModified":"2024-11-01T11:40:23+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34644\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34644\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34644\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"JavaScript Coding Test Course, Segment Tree"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34644","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=34644"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34644\/revisions"}],"predecessor-version":[{"id":34645,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34644\/revisions\/34645"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34644"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34644"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34644"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}