{"id":33672,"date":"2024-11-01T09:19:12","date_gmt":"2024-11-01T09:19:12","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=33672"},"modified":"2024-11-01T11:47:11","modified_gmt":"2024-11-01T11:47:11","slug":"python-coding-test-course-segment-tree","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/33672\/","title":{"rendered":"python coding test course, segment tree"},"content":{"rendered":"<article>\n<header>\n<p>Written on: <time datetime=\"2023-10-08\">October 8, 2023<\/time><\/p>\n<\/header>\n<section>\n<h2>Table of Contents<\/h2>\n<ol>\n<li><a href=\"#intro\">1. Introduction to Segment Trees<\/a><\/li>\n<li><a href=\"#problem\">2. Problem Description<\/a><\/li>\n<li><a href=\"#solution\">3. Solution Process<\/a><\/li>\n<li><a href=\"#complexity\">4. Time Complexity Analysis<\/a><\/li>\n<li><a href=\"#conclusion\">5. Conclusion<\/a><\/li>\n<\/ol>\n<\/section>\n<section id=\"intro\">\n<h2>1. Introduction to Segment Trees<\/h2>\n<p>A segment tree is a data structure designed for efficient interval query processing and updates.<br \/>\n        It is primarily designed for quickly handling sums, minimums, maximums, etc., over intervals in datasets like arrays.<br \/>\n        The segment tree is structured as a complete binary tree, and each node of the tree stores information about a specific interval.<\/p>\n<p>The main features of a segment tree are as follows:<\/p>\n<ul>\n<li>Interval Query: It allows for quick retrieval of values within a specified range.<\/li>\n<li>Update Functionality: The tree can be efficiently updated whenever the data changes.<\/li>\n<li>Relatively Low Memory Usage: It requires relatively less memory compared to using an array.<\/li>\n<\/ul>\n<\/section>\n<section id=\"problem\">\n<h2>2. Problem Description<\/h2>\n<p>Consider the following problem. Given an integer array <code>arr<\/code>, write a program that supports two functionalities: a query to find the sum of a specific interval [<code>l<\/code>, <code>r<\/code>] and an update to set the value at the <code>i<\/code>th position to <code>val<\/code>.<\/p>\n<p>The input format for the problem is as follows:<\/p>\n<ul>\n<li>First line: Size of the array <code>N<\/code> (1 \u2264 <code>N<\/code> \u2264 100,000)<\/li>\n<li>Second line: Elements of the array <code>arr[1], arr[2], ..., arr[N]<\/code><\/li>\n<li>Third line: Number of queries <code>Q<\/code><\/li>\n<li>Next <code>Q<\/code> lines: Three integers <code>type, l, r<\/code> for each query (type = 1: interval sum query, type = 2: update query)<\/li>\n<\/ul>\n<p>For example, consider the following input:<\/p>\n<pre>\n5\n1 2 3 4 5\n3\n1 1 3\n2 2 10\n1 1 5\n        <\/pre>\n<p>Here, the first query requests the sum of the interval [1, 3], the second query updates the second element to 10, and the third query requests the sum of the interval [1, 5] after the update.<\/p>\n<\/section>\n<section id=\"solution\">\n<h2>3. Solution Process<\/h2>\n<p>To solve this problem using a segment tree, the following approach can be used.<\/p>\n<h3>3.1. Constructing the Segment Tree<\/h3>\n<p>First, we need to construct the segment tree from the given array <code>arr<\/code>.<br \/>\n        The parent node stores the sum of its child nodes&#8217; values.<br \/>\n        The tree can be initialized as follows:<\/p>\n<pre>\nclass SegmentTree:\n    def __init__(self, data):\n        self.n = len(data)\n        self.tree = [0] * (2 * self.n)\n        # Insert data into leaf nodes\n        for i in range(self.n):\n            self.tree[self.n + i] = data[i]\n        # Calculate internal nodes\n        for i in range(self.n - 1, 0, -1):\n            self.tree[i] = self.tree[i * 2] + self.tree[i * 2 + 1]\n        <\/pre>\n<h3>3.2. Processing Interval Sum Queries<\/h3>\n<p>To process interval sum queries, we need to traverse from the leaf nodes up to the root node.<br \/>\n        To get the sum over the interval [l, r], we can implement as follows:<\/p>\n<pre>\n    def query(self, l, r):\n        result = 0\n        l += self.n\n        r += self.n + 1\n        while l < r:\n            if l % 2 == 1:\n                result += self.tree[l]\n                l += 1\n            if r % 2 == 1:\n                r -= 1\n                result += self.tree[r]\n            l \/\/= 2\n            r \/\/= 2\n        return result\n        <\/pre>\n<h3>3.3. Processing Updates<\/h3>\n<p>An update query modifies the value at a specific index, affecting that node and its parent nodes.<br \/>\n        It can be implemented as follows:<\/p>\n<pre>\n    def update(self, index, value):\n        index += self.n\n        self.tree[index] = value\n        while index > 1:\n            index \/\/= 2\n            self.tree[index] = self.tree[index * 2] + self.tree[index * 2 + 1]\n        <\/pre>\n<h3>3.4. Complete Code<\/h3>\n<p>Now let's write the complete code that includes all the components above:<\/p>\n<pre>\ndef main():\n    import sys\n    input = sys.stdin.read\n    data = input().split()\n    \n    idx = 0\n    N = int(data[idx]); idx += 1\n    arr = [0] * N\n    for i in range(N):\n        arr[i] = int(data[idx]); idx += 1\n    Q = int(data[idx]); idx += 1\n    \n    seg_tree = SegmentTree(arr)\n    \n    output = []\n    for _ in range(Q):\n        query_type = int(data[idx]); idx += 1\n        l = int(data[idx]); idx += 1\n        r = int(data[idx]); idx += 1\n        if query_type == 1:\n            result = seg_tree.query(l - 1, r - 1)\n            output.append(str(result))\n        elif query_type == 2:\n            seg_tree.update(l - 1, r)\n    \n    print('\\n'.join(output))\n\nif __name__ == \"__main__\":\n    main()\n        <\/pre>\n<\/section>\n<section id=\"complexity\">\n<h2>4. Time Complexity Analysis<\/h2>\n<p>The time complexity of the segment tree is as follows:<\/p>\n<ul>\n<li>Constructing the segment tree: O(N)<\/li>\n<li>Interval sum query: O(log N)<\/li>\n<li>Update query: O(log N)<\/li>\n<\/ul>\n<p>Therefore, this algorithm can operate efficiently even with large datasets.<\/p>\n<\/section>\n<section id=\"conclusion\">\n<h2>5. Conclusion<\/h2>\n<p>In this article, we explored how to handle interval sum queries and update queries using segment trees.<br \/>\n        Segment trees are a powerful data structure that can be used effectively in various problems.<br \/>\n        During coding tests, it is advisable to consider segment trees when facing interval query-related problems.<\/p>\n<\/section>\n<\/article>\n","protected":false},"excerpt":{"rendered":"<p>Written on: October 8, 2023 Table of Contents 1. Introduction to Segment Trees 2. Problem Description 3. Solution Process 4. Time Complexity Analysis 5. Conclusion 1. Introduction to Segment Trees A segment tree is a data structure designed for efficient interval query processing and updates. It is primarily designed for quickly handling sums, minimums, maximums, &hellip; <a href=\"https:\/\/atmokpo.com\/w\/33672\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;python 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":[145],"tags":[],"class_list":["post-33672","post","type-post","status-publish","format-standard","hentry","category-python-coding-test"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>python 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\/33672\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"python coding test course, segment tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Written on: October 8, 2023 Table of Contents 1. Introduction to Segment Trees 2. Problem Description 3. Solution Process 4. Time Complexity Analysis 5. Conclusion 1. Introduction to Segment Trees A segment tree is a data structure designed for efficient interval query processing and updates. It is primarily designed for quickly handling sums, minimums, maximums, &hellip; \ub354 \ubcf4\uae30 &quot;python coding test course, segment tree&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/33672\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:19:12+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:47:11+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=\"3\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/33672\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33672\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"python coding test course, segment tree\",\"datePublished\":\"2024-11-01T09:19:12+00:00\",\"dateModified\":\"2024-11-01T11:47:11+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33672\/\"},\"wordCount\":461,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"Python Coding Test\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/33672\/\",\"url\":\"https:\/\/atmokpo.com\/w\/33672\/\",\"name\":\"python coding test course, segment tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:19:12+00:00\",\"dateModified\":\"2024-11-01T11:47:11+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/33672\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/33672\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/33672\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"python 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":"python 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\/33672\/","og_locale":"ko_KR","og_type":"article","og_title":"python coding test course, segment tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Written on: October 8, 2023 Table of Contents 1. Introduction to Segment Trees 2. Problem Description 3. Solution Process 4. Time Complexity Analysis 5. Conclusion 1. Introduction to Segment Trees A segment tree is a data structure designed for efficient interval query processing and updates. It is primarily designed for quickly handling sums, minimums, maximums, &hellip; \ub354 \ubcf4\uae30 \"python coding test course, segment tree\"","og_url":"https:\/\/atmokpo.com\/w\/33672\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:19:12+00:00","article_modified_time":"2024-11-01T11:47:11+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":"3\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/33672\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/33672\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"python coding test course, segment tree","datePublished":"2024-11-01T09:19:12+00:00","dateModified":"2024-11-01T11:47:11+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/33672\/"},"wordCount":461,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["Python Coding Test"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/33672\/","url":"https:\/\/atmokpo.com\/w\/33672\/","name":"python coding test course, segment tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:19:12+00:00","dateModified":"2024-11-01T11:47:11+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/33672\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/33672\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/33672\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"python 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\/33672","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=33672"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33672\/revisions"}],"predecessor-version":[{"id":33673,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/33672\/revisions\/33673"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=33672"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=33672"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=33672"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}