• 线段树 模板


    染色

    插入算法

    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;

      

  • 相关阅读:
    P5107 能量采集
    P4655 [CEOI2017]Building Bridges
    P1129 [ZJOI2007]矩阵游戏
    P5299 [PKUWC2018]Slay the Spire
    P1625求和 giao精大杂烩
    背包
    根号分治
    CF963B
    国王游戏
    P6006 USACO 3SUM G
  • 原文地址:https://www.cnblogs.com/jackes/p/2453940.html
Copyright © 2020-2023  润新知