题目大意
桌子上零散地放着若干个盒子,桌子的后方是一堵墙。如右图所示。现在从桌子的前射来一束平行光, 把盒子的影子投射到了墙上。问影子的总宽度是多少?
样例输入
20 //桌面总宽度
4 //盒子数量
1 5
3 8
7 10
13 19
样例输出
15
思路:
简化题目得:x轴上有若干条线段,求覆盖的总长度。给线段树每个节点增加一个域cover。cover=1表示该结点所对应的区间被完全覆盖,cover=0表示该结点所对应的区间未被完全
覆盖。最后统计被完全覆盖的节点的长度。当然方法不统一,也可以用最暴力的暴力,数据范围大时可以考虑离散化,都是不错的解法。这里就不放出来了。
源代码/pas:
program Project1;
type
tree=record
x,y,c:longint;
end;
var
n,m:longint;
t:array[1..1000]of tree;
procedure insert(f,l,r:longint);
var
mid:longint;
begin
if t[f].c=0 then
begin
mid:=(t[f].x+t[f].y)div 2;
if (t[f].x=l)and(t[f].y=r) then
t[f].c:=1
else
if (r<=mid) then
insert(f*2,l,r)
else
if (l>=mid) then
insert(f*2+1,l,r)
else
begin
insert(f*2,l,mid);
insert(f*2+1,mid,r);
end;
end;
end;
function count(f:longint):longint;
begin
if t[f].c=1 then
count:=t[f].y-t[f].x
else
if t[f].y-t[f].x=0 then
count:=0
else
count:=count(f*2)+count(f*2+1);
end;
procedure create(f:longint);
var
mid:longint;
begin
if t[f].x<>t[f].y-1 then
begin
mid:=(t[f].x+t[f].y)div 2;
t[f*2].x:=t[f].x;
t[f*2].y:=mid;
t[f*2+1].x:=mid;
t[f*2+1].y:=t[f].y;
create(f*2);
create(f*2+1);
end;
end;
procedure init;
var
i,u,v:longint;
begin
readln(n);
readln(m);
t[1].x:=1;
t[1].y:=n;
create(1);
for i:=1 to m do
begin
readln(u,v);
insert(1,u,v);
end;
writeln(count(1));
end;
begin
init;
end.