2021年6月18日 星期五

Segment Tree 筆記

Segment-Tree 介紹

主要用來找區間最大值區間總和。由於我是為了這題所以以區間總和做介紹。下面陣列是應對區間總和,每個 node 紀錄的有起始點, 結束點及區間總和,EXAMPLE: [0,4] 代表 index 0 ~ 4 的總和,其總和為 10,[0,0] 代表 index 0~0 也就是 nums[0] 的本身值。至於樹為什麼長這樣跟 Build tree 有關。
    // nums:[-1, 4, 2, 0, 5]
    //
    // Segment Tree for the Above Array:
    //
    //         10                      [0,4]
    //        /  \
    //      5      5           [0,2]          [3,4]
    //     / \    / \
    //    3   2  0   5      [0,1]  [2,2]   [3,3]  [4,4]
    //   / \   
    // -1  4            [0,0]  [1,1]

Segment-Tree 實作

  • Build Tree
    這邊我用 c++ 概略作介紹。基本上是 top-down 的 build 法,時間複雜度為 O(n)。
        // Node 資料結構
        class segTreeNode {
        public:
            int begin, end, sum;
            segTreeNode * left = NULL;
            segTreeNode * right = NULL;
            segTreeNode(int s, int e, int m):begin(s), end(e), sum(m) {}
        };
    
        // Build
        segTreeNode* buildSegTree(int begin, int end, vector& nums) {
            // 若 begin = end,代表是 leaf。
            if (begin == end) {
                auto node = new segTreeNode(begin, begin, nums[begin]);
                return node;
            }  
            
            // 分割
            int mid = (begin + end) / 2;
            auto left  = buildSegTree(begin, mid, nums);
            auto right = buildSegTree(mid+1, end, nums);
            auto node = new segTreeNode(begin, end, left->sum + right->sum);
            node->left = left;
            node->right = right;
            return node;
        }
    
  • Update tree
    時間複雜度為 O(log n)。
        // Update
        void updateSegTree(segTreeNode* node, int index, int val) {
            
            // 若是該 index 的 leaf。則更新其 sum
            if (node->begin == index && node->end == index) {
                node->sum = val;
                return;
            }
            
            // 若是中間 node 則分割往下繼續找
            int mid = (node->begin + node->end) / 2;
            if (index > mid)
                updateSegTree(node->right, index, val);
            else
                updateSegTree(node->left, index, val);
                
            // 最後更新 node's sum
            node->sum = node->left->sum + node->right->sum;
        }
    
  • Sum
    時間複雜度為 O(log n + k)。
        // Sum
        int sumSegTree(segTreeNode* node, int left, int right) {
            // 若完美覆蓋區間,直接回傳。
            if (node->begin == left && node->end == right)
                return node->sum;
            
            int mid = (node->begin + node->end) / 2;
            
            // 要求區間完全在左邊
            if (right <= mid)
                return sumSegTree(node->left, left, right);
            // 要求區間完全在右邊
            else if (left > mid)
                return sumSegTree(node->right, left, right);
            // 兩邊都有
            else
                return sumSegTree(node->left, left, mid) + 
                       sumSegTree(node->right, mid+1, right);
        }
    
參考資料 :
1.leetcode/hg3994 answer
2.youtube 中文講解

Related Posts:

  • Python - BeautifulSoup 基本應用 (1) BeautifulSoup 紀錄下 Python 網頁爬蟲大部分會用到的 BeautifulSoup。BeautifulSoup 本質上就是 parser,知道這點後其 function 和 parameter 的使用就能得心應手。 Install 這裡紀錄使用 pip 安裝 pip install beautifulsoup4 pip install lxml # 非必要 I… Read More
  • Python - BeautifulSoup 基本應用 (2) Navigating the tree - Going down child Tag 前一篇呼叫 Tag 底下的 subTag 是用像 class 的方式 (Tag.subTag),沒完整說明的是這個 subTag 可呼叫到的範圍並不只侷限 children Tag,而是整個 Tag 底下第一個遇到的 Tag.name == subTag。 """ html <b class="bo… Read More
  • Python Error - UnicodeDecodeError: 'cp950' codec can't decode 讀檔時 UnicodeDecodeError: 'cp950' codec can't decode 老實說這非常常見,即使你的檔案是用 UTF-8 編寫,而且用 Python3 ( 絕大部分 default 是 utf-8 ),仍會報這個錯。所以要避免程式能在不同平台都能正常使用,讀檔時最好都加上 encoding="utf-8" with open("somefile.py", encoding="utf-8"… Read More
  • Google API - Python 學習筆記 - Upload post Google Blogger API Table 其實找到好用的 Google API document 就完成 80% 了,這裡紀錄 Python 連結。再紀錄個 Google 給開發者用的 Playground,可以讓你先試試 API。 Post article 這裡紀錄程式碼模擬平常的流程,但其實可以只用 insert 就完成所有事。 New Post 新增草稿。 # Create a… Read More
  • Google API - Python 學習筆記 Google API - python ver. 紀錄一些使用 python 呼叫 Google API 的心得 基本前置作業 Google Account 首先你要有 google 帳號。 Google Cloud Platform project 想要使用 Google Cloud Platform (GCP) 的 service,你需要創建一個 GCP project。左上角 pr… Read More

0 意見:

張貼留言

Popular Posts