• 动态线段树


    class CountIntervals {
        Node root;
    
        public CountIntervals() {
            root = new Node(1, (int)1e9);
        }
    
        public void add(int left, int right) {
            root.update(root, left, right);
        }
    
        public int count() {
            return root.val;
        }
    }
    
    class Node {
        int left;
        int right;
        int val;
        int mark;
        Node leftNode;
        Node rightNode;
    
        public Node(int l, int r) {
            left = l;
            right = r;
        }
    
        public Node getLeftNode() {
            if (leftNode == null) {
                leftNode = new Node(left, left + (right - left) / 2);
            }
            return leftNode;
        }
    
        public Node getRightNode() {
            if (rightNode == null) {
                rightNode = new Node(left + (right - left) / 2 + 1, right);
            }
            return rightNode;
        }
    
        public void update(Node node, int lo, int hi) {
            if (node.left > hi || node.right < lo) {
                return;
            }
            if (node.left >= lo && node.right <= hi) {
                node.mark = 1;
            } else {
                update(node.getLeftNode(), lo, hi);
                update(node.getRightNode(), lo, hi);
            }
            if (node.mark > 0) {
                node.val = node.right - node.left + 1;
            } else {
                node.val = node.getLeftNode().val + node.getRightNode().val;
            }
        }
    }
    

      

  • 相关阅读:
    13年7月memory point
    getDefinitionByName getDefinition 区别
    cocos2d-html5版日历组件
    一个js对象的代码结构
    计算机操作系统复习
    计算机组成原理复习
    最新的hustoj搭建姿势
    推荐算法学习笔记
    BUPT 2012复试机考 4T
    BUPT 2012复试机考 3T
  • 原文地址:https://www.cnblogs.com/lhclqslove/p/16274688.html
Copyright © 2020-2023  润新知