线段树练习题一

线段树练习题一

Time Limit:10000MS Memory Limit:65536K
Total Submit:32 Accepted:14
Case Time Limit:1000MS

Description

桌子上零散地放着若干个盒子,桌子的后方是一堵墙。如右图所示。现在从桌子的前方射来一束平行光, 把盒子的影子投射到了墙上。问影子的总宽度是多少?

Input

Output

Sample Input

20 //桌面总宽度
4 //盒子数量
1 5
3 8
7 10
13 19
Sample Output

15
Hint

数据范围
1<=n<=100000,1<=m<=100000,保证坐标范围为[1,n].
做法:线段树模板
代码如下:

const
  maxn=1000000;
var
  c:array[0..maxn] of longint;
  n,m,a,b,i,ans:longint;

procedure insert(p,l,r,a,b:longint);
var
  m:longint;
begin
  if c[p]=0 then
    begin
      m:=(l+r) div 2;
      if (a=l)and(b=r) then c[p]:=1
      else if b<=m then insert(p*2,l,m,a,b)
      else if a>=m then insert(p*2+1,m,r,a,b)
      else begin
             insert(p*2,l,m,a,m);
             insert(p*2+1,m,r,m,b);
           end;
    end;
end;

function count(p,l,r:longint):longint;
begin
  if c[p]=1 then count:=r-l
  else if r-l=1 then count:=0
  else count:=count(p*2,l,(l+r) div 2)+count(p*2+1,(l+r) div 2,r);
end;

begin
  read(n,m);
  for i:=1 to m do
    begin
      readln(a,b);
      insert(1,1,n,a,b);
    end;
  ans:=count(1,1,n);
  writeln(ans);
end.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值