线段树 模板
染色
插入算法
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;