{"id":34054,"date":"2024-11-01T09:23:36","date_gmt":"2024-11-01T09:23:36","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=34054"},"modified":"2024-11-01T10:53:49","modified_gmt":"2024-11-01T10:53:49","slug":"c-coding-test-course-segment-tree","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/34054\/","title":{"rendered":"C# Coding Test Course, Segment Tree"},"content":{"rendered":"<p><body><\/p>\n<p>\n    One of the techniques for efficiently solving problems in algorithm competitions or coding interviews is the <strong>Segment Tree<\/strong>.<br \/>\n    In this article, we will explore the basic concepts of segment trees and how to use them through practical problems. Segment trees can be very useful in interval query problems.<br \/>\n    In particular, they can efficiently handle operations such as range sum, range minimum\/maximum, and range updates.\n<\/p>\n<h2>What is a Segment Tree?<\/h2>\n<p>\n    A segment tree is a data structure used to efficiently manage information about intervals of an array.<br \/>\n    It allows for query operations and updates on specific intervals to be processed in logarithmic time complexity.<br \/>\n    A segment tree is generally structured as a binary tree, where each node stores information about a specific interval.\n<\/p>\n<h3>Structure of a Segment Tree<\/h3>\n<p>\n    The segment tree consists of the following structure:\n<\/p>\n<ul>\n<li>Leaf Nodes: Represent each element of the array.<\/li>\n<li>Internal Nodes: Represent the information of intervals based on the information of their child nodes (leaf nodes). For example, they store sums, minima, or maxima of their leaf nodes.<\/li>\n<\/ul>\n<h2>Problem Introduction<\/h2>\n<p>\n    Now, let&#8217;s introduce an algorithm problem that can be solved using a segment tree.\n<\/p>\n<h3>Problem Description<\/h3>\n<p>\n    Write a program that efficiently handles the following two operations on a given integer array.\n<\/p>\n<ol>\n<li>Range Sum Query: Calculate the sum of a specific range [l, r] in the array.<\/li>\n<li>Range Update: Update the value at a specific index in the array and reflect changes accordingly.<\/li>\n<\/ol>\n<h3>Input Format<\/h3>\n<p>\n    The first line contains the size of the array <code>N<\/code> and the number of queries <code>M<\/code>. (1 \u2264 N \u2264 100,000, 1 \u2264 M \u2264 100,000)<br \/>\n    Following this, <code>N<\/code> integers are given for the array, and then <code>M<\/code> queries are provided.\n<\/p>\n<p>The format of the queries is as follows:<\/p>\n<ul>\n<li>1 l r: Outputs the sum from index l to r of the array (1 \u2264 l \u2264 r \u2264 N)<\/li>\n<li>2 x y: Updates the x-th element of the array to y (1 \u2264 x \u2264 N, -1,000,000,000 \u2264 y \u2264 1,000,000,000)<\/li>\n<\/ul>\n<h2>Implementation of Segment Tree<\/h2>\n<p>\n    Now we will implement a segment tree in C# to solve the above problem.\n<\/p>\n<pre><code>\nusing System;\n\nclass SegmentTree\n{\n    private int[] tree;\n    private int size;\n\n    public SegmentTree(int n)\n    {\n        size = n;\n        tree = new int[n * 4]; \/\/ Size of the segment tree\n    }\n\n    \/\/ Build the tree\n    public void Build(int[] arr, int node, int start, int end)\n    {\n        if (start == end)\n        {\n            tree[node] = arr[start]; \/\/ Leaf node\n        }\n        else\n        {\n            int mid = (start + end) \/ 2;\n            Build(arr, node * 2, start, mid); \/\/ Left child\n            Build(arr, node * 2 + 1, mid + 1, end); \/\/ Right child\n            tree[node] = tree[node * 2] + tree[node * 2 + 1]; \/\/ Update parent node\n        }\n    }\n\n    \/\/ Range sum query\n    public int Query(int node, int start, int end, int l, int r)\n    {\n        if (r < start || end < l)\n        {\n            return 0; \/\/ Range not evaluable\n        }\n\n        if (l <= start &#038;&#038; end <= r)\n        {\n            return tree[node]; \/\/ Completely inside\n        }\n\n        int mid = (start + end) \/ 2;\n        int leftSum = Query(node * 2, start, mid, l, r);\n        int rightSum = Query(node * 2 + 1, mid + 1, end, l, r);\n        return leftSum + rightSum; \/\/ Return sum of both ranges\n    }\n\n    \/\/ Update\n    public void Update(int node, int start, int end, int idx, int val)\n    {\n        if (start == end)\n        {\n            tree[node] = val; \/\/ Update leaf node\n        }\n        else\n        {\n            int mid = (start + end) \/ 2;\n            if (start <= idx &#038;&#038; idx <= mid)\n            {\n                Update(node * 2, start, mid, idx, val); \/\/ Update left child\n            }\n            else\n            {\n                Update(node * 2 + 1, mid + 1, end, idx, val); \/\/ Update right child\n            }\n            tree[node] = tree[node * 2] + tree[node * 2 + 1]; \/\/ Update parent node\n        }\n    }\n}\n\nclass Program\n{\n    static void Main(string[] args)\n    {\n        int N, M;\n        N = int.Parse(Console.ReadLine());\n        M = int.Parse(Console.ReadLine());\n\n        int[] arr = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);\n        SegmentTree segTree = new SegmentTree(N);\n        segTree.Build(arr, 1, 0, N - 1); \/\/ Build segment tree\n\n        for (int i = 0; i < M; i++)\n        {\n            string[] query = Console.ReadLine().Split(' ');\n            int type = int.Parse(query[0]);\n\n            if (type == 1)\n            {\n                \/\/ Range sum query\n                int l = int.Parse(query[1]) - 1;\n                int r = int.Parse(query[2]) - 1;\n                Console.WriteLine(segTree.Query(1, 0, N - 1, l, r));\n            }\n            else if (type == 2)\n            {\n                \/\/ Update\n                int x = int.Parse(query[1]) - 1;\n                int y = int.Parse(query[2]);\n                segTree.Update(1, 0, N - 1, x, y);\n            }\n        }\n    }\n}\n<\/code><\/pre>\n<h2>Code Explanation<\/h2>\n<p>\n    The above C# code represents the implementation of a segment tree to solve the given problem.<br \/>\n    The explanation for each method and class is as follows:\n<\/p>\n<h3>Class and Variable Explanation<\/h3>\n<ul>\n<li><code>SegmentTree<\/code>: A class representing the segment tree. It takes an array to build the tree and handles range sum queries and updates.<\/li>\n<li><code>tree<\/code>: An array representing the segment tree. The maximum size of the array is <code>N * 4<\/code>.<\/li>\n<li><code>Build<\/code>: A method that constructs the segment tree from the given array.<\/li>\n<li><code>Query<\/code>: A method that returns the sum for a given interval.<\/li>\n<li><code>Update<\/code>: A method that updates the value at a given index and reflects changes in the tree.<\/li>\n<\/ul>\n<h3>Main Function Explanation<\/h3>\n<p>\n    The main function takes the size of the array and the number of queries as input, builds the segment tree with the given array, and then, based on the queries,<br \/>\n    computes the range sum or performs updates.\n<\/p>\n<h2>Conclusion<\/h2>\n<p>\n    In this article, we explored the implementation of a segment tree using C# and how to solve range sum query and update problems.<br \/>\n    The segment tree is a powerful tool for efficiently handling interval queries and can be applied to various problems.<br \/>\n    When faced with problems requiring complex queries or updates, it is advisable to consider using a segment tree.<br \/>\n    I hope you deepen your understanding of this data structure by solving various practice problems.\n<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the techniques for efficiently solving problems in algorithm competitions or coding interviews is the Segment Tree. In this article, we will explore the basic concepts of segment trees and how to use them through practical problems. Segment trees can be very useful in interval query problems. In particular, they can efficiently handle operations &hellip; <a href=\"https:\/\/atmokpo.com\/w\/34054\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;C# 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":[90],"tags":[],"class_list":["post-34054","post","type-post","status-publish","format-standard","hentry","category-c-coding-test-tutorials"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C# 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\/34054\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C# Coding Test Course, Segment Tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"One of the techniques for efficiently solving problems in algorithm competitions or coding interviews is the Segment Tree. In this article, we will explore the basic concepts of segment trees and how to use them through practical problems. Segment trees can be very useful in interval query problems. In particular, they can efficiently handle operations &hellip; \ub354 \ubcf4\uae30 &quot;C# Coding Test Course, Segment Tree&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/34054\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:23:36+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T10:53:49+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\/34054\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34054\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"C# Coding Test Course, Segment Tree\",\"datePublished\":\"2024-11-01T09:23:36+00:00\",\"dateModified\":\"2024-11-01T10:53:49+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34054\/\"},\"wordCount\":551,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"articleSection\":[\"C# Coding Test Tutorials\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/34054\/\",\"url\":\"https:\/\/atmokpo.com\/w\/34054\/\",\"name\":\"C# Coding Test Course, Segment Tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:23:36+00:00\",\"dateModified\":\"2024-11-01T10:53:49+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/34054\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/34054\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/34054\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C# 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":"C# 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\/34054\/","og_locale":"ko_KR","og_type":"article","og_title":"C# Coding Test Course, Segment Tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"One of the techniques for efficiently solving problems in algorithm competitions or coding interviews is the Segment Tree. In this article, we will explore the basic concepts of segment trees and how to use them through practical problems. Segment trees can be very useful in interval query problems. In particular, they can efficiently handle operations &hellip; \ub354 \ubcf4\uae30 \"C# Coding Test Course, Segment Tree\"","og_url":"https:\/\/atmokpo.com\/w\/34054\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:23:36+00:00","article_modified_time":"2024-11-01T10:53:49+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\/34054\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/34054\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"C# Coding Test Course, Segment Tree","datePublished":"2024-11-01T09:23:36+00:00","dateModified":"2024-11-01T10:53:49+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/34054\/"},"wordCount":551,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"articleSection":["C# Coding Test Tutorials"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/34054\/","url":"https:\/\/atmokpo.com\/w\/34054\/","name":"C# Coding Test Course, Segment Tree - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:23:36+00:00","dateModified":"2024-11-01T10:53:49+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/34054\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/34054\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/34054\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"C# 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\/34054","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=34054"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34054\/revisions"}],"predecessor-version":[{"id":34055,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/34054\/revisions\/34055"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=34054"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=34054"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=34054"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}