• 线段树 模板


    染色

    插入算法

    procedure Insert(p, l, r, a, b, c: Integer);
    var
        m: Integer;
    begin
        if Tree[p].cover <> c then
        begin
            m := (l + r) div 2;
            if (a = l) and (b = r) then Tree[p].cover := c
            else begin
                if Tree[p].cover >= 0 then
                begin
                    Tree[p * 2].cover := Tree[p].cover;

                    Tree[p * 2 + 1].cover := Tree[p].cover;
                    Tree[p].cover := -1;
                end;
                if b <= m then Insert(p * 2, l, m, a, b, c)
                else if a >= m then  Insert(p * 2 + 1, m, r, a, b, c)
                else begin
                    Insert(p * 2, l, m, a, m, c);
                    Insert(p * 2 + 1, m, r, m, b, c);
                end;
            end;
        end;
    end;

    统计算法

    procedure Count(p, l, r: Integer);
    begin
        if Tree[p].cover >= 0 then Flag[Tree[p].cover] := 1
        else if r – l > 1 then
        begin
            Count(p * 2, l, (l + r) div 2);
            Count(p * 2 + 1, (l + r) div 2, r);
        end;
    end;

    统计算法

    function Count(p, l, r: Integer; var lc, rc: Integer): Integer;
    var
        result, tl, tr: Integer;
    begin
        if Tree[p].cover >= 0 then
        begin
            lc := Tree[p].cover;
            rc := Tree[p].cover;
            if Tree[p].cover > 0 then Count := 1
            else Count := 0;

        end     else if r – l > 1 then     begin        

        result := Count(p * 2, l, (l + r) div 2, lc, tl) + Count(p * 2 + 1, (l + r) div 2, r, tr, rc);       

      if (tl = tc) and (tl > 0) then         

      result := result - 1;        

    Count := result;     end; end;

      

  • 相关阅读:
    POJ1094查分约束,判断关系是否唯一
    POJ1087DFS+匈牙利或者DINIC
    POJ1087DFS+匈牙利或者DINIC
    #Leetcode# 34. Find First and Last Position of Element in Sorted Array
    #Leetcode# 18. 4Sum
    #Leetcode# 16. 3Sum Closest
    #Leetcode# 15. 3Sum
    #Leetcode# 42. Trapping Rain Water
    #Leetcode# 63. Unique Paths II
    #Leetcode# 62. Unique Paths
  • 原文地址:https://www.cnblogs.com/jackes/p/2453940.html
Copyright © 2020-2023  润新知